求解,关于ganymed-ssh2-build210的应用

hcdieche 2011-08-18 02:47:09
场景介绍:
1、脚本test.sh放在服务器A上(LINUX环境)
2、服务器A上搭建了WEB工程(JSP实现),用于监控和校验各LINUX服务器配置。
3、使用ganymed-ssh2-build210实现
4、想要监控的服务器B的IP为10.111.24.39
现已完成代码如下:

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.ConnectionInfo;
import ch.ethz.ssh2.Session;
import ch.ethz.ssh2.StreamGobbler;

public class Test {
public static void main(String[] args) {
try {
Connection con = new Connection("10.111.24.39");
ConnectionInfo info = con.connect();
boolean result = con.authenticateWithPassword("user", "pwd");
if(result!=false){
Session session = con.openSession();
session.execCommand(cmd);

InputStream stdout = new StreamGobbler(session.getStdout());
BufferedReader br = new BufferedReader(new InputStreamReader(stdout));
String str = br.readLine();
System.out.println("br"+str);
System.out.println("ExitCode: " + session.getExitStatus());
session.close();
}
con.close();
} catch (Exception ex) {
System.out.println(ex.getLocalizedMessage());
}
}
}

其中session.execCommand(cmd),若cmd为单独的命令,如who,执行没有问题,但如果是想执行放在服务器A上的脚本test.sh就报错,空指针异常。
想请教下,以上这样的需求是否能实现的,即脚本都放在一个服务器上,而不是放在所有服务器上,如果可以的话,想请问下上面的代码有什么问题,求指教。
...全文
514 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
y541397839 2011-09-07
  • 打赏
  • 举报
回复
楼主 最近我们也在做一个你这样的程序,不过我们实现用 密钥的方式登录,也遇到麻烦了啊
y541397839 2011-09-07
  • 打赏
  • 举报
回复
兄弟 我写了个给你看看 贴个代码

public class Monitor
{
private Connection conn;
private String ipAdd;
private String charset = Charset.defaultCharset().toString();
private String userName;
private String password;

/**
* 初始化变量
* 定义构造函数,创建对象时初始化变量
* @param ipAdd liunx服务器IP
* @param charset 对返回结果集编码
* @param userName 用户名
* @param password 密码
*/
public Monitor(String ipAdd,String charset,String userName,String password)
{
this.ipAdd = ipAdd;
this.userName = userName;
this.password = password;

if(charset != null)
{
this.charset = charset;
}
}

/**
* 远程登录Linux服务器
* @return 标示是否登录成功
* @throws IOException
*/
public boolean login() throws IOException
{
conn = new Connection(ipAdd);
//连接
conn.connect();
//认证登录
// return conn.authenticateWithPassword(userName, password);
File keyFile = new File("D:\\ssh\\id_dsa");
return conn.authenticateWithPublicKey("root", keyFile, null);
}

/**
* 执行Shell脚本
* 开启一个Session会话,执行sh脚本
* @param cmds 被执行的脚本
* @return 结果集文本
* @throws IOException
*/
public String exec(String cmds)
{
InputStream in = null;
String result = "";

try
{
if(this.login())
{
//打开一个会话
Session session = conn.openSession();
session.execCommand(cmds);
in = session.getStdout();
result = this.processStdout(in, charset);
}
}
catch(IOException e)
{
e.printStackTrace();
}
finally
{
conn.close();
}

return result;
}


/**
* 解析流获取字符串信息
* @param in 输入字节流
* @param charset 编码
* @return 字符串信息
* @throws IOException
*/
public String processStdout(InputStream in,String charset)
{
byte[] byt = new byte[1];
StringBuffer sb = new StringBuffer();

try
{
while(in.read(byt) != -1)
{
sb.append(new String(byt,charset));
}
}
catch(IOException e)
{
e.printStackTrace();
}

return sb.toString();
}

/**
* 测试接口
* @param args
*/
public static void main(String[] args)
{
Monitor test = new Monitor("10.78.222.111","UTF-8","","");
System.out.println(test.exec("sh -c /opt/yanpan/helloword.sh"));
}
niuniu20008 2011-08-25
  • 打赏
  • 举报
回复
技术有限,爱莫能助
小笨熊 2011-08-25
  • 打赏
  • 举报
回复
希望lz快点解决
luohuijun619 2011-08-25
  • 打赏
  • 举报
回复
没用过,帮顶
hcdieche 2011-08-18
  • 打赏
  • 举报
回复
Runtime.getRuntime()方法有能用的例子也可以的

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧