com.jcraft.jsch.Channel.getInputStream中的java.lang.NullPointerException(Channel.java:222)

时间:2014-05-28 13:38:09

标签: java ssh

我想用com.jcraft.jsch从windows连接到linux控制台。

我有这样的连接类:

import java.io.IOException;
import java.io.InputStream;
import java.util.logging.Level;
import java.util.logging.Logger;

import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;

public class SSHManager {
private static final Logger LOGGER = Logger.getLogger(SSHManager.class.getName());
private JSch jschSSHChannel;
private String strUserName;
private String strConnectionIP;
private int intConnectionPort;
private String strPassword;
private Session sesConnection;
private int intTimeOut;


private void doCommonConstructorActions(String userName, String password, String connectionIP, String knownHostsFileName) {
    jschSSHChannel = new JSch();

    try {
        jschSSHChannel.setKnownHosts(knownHostsFileName);
    } catch (JSchException jschX) {
        logError(jschX.getMessage());
    }

    strUserName = userName;
    strPassword = password;
    strConnectionIP = connectionIP;
}

public SSHManager(String userName, String password, String connectionIP, String knownHostsFileName) {
    doCommonConstructorActions(userName, password, connectionIP, knownHostsFileName);
    intConnectionPort = 22;
    intTimeOut = 60000;
}

public SSHManager(String userName, String password, String connectionIP, String knownHostsFileName, int connectionPort) {
    doCommonConstructorActions(userName, password, connectionIP, knownHostsFileName);
    intConnectionPort = connectionPort;
    intTimeOut = 60000;
}

public SSHManager(String userName, String password, String connectionIP, String knownHostsFileName, int connectionPort, int timeOutMilliseconds) {
    doCommonConstructorActions(userName, password, connectionIP, knownHostsFileName);
    intConnectionPort = connectionPort;
    intTimeOut = timeOutMilliseconds;
}

public String connect() {
    String errorMessage = null;

    try {
        sesConnection = jschSSHChannel.getSession(strUserName, strConnectionIP, intConnectionPort);
        sesConnection.setPassword(strPassword);
        // UNCOMMENT THIS FOR TESTING PURPOSES, BUT DO NOT USE IN PRODUCTION
        java.util.Properties config = new java.util.Properties(); 
        config.put("StrictHostKeyChecking", "no");

        sesConnection.setConfig(config);
        sesConnection.connect(intTimeOut);
    } catch (JSchException jschX) {
        errorMessage = jschX.getMessage();
    }

    return errorMessage;
}

private String logError(String errorMessage) {
    if (errorMessage != null) {
        LOGGER.log(Level.SEVERE, "{0}:{1} - {2}", new Object[] { strConnectionIP, intConnectionPort, errorMessage });
    }

    return errorMessage;
}

private String logWarning(String warnMessage) {
    if (warnMessage != null) {
        LOGGER.log(Level.WARNING, "{0}:{1} - {2}", new Object[] { strConnectionIP, intConnectionPort, warnMessage });
    }

    return warnMessage;
}

public String sendCommand(String command) {
    StringBuilder outputBuffer = new StringBuilder();

    try {
        Channel channel = sesConnection.openChannel("exec");
        ((ChannelExec) channel).setCommand(command);
        channel.connect();
        InputStream commandOutput = channel.getInputStream();//tr
        int readByte = commandOutput.read();

        while (readByte != 0xffffffff) {
            outputBuffer.append((char) readByte);
            readByte = commandOutput.read();
        }

        channel.disconnect();
    } catch (IOException ioX) {
        logWarning(ioX.getMessage());
        return null;
    } catch (JSchException jschX) {
        logWarning(jschX.getMessage());
        return null;
    }

    return outputBuffer.toString();
}

public void close() {
    sesConnection.disconnect();
}

}

我尝试在这样的测试中发送命令(我使用testng):

@Test
  public void testCommand()
  {

     String command1 = "cd /home/bin";
     String userName = "user";
     String password = "passwd";
     String connectionIP = "myip";
     SSHManager instance = new SSHManager(userName, password, connectionIP, "");
     String errorMessage = instance.connect();

     if(errorMessage != null)
     {
        System.out.println(errorMessage);
     }
     instance.sendCommand(command1);
     instance.close();

  }

但我收到了错误:

java.lang.NullPointerException
at com.jcraft.jsch.Channel.getInputStream(Channel.java:222)
at com.tests.util.SSHManager.sendCommand(SSHManager.java:99)
at com.tests.Test.testCommand(Test.java:65)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:84)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:714)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
at org.testng.TestRunner.privateRun(TestRunner.java:767)
at org.testng.TestRunner.run(TestRunner.java:617)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
at org.testng.SuiteRunner.run(SuiteRunner.java:240)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1224)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1149)
at org.testng.TestNG.run(TestNG.java:1057)
at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)

有人可以帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

如何改变?

  Channel channel = sesConnection.openChannel("exec");
  ((ChannelExec) channel).setCommand(command);
  InputStream commandOutput = channel.getInputStream();//tr
  channel.connect();
  //InputStream commandOutput = channel.getInputStream();//tr
  int readByte = commandOutput.read();