求解,关于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中截屏 存储到BufferedImage中 除了createScreenCapture之外还有什么其他的函数么? 求救SWING自定义按钮 主方法的问题 我在session里存了一个用户bean ,当一段时间后这个session释放掉后. 里面的bean 会不会自动释放掉? 独享模式读取文件 JAVA实现实时语音通信?????? 关于内部类的实例化问题 载入Java VM时错误??? 急:下面代码为什么不能运行,为什么? 请问如何在Java中调用一个win32的dll的接口函数呢? 求一个{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"));
}