您的位置:首页 > 运维架构 > Linux

JAVA连接Linux

2011-04-25 22:41 106 查看
先要在网上下个SSH.jar包,我要用的是一些文件相关的操作,以下是一个简单的例子。

package test;

import java.io.IOException;
import java.util.List;

import com.sshtools.j2ssh.SshClient;
import com.sshtools.j2ssh.authentication.AuthenticationProtocolState;
import com.sshtools.j2ssh.authentication.PasswordAuthenticationClient;
import com.sshtools.j2ssh.sftp.SftpFile;

/**
*
* @author qianj
* @version 1.0.0
* @2011-3-31 下午06:44:56
*/
public class Test {
public static void main(String[] args) {
SshClient client = new SshClient();
try {
client.connect("192.168.20.133", 22);//IP和端口
//设置用户名和密码
PasswordAuthenticationClient pwd = new PasswordAuthenticationClient();
pwd.setUsername("root");
pwd.setPassword("111111");
int result = client.authenticate(pwd);
if (result == AuthenticationProtocolState.COMPLETE) {//如果连接完成
List<SftpFile> list = client.openSftpClient().ls("./Desktop/hehe");
for (SftpFile f : list) {
System.out.println(f.getFilename());
System.out.println(f.getAbsolutePath());
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: