MyShell


import com.jcraft.jsch.*;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;

public class MyShell {
final static String ENDTAG = "!@#@!$@!23432432fdsafds#@$@#";

public static void main(String[] arg) {
String host = "96.126.126.117";
String username = "zgq";
String password = "xxxx";
String[] commandArr = { "ls", "date", "pwd" };
MyShell myShell = new MyShell();
myShell.runCommands(host, username, password, commandArr);
}

public void runCommands(String host, String username, String password,
final String[] commandArr) {
try {
JSch jsch = new JSch();

Session session = jsch.getSession(username, host, 4321);

session.setPassword(password);

UserInfo ui = new MyUserInfo() {
public void showMessage(String message) {
}

public boolean promptYesNo(String message) {
return true;
}
};

session.setUserInfo(ui);

session.connect(30000); // making a connection with timeout.

Channel channel = session.openChannel("shell");

final PipedOutputStream pos = new PipedOutputStream();
PipedInputStream pis = new PipedInputStream();
pos.connect(pis);

channel.setInputStream(pis);
PipedInputStream pis2 = new PipedInputStream();
PipedOutputStream pos2 = new PipedOutputStream(pis2);
channel.setOutputStream(pos2);
final BufferedReader outBuffReader = new BufferedReader(
new InputStreamReader(pis2));

channel.connect(3 * 1000);

Thread inputThread = new Thread(new Runnable() {
public void run() {
try {
for (int i = 0; i < commandArr.length; i++) {
Thread.sleep(5000);
System.out.println("###begin run:" + commandArr[i]);
pos.write((commandArr[i] + "\n").getBytes());
}
pos.write((ENDTAG + "\n").getBytes());
pos.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
pos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
System.out.println("inputThread end");
}
});
inputThread.start();

// channel.setOutputStream(System.out);

Thread outputThread = new Thread(new Runnable() {
public void run() {
String oneLine = null;
try {
while ((oneLine = outBuffReader.readLine()) != null) {
System.out.println("output:" + oneLine);
if (oneLine.contains(ENDTAG)) {
break;
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
outBuffReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
System.out.println("outputThread end");
}
});
outputThread.start();

inputThread.join();
outputThread.join();

channel.disconnect();
session.disconnect();
System.out.println("###end process commands###");
} catch (Exception e) {
e.printStackTrace();
}

}

public static abstract class MyUserInfo implements UserInfo,
UIKeyboardInteractive {
public String getPassword() {
return null;
}

public boolean promptYesNo(String str) {
return false;
}

public String getPassphrase() {
return null;
}

public boolean promptPassphrase(String message) {
return false;
}

public boolean promptPassword(String message) {
return false;
}

public void showMessage(String message) {
}

public String[] promptKeyboardInteractive(String destination,
String name, String instruction, String[] prompt, boolean[] echo) {
return null;
}
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值