求解,关于ganymed-ssh2-build210的应用 本帖最后由 hcdieche 于 2011-08-19 10:59:31 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 Runtime.getRuntime()方法有能用的例子也可以的 兄弟 我写了个给你看看 贴个代码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")); } 请问下面一代码是什么意思? 进程的互相控制 求助!初学java 一个比较棘手的SWING问题 在一个vm里面,同一个类的static变量在什么情况下会出现不同的实例? 请问用java如何得到当前操作系统的信息???比如说用的什么操作系统等等 请各位帮帮我写一个求二个数的绝对值代码?谢谢!现在就给分 大神给看看 大家进来讨论下JBuilder7和IBM AGE呀…… help thanks a lot 求一个{name}大括号内的正则表达式 求教初级java问题
{
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"));
}