http://www.ganymed.ethz.ch/ssh2/ 一个SSH协议的JAVA实现包,下面我代码去调用它package cn.com.xinli.ssh.test;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.Session;
import ch.ethz.ssh2.StreamGobbler;
public class Basic
{
public static void main(String[] args)
{
String hostname = "172.100.1.64";
String username = "******";
String password = "******";
try
{
Connection conn = new Connection(hostname);
conn.connect();
boolean isAuthenticated = conn.authenticateWithPassword(username, password);
Session sess = conn.openSession();
///////////////////////////////////////////// 执行多个就报错
sess.execCommand("last");
/////////////////////////////////////////////
InputStream stdout = new StreamGobbler(sess.getStdout());
BufferedReader br = new BufferedReader(new InputStreamReader(stdout));
while (true)
{
String line = br.readLine();
if (line == null)
break;
System.out.println(line);
}
sess.close();
conn.close();
}
}
}
我现在要在Session处理多个command指令,如上述代码处
sess.execCommand("last");
sess.execCommand("last");
就会报错说Session不能2个,看FAQ,说需要用Session.startShell(), Session.requestPTY("last"),Session.requestPTY("last"),可我用了还是不行,有没有做过相关的大侠,如何才能支持多个操作进行
如果实在不行,我单独用一个线程处理一个session,又担心while不结束会有很多SESSION堆积
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.Session;
import ch.ethz.ssh2.StreamGobbler;
public class Basic
{
public static void main(String[] args)
{
String hostname = "172.100.1.64";
String username = "******";
String password = "******";
try
{
Connection conn = new Connection(hostname);
conn.connect();
boolean isAuthenticated = conn.authenticateWithPassword(username, password);
Session sess = conn.openSession();
///////////////////////////////////////////// 执行多个就报错
sess.execCommand("last");
/////////////////////////////////////////////
InputStream stdout = new StreamGobbler(sess.getStdout());
BufferedReader br = new BufferedReader(new InputStreamReader(stdout));
while (true)
{
String line = br.readLine();
if (line == null)
break;
System.out.println(line);
}
sess.close();
conn.close();
}
}
}
我现在要在Session处理多个command指令,如上述代码处
sess.execCommand("last");
sess.execCommand("last");
就会报错说Session不能2个,看FAQ,说需要用Session.startShell(), Session.requestPTY("last"),Session.requestPTY("last"),可我用了还是不行,有没有做过相关的大侠,如何才能支持多个操作进行
如果实在不行,我单独用一个线程处理一个session,又担心while不结束会有很多SESSION堆积
解决方案 »
- 求教数组小程序
- javamail 读取邮件内容时为什么邮件中的html内容读取不到?
- FileInputStream的Read方法的两次输出问题?
- JAVA导入图片的问题
- 如何将字符串转换成数字?比如“1234”转换成1234,不胜感激
- 刚用jdb调试程序,抛了个异常。。。。
- 请问各位老大,在java中如何写一个有返回值为DefaultMutableTreeNode的函数
- 求一小程序!!(急!!!)200分一定给!
- 大家新年好,散分啦!!!
- 初学者,选择什么样的教科书?请大家谈谈、
- 关于String类型加法拼接,求解答
- 自学编程从零基础 打算学JAVA方向,一直到安卓开发需要看那些书和视频?求高手指点几句
Note: If you really want to mimic the behavior of putty, then don't use Session.execCommand(), instead aquire a pty (pseudo terminal) and then start a shell (use Session.requestPTY() and Session.startShell()). You then have to communicate with the shell process at the other end through stdin and stdout. However, you also have to implement terminal logic (e.g., escape sequence handling (unless you use a "dumb" pty), "expect-send" logic (output parsing, shell prompt detection), etc.).
我看例子里面多个命令是用&&分隔的,如:sess.execCommand("uname -a && date && uptime && who");不妨试试。
借贵宝地问一个问题:sess.execCommand("ls");能打印出所有资料夹和文件的名称,sess.execCommand("ll");什么也打印不出来,这是为什么呢?
加;号就行了呀
sess.execCommand("last;last");