Java远程连接操作linux服务器,scp获取文件

scp和sftp运用例子,ganymed-ssh2-build210.jar包远程连接操作linux服务器

 

http://blog.sina.com.cn/s/blog_68fec59801015f5n.html

jar包  http://download.csdn.net/detail/wawaxbc/2801636#comment

 

1、建立连接

Connection con = newConnection(IP, PORT);

//连接

con.connect();

//远程服务器的用户名密码

boolean isAuthed = con.authenticateWithPassword(remoteUser,remotePass);

//建立SCP客户端

SCPClient scpClient = con.createSCPClient();

//服务器端的文件下载到本地的目录下

scpClient.getFile("/home/oracle/RUNNING.txt", "C:/");

//将本地文件上传到服务器端的目录下

scp.putFile("C:/RUNNING.txt", "/home/oracle");

 

 

建立一个SFTP客户端

SFTPv3Client sftpClient = new SFTPv3Client(con);

//远程新建目录

sftpClient.mkdir("newRemoteDir", 6);

//远程删除目录

sftpClient.rmdir(RemoteDir);

//远程新建文件

sftpClient.createFile("newRemoteFile");

//远程打开文件,可进行读写

sftpClient.openFileRW("remoteFile");

//建立会话

Session session = null;

session = con.openSession();

//利用会话可以操作远程服务器

//例如:删除远程目录下的文件

session.execCommand("rm -f".concat(remotePath).concat(qrCodeFileMode));

//显示执行命令后的信息

InputStream stdout = new StreamGobbler(session.getStdout());

BufferedReader br = new BufferedReader(new InputStreamReader(stdout));

 

while (true) {

String line = br.readLine();

if (line == null) {

log.info("远程服务器返回信息:空");

break;

}

log.info("远程服务器返回信息:" + line);

}

//获得推出状态

System.out.println("ExitCode: " + session.getExitStatus());

session.close();

con.close();

posted @ 2017-12-14 09:14  新月勾魂  阅读(3047)  评论(0编辑  收藏  举报