如何通过ssh登录服务器执行linux指令-ganymed的简单使用(一)
ganymed是用java实现的一个ssh协议包.通过他可以直接在java程序中连接ssh服务器并执行指令.使用步骤如下:1.创建Connection: Connection connection = new Connection("192.168.73.246").2.验证是否可以登录服务器: boolean isAuthenticated = conn.authenti
·
ganymed是用java实现的一个ssh协议包.通过他可以直接在java程序中连接ssh服务器并执行指令.
使用步骤如下:
1.创建Connection:
Connection connection = new Connection("192.168.73.246").
2.验证是否可以登录服务器:
boolean isAuthenticated = conn.authenticateWithPassword(username, password).true表示登陆成功,false表示登陆失败.
3.创建session,并执行指令:
Session session = conn.openSession();
session.execCommand("cd /home").
4.接收服务器上控制台的返回结果:
InputStream is = new StreamGobbler(session.getStdout());
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
5.获取指令是否成功执行:0-成功,非0-失败.
System.out.println("ExitCode: " + sess.getExitStatus());
6.关闭session和connection.
sess.close();
使用步骤如下:
1.创建Connection:
Connection connection = new Connection("192.168.73.246").
2.验证是否可以登录服务器:
boolean isAuthenticated = conn.authenticateWithPassword(username, password).true表示登陆成功,false表示登陆失败.
3.创建session,并执行指令:
Session session = conn.openSession();
session.execCommand("cd /home").
4.接收服务器上控制台的返回结果:
InputStream is = new StreamGobbler(session.getStdout());
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
5.获取指令是否成功执行:0-成功,非0-失败.
System.out.println("ExitCode: " + sess.getExitStatus());
6.关闭session和connection.
sess.close();
conn.close();
下面通过一个小例子来简单学习一下如何通过ganymed实现连接ssh服务器并执行指令.
1.直接看工具类:
<span style="font-size:12px;">package com.ilucky.ssh.util;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.Session;
import ch.ethz.ssh2.StreamGobbler;
/**
* @author IluckySi
* @date 20140805
*/
public class SshUtil {
private Connection connection;
public SshUtil(SshSetting sshSetting) {
try {
//判断SshSetting中的参数是否为空.
String ip = sshSetting.getIp();
String username = sshSetting.getUsername();
String password = sshSetting.getPassword();
if (ip == null || username == null || password == null) {
throw new Exception("SshSetting参数: ip, username和password不能为空!");
}
//判断Ssh服务器是否可以登录.
connection = new Connection(ip);
connection.connect();
boolean isAuthenticated = connection.authenticateWithPassword(username, password);
if (!isAuthenticated) {
throw new Exception("SshSetting参数: username和password不正确!");
}
} catch (Exception e) {
System.out.println("获取Connection发生问题!");
}
}
public String execute(String command) {
StringBuffer sb = new StringBuffer();
Session session = null;
InputStream is = null;
InputStreamReader isr = null;
BufferedReader br = null;
try {
//获取Session会话并执行指令.
session = connection.openSession();
session.execCommand(command);
//获取服务器上控制台的返回结果并编码为UTF-8.
is = new StreamGobbler(session.getStdout());
isr = new InputStreamReader(is, Charset.forName("UTF-8"));
br = new BufferedReader(isr);
//将服务器返回结果拼接成StringBuffer并返回.
String buffer = br.readLine();
while ((buffer = br.readLine()) != null) {
if (buffer != null) {
sb.append(buffer.toString() + "\n");
}
}
//获取指令在Ssh服务器上是否执行成功.
if(session.getExitStatus() == 0) {
System.out.println(command + "执行成功!");
} else {
System.out.println(command + "执行失败!");
}
} catch (Exception e) {
System.out.println("执行指令: " + command + "发生问题!");
} finally {
try {
if(br != null) {
br.close();
br = null;
}
if(isr != null) {
isr.close();
isr = null;
}
if(is != null) {
is.close();
is = null;
}
if(session != null) {
session.close();
session = null;
}
} catch (Exception e) {
System.out.println("关闭Session发生问题!");
}
}
return sb.toString();
}
public void finish() {
try {
if(connection != null) {
connection.close();
connection = null;
}
} catch (Exception e) {
System.out.println("关闭Connection发生问题!");
}
}
}
</span>
<span style="font-size:12px;">package com.ilucky.ssh.util;
/**
* @author IluckySi
* @date 20140805
*/
public class SshSetting {
private String ip;
private int port;
private String username;
private String password;
public String getIp() {
return ip;
}
public void setIp(String ip) {
this.ip = ip;
}
public int getPort() {
return port;
}
public void setPort(int port) {
this.port = port;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
</span>
2.再看测试类:
<span style="font-size:12px;">package com.ilucky.ssh;
import java.util.ArrayList;
import java.util.List;
import com.ilucky.ssh.util.SshSetting;
import com.ilucky.ssh.util.SshUtil;
/**
* @author IluckySi
* @date 20140805
*/
public class MainTest {
public static void main(String[] args) {
SshSetting sshSetting = new SshSetting();
sshSetting.setIp("192.168.73.246");
sshSetting.setPort(22); //ssh默认端口是22.
sshSetting.setUsername("root");
sshSetting.setPassword("Talent123");
SshUtil sshUtil = new SshUtil(sshSetting);
List<String> commondList = new ArrayList<String>();
commondList.add("cd /home");
commondList.add("ls");
commondList.add("ls -l");
for (int i = 0; i < commondList.size(); i++) {
System.out.println(sshUtil.execute(commondList.get(i)));
}
}
}
/**
运行结果:
cd /home执行成功!
ls执行成功!
anaconda-ks.cfg
install.log
install.log.syslog
tap-home
公共的
模板
视频
图片
文档
下载
音乐
桌面
ls -l执行成功!
-rw-r--r--. 1 root root 3233 2月 26 17:29 \
-rw-------. 1 root root 1301 8月 30 2013 anaconda-ks.cfg
-rw-r--r--. 1 root root 39043 8月 30 2013 install.log
-rw-r--r--. 1 root root 9936 8月 30 2013 install.log.syslog
drwxr-xr-x. 2 root root 4096 6月 26 10:08 tap-home
drwxr-xr-x. 2 root root 4096 8月 30 2013 公共的
drwxr-xr-x. 2 root root 4096 8月 30 2013 模板
drwxr-xr-x. 2 root root 4096 8月 30 2013 视频
drwxr-xr-x. 2 root root 4096 8月 30 2013 图片
drwxr-xr-x. 2 root root 4096 8月 30 2013 文档
drwxr-xr-x. 2 root root 4096 8月 30 2013 下载
drwxr-xr-x. 2 root root 4096 8月 30 2013 音乐
drwxr-xr-x. 2 root root 4096 12月 5 2013 桌面
*/
</span>
更多推荐
已为社区贡献2条内容
所有评论(0)