Ganymed SSH-2 for Java系列3之执行远程shell 命令
2014-04-18 12:35
701 查看
利用Ganymed SSH-2 for Java 连接到远程服务器,然后执行shell命令;
首先我们再在之前CommandRunner类中再添加一个执行shell命令的方法,具体如下所示:
[java]
view plaincopy
public static String execShellScript(String host, String username,
String password,
String cmd, int port) throws IOException {
if (logger.isInfoEnabled()) {
logger.info("running SSH cmd [" + cmd + "]");
}
Connection conn = null;
Session sess = null;
InputStream stdout = null;
BufferedReader br = null;
StringBuffer buffer = new StringBuffer("exec result:");
buffer.append(System.getProperty("line.separator"));// 换行
try {
conn = getOpenedConnection(host, username, password, port);
sess = conn.openSession();
sess.execCommand(cmd);
stdout = new StreamGobbler(sess.getStdout());
br = new BufferedReader(new InputStreamReader(stdout));
while (true) {
// attention: do not comment this block, or you will hit
// NullPointerException
// when you are trying to read exit status
String line = br.readLine();
if (line == null)
break;
buffer.append(line);
buffer.append(System.getProperty("line.separator"));// 换行
if (logger.isInfoEnabled()) {
logger.info(line);
}
}
} finally {
sess.close();
conn.close();
}
return buffer.toString();
}
测试代码:
[java]
view plaincopy
public static void main(String[] args) {
String cmd = "uname -a";
try {
String info = CommandRunner.execShellScript("172.16.18.141", "root",
"123456",cmd,22);
System.out.println("info is:"+info);
} catch (IOException e) {
e.printStackTrace();
}
}
执行结果log4j:WARN No appenders could be found for logger (com.ssh2.shell.ganymed.CommandRunner).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
info is:exec result:
Linux localhost.localdomain 2.6.32-220.el6.x86_64 #1 SMP Wed Nov 9 08:03:13 EST 2011 x86_64 x86_64 x86_64 GNU/Linux
至此,连接远程服务器执行shell命令简单方法就实现了。
首先我们再在之前CommandRunner类中再添加一个执行shell命令的方法,具体如下所示:
[java]
view plaincopy
public static String execShellScript(String host, String username,
String password,
String cmd, int port) throws IOException {
if (logger.isInfoEnabled()) {
logger.info("running SSH cmd [" + cmd + "]");
}
Connection conn = null;
Session sess = null;
InputStream stdout = null;
BufferedReader br = null;
StringBuffer buffer = new StringBuffer("exec result:");
buffer.append(System.getProperty("line.separator"));// 换行
try {
conn = getOpenedConnection(host, username, password, port);
sess = conn.openSession();
sess.execCommand(cmd);
stdout = new StreamGobbler(sess.getStdout());
br = new BufferedReader(new InputStreamReader(stdout));
while (true) {
// attention: do not comment this block, or you will hit
// NullPointerException
// when you are trying to read exit status
String line = br.readLine();
if (line == null)
break;
buffer.append(line);
buffer.append(System.getProperty("line.separator"));// 换行
if (logger.isInfoEnabled()) {
logger.info(line);
}
}
} finally {
sess.close();
conn.close();
}
return buffer.toString();
}
测试代码:
[java]
view plaincopy
public static void main(String[] args) {
String cmd = "uname -a";
try {
String info = CommandRunner.execShellScript("172.16.18.141", "root",
"123456",cmd,22);
System.out.println("info is:"+info);
} catch (IOException e) {
e.printStackTrace();
}
}
执行结果log4j:WARN No appenders could be found for logger (com.ssh2.shell.ganymed.CommandRunner).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
info is:exec result:
Linux localhost.localdomain 2.6.32-220.el6.x86_64 #1 SMP Wed Nov 9 08:03:13 EST 2011 x86_64 x86_64 x86_64 GNU/Linux
至此,连接远程服务器执行shell命令简单方法就实现了。
相关文章推荐
- NC(Net cat ) 使用详解(用作文件传输,端口转发,反弹Shell)
- shell-判断所指目录下的对象是一般文件还是目录
- shell-判断输入的文件是否存在
- shell-用grep查看输入的参数是否在/etc/passwd中
- shell――求和
- ftp自动上传下载文件(Shell脚本)及ftp命令详解
- linux shell实现随机数多种方法(date,random,uuid)
- Android中使用代码执行shell命令
- android adb shell 命令大全
- linux shell 命令学习(2) paste - merge lines of files
- shell程序设计(3)
- linux远程客户端putty,xshell搭建注意事项——《视频》
- shell脚本中的开关语句
- shell程序设计(2)
- Linux 之 shell 比较运算符
- shell语法简介
- [Windows Powershell]-学习笔记(6)
- Bash
- Shell(Bash)编程实例之获取某个文件夹下的所有文件名(含文件夹)
- Shell之Here Document