java 远程调用shell 上传文件到服务器
2013-10-29 15:20
671 查看
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.SCPClient;
import ch.ethz.ssh2.Session;
import ch.ethz.ssh2.StreamGobbler;
/**
* Purpose:
*
* @see
* @since 1.1.0
*/
public class Scp
{
public static void main(String[] args)
{
String user = "root";
String pass = "111111";
String host = "192.168.0.24";
Connection con = new Connection(host);
try
{
con.connect();
boolean isAuthed = con.authenticateWithPassword(user, pass);
System.out.println("isAuthed====" + isAuthed);
SCPClient scpClient = con.createSCPClient();
// scpClient.put("localFiles", "remoteDirectory"); //从本地复制文件到远程目录
// scpClient.get("remoteFiles","localDirectory"); //从远程获取文件
scpClient.put("F:\\lib\\ganymed-ssh2-build210\\ganymed-ssh2-build210.jar", "/usr/local/dota"); // 从本地复制文件到远程目录
//
// SFTPv3Client sftpClient = new SFTPv3Client(con);
// sftpClient.mkdir("newRemoteDir", 6); //远程新建目录
// sftpClient.rmdir(""); //远程删除目录
//
// sftpClient.createFile("newRemoteFile"); //远程新建文件
// sftpClient.openFileRW("remoteFile"); //远程打开文件,可进行读写
Session session = con.openSession();
session.execCommand("uname -a && date && uptime && who");
// 远程执行命令
// 显示执行命令后的信息
System.out.println("Here is some information about the remote host:");
InputStream stdout = new StreamGobbler(session.getStdout());
BufferedReader br = new BufferedReader(new InputStreamReader(stdout));
while (true)
{
String line = br.readLine();
if (line == null)
break;
System.out.println(line);
}
/* Show exit status, if available (otherwise "null") */
System.out.println("ExitCode: " + session.getExitStatus());
session.close();
con.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.SCPClient;
import ch.ethz.ssh2.Session;
import ch.ethz.ssh2.StreamGobbler;
/**
* Purpose:
*
* @see
* @since 1.1.0
*/
public class Scp
{
public static void main(String[] args)
{
String user = "root";
String pass = "111111";
String host = "192.168.0.24";
Connection con = new Connection(host);
try
{
con.connect();
boolean isAuthed = con.authenticateWithPassword(user, pass);
System.out.println("isAuthed====" + isAuthed);
SCPClient scpClient = con.createSCPClient();
// scpClient.put("localFiles", "remoteDirectory"); //从本地复制文件到远程目录
// scpClient.get("remoteFiles","localDirectory"); //从远程获取文件
scpClient.put("F:\\lib\\ganymed-ssh2-build210\\ganymed-ssh2-build210.jar", "/usr/local/dota"); // 从本地复制文件到远程目录
//
// SFTPv3Client sftpClient = new SFTPv3Client(con);
// sftpClient.mkdir("newRemoteDir", 6); //远程新建目录
// sftpClient.rmdir(""); //远程删除目录
//
// sftpClient.createFile("newRemoteFile"); //远程新建文件
// sftpClient.openFileRW("remoteFile"); //远程打开文件,可进行读写
Session session = con.openSession();
session.execCommand("uname -a && date && uptime && who");
// 远程执行命令
// 显示执行命令后的信息
System.out.println("Here is some information about the remote host:");
InputStream stdout = new StreamGobbler(session.getStdout());
BufferedReader br = new BufferedReader(new InputStreamReader(stdout));
while (true)
{
String line = br.readLine();
if (line == null)
break;
System.out.println(line);
}
/* Show exit status, if available (otherwise "null") */
System.out.println("ExitCode: " + session.getExitStatus());
session.close();
con.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
相关文章推荐
- 自动化测试工具_Calabash-android调研
- 我的一些简单的shell脚本实例
- Bash Shell脚本学习小结
- Bash One-Liners Explained(一):文件处理
- Bash One-Liners Explained (二):操作字符串
- VBS中WScript.Shell对象的run和exec的使用及区别
- Shell编程基础 part 2/2
- 如何避免SSH Secure Shell Client链接LINUX超时,自动断开?
- YARN编程实例—distributedshell源码分析
- android中运行shell
- 打开文件的shell脚本
- shell 批量修改密码
- 第一次亲密接触(1)
- Shell编程笔记
- Linux Shell编程入门
- Android shell 系统命令大全
- shell中$(( )) 与 $( ) 还有${ }的区别
- Delphi中ShellExecute的妙用
- Delphi ShellExecute的用法
- 【转】webshell中的不死僵尸和隐藏后门的原理以及删除