Java 使用JSch sudo示例和Channel.setPty在远程主机上运行sudo命令

Java 使用JSch sudo示例和Channel.setPty在远程主机上运行sudo命令,java,ssh,amazon-ec2,sudo,jsch,Java,Ssh,Amazon Ec2,Sudo,Jsch,我在以下链接中使用了JSch Sudo示例: 对它做了一点修改,去掉了所有对话框,因为我必须使用PuTTY将其用于EC2实例 现在,我的代码如下所示: import com.jcraft.jsch.*; import java.awt.*; import javax.swing.*; import java.io.*; public class sudo{ public static void main(String[] arg){ try{ JSch jsch=ne

我在以下链接中使用了JSch Sudo示例:

对它做了一点修改,去掉了所有对话框,因为我必须使用PuTTY将其用于EC2实例

现在,我的代码如下所示:

import com.jcraft.jsch.*;
import java.awt.*;
import javax.swing.*;
import java.io.*;

public class sudo{
  public static void main(String[] arg){
    try{
      JSch jsch=new JSch();

      String host=null;
      if(arg.length>0){
        host=arg[0];
      }

      String privateKey = "my private key.pem";

      jsch.addIdentity(privateKey, "");
      Session session=jsch.getSession("ec2-user", "xx.xx.xx.xx", 22);

      session.setPassword("");
      java.util.Properties config = new java.util.Properties();
      config.put("StrictHostKeyChecking", "no");
      session.setConfig(config);

      session.connect();

      String command="sudo mkdir /data";
      String sudo_pass="";

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

      // man sudo
      // -S The -S (stdin) option causes sudo to read the password from the
      // standard input instead of the terminal device.
      // -p The -p (prompt) option allows you to override the default
      // password prompt and use a custom one.
      ((ChannelExec)channel).setCommand("sudo -S -p '' "+command);

      InputStream in=channel.getInputStream();
      OutputStream out=channel.getOutputStream();
      ((ChannelExec)channel).setErrStream(System.err);

      channel.connect();

      out.write((sudo_pass+"\n").getBytes());
      out.flush();

      byte[] tmp=new byte[1024];
      while(true){
        while(in.available()>0){
          int i=in.read(tmp, 0, 1024);
          if(i<0)break;
          System.out.print(new String(tmp, 0, i));
        }
        if(channel.isClosed()){
          System.out.println("exit-status: "+channel.getExitStatus());
          break;
        }
        try{Thread.sleep(1000);}catch(Exception ee){}
      }
      channel.disconnect();
      session.disconnect();
    }
    catch(Exception e){
      System.out.println(e);
    }
  }
}
然后,我也不能从命令行ssh远程主机


有人能告诉我,在远程主机上运行
sudo
命令需要做什么。

我在一个项目中有类似的代码,并且得到了相同的错误。我像您一样使用
setPty(true)
解决了这个问题

我认为您得到这个错误是因为您没有关闭代码中的流。如果您使用的是Java 1.7,则可以按如下方式使用资源块:

try( InputStream in=channel.getInputStream() ) {
    try( OutputStream out = channel.getOutputStream() ) {
    ...
    }
}
或者尝试…最终阻止以前版本的模式。

设置后

((ChannelExec) channel).setPty(true)

我的代码中报告的问题已得到解决。

默认情况下,SUDO配置为需要TTY。也就是说,SUDO应该从登录shell运行

这可能是因为/etc/sudoers文件(或其包含的任何文件)具有:

但是sudo中的选项-S将使其从标准输入读取

-S    The -S (stdin) option causes sudo to read the password from
      the standard input instead of the terminal device.  The pass-
      word must be followed by a newline character.
Jsch利用该漏洞并尝试发送密码。如果希望Jsch在不提示密码的情况下工作,则需要使用visudo命令从sudoers文件中禁用requirety…如下所示

Defaults !requiretty


我得到了相同的错误,我应该设置setPty(真)还是setPty(假)?一些人建议使用false来解决此问题。这两种选择似乎都不能解决这个问题。谁能帮我一下吗?
Defaults !requiretty
#Defaults requiretty