Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package test;
- import java.io.InputStream;
- import com.jcraft.jsch.ChannelExec;
- import com.jcraft.jsch.JSch;
- import com.jcraft.jsch.Session;
- import com.jcraft.jsch.UserInfo;
- public class Question
- {
- public static void main(String[] args)
- {
- JSch jsch = new JSch();
- Session session = null;
- try
- {
- String user = "custpeak";
- String host = "172.23.54.253";
- String password = "paas@tju";
- int port = 22;
- session = jsch.getSession(user, host, port);
- UserInfo userInfo = new MyUserInfo(null);
- session.setUserInfo(userInfo);
- session.setPassword(password);
- session.connect();
- ChannelExec channel = (ChannelExec) session.openChannel("exec");
- String command = "cd /home && sh pressure.sh" + "n";
- channel.setCommand(command);
- channel.setInputStream(null);
- InputStream instream = channel.getErrStream();
- byte[] tmp = new byte[1024];
- channel.connect();
- String errOut = "";
- int exitStatus = -1;
- while (true)
- {
- while (instream.available() > 0)
- {
- int i = instream.read(tmp, 0, 1024);
- if (i < 0)
- {
- break;
- }
- String tempOut = new String(tmp, 0, i, "UTF-8");
- errOut += tempOut;
- }
- if (channel.isClosed())
- {
- if (instream.available() > 0)
- {
- continue;
- }
- exitStatus = channel.getExitStatus();
- break;
- }
- }
- System.out.println("exitStatus:" + exitStatus);
- System.out.println("errMsg:" + errOut);
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- }
- if(!session.isConnected())
- {
- break;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement