用ssh2連線linux實現putty功能範例程式碼
RemoteConnect remoteConnect;
remoteConnect=new RemoteConnect();
remoteConnect.setIp(ipAddr);
remoteConnect.setUserName(user);
remoteConnect.setPassword(password);
Connection conn = new Connection(remoteConnect.getIp());
conn.connect();
flag = conn.authenticateWithPassword(remoteConnect.getUserName(), remoteConnect.getPassword());
final Session session=conn.openSession();
session.requestDumbPTY();
session.execCommand("/bin/bash");
Runnable stdout = new Runnable(){
public void run() {
InputStream is = session.getStdout();
int b;
try {
while ( (b = is.read()) != -1) {
System.out.print((char)b);
}
} catch (IOException e) {
e.printStackTrace();
}
}
};
new Thread(stdout ).start();
Console console = System.console();
while(true){
String cmd=console.readLine();
session.getStdin().write((cmd+"\n").getBytes());
session.getStdin().flush();
Thread.sleep(1000);