Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/342.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 无法使用jsch执行sftp子命令_Java_Linux_Sftp_Jsch - Fatal编程技术网

Java 无法使用jsch执行sftp子命令

Java 无法使用jsch执行sftp子命令,java,linux,sftp,jsch,Java,Linux,Sftp,Jsch,这是我的代码,它登录到sftp服务器并执行命令“dzdo su-ibmusr”命令,更改和文件夹的路径并执行ls命令。这是密码 public class Sudo{ public static void main(String[] arg) throws Exception{ int port=22; String name ="john"; String ip ="xxxx"; String password ="root";

这是我的代码,它登录到sftp服务器并执行命令“dzdo su-ibmusr”命令,更改和文件夹的路径并执行ls命令。这是密码

public class Sudo{
  public static void main(String[] arg) throws Exception{

      int port=22;
      String name ="john";
      String ip ="xxxx";
      String password ="root";

      JSch jsch = new JSch();
      Session session = jsch.getSession(name, ip, 22);
      session.setPassword(password);
      session.setConfig("StrictHostKeyChecking", "no");
      System.out.println("Establishing Connection...");
      session.connect();
      System.out.println("Connection established.");

      ChannelExec channelExec = (ChannelExec)session.openChannel("exec");

      InputStream in = channelExec.getInputStream();


      channelExec.setCommand("dzdo su - john");
      OutputStream out = channelExec.getOutputStream();
      out.write(("cd /xx.yy/zz \n").getBytes());

      out.write(("ls \n").getBytes());
      out.flush();
      channelExec.connect();

      BufferedReader reader = new BufferedReader(new InputStreamReader(in));
      String line;
      int index = 0;
      StringBuilder sb = new StringBuilder();
      while ((line = reader.readLine()) != null)
      {
          System.out.println(line);
      }
      session.disconnect();
  }
}
我越来越不正常了

Exception in thread "main" java.io.IOException: failed to initialize the channel.
    at com.jcraft.jsch.Channel$1.init(Channel.java:242)
    at com.jcraft.jsch.Channel$1.write(Channel.java:253)
    at java.io.OutputStream.write(OutputStream.java:75)
    at com.consol.citrus.samples.todolist.Sudo.main(Sudo.java:43)
移动线路

channelExec.connect()

在该街区上方:

 OutputStream out = channelExec.getOutputStream();
 out.write(("cd /xx.yy/zz \n").getBytes());
 out.write(("ls \n").getBytes());
 out.flush();

因此,您首先建立连接,然后获取输出流。

您从何处获得错误?在哪个
上写入
?手动执行ssh时是否有效?@RobertKock正好在这一行out.write(((((“cd/xx.yy/zz\n”).getBytes());ssh命令在putty terminalOK中执行时工作正常,我重现了错误。明天我将尝试tro找出发生了什么。谢谢,程序运行正常,但程序没有停止断开频道和会话。正如我所说:“很自然,因为
su
一直在运行。你必须发送
exit
来关闭它。”谢谢,程序运行正常。很乐意帮助!:)