【问题标题】:Use JSch sudo example and Channel.setPty for running sudo command on remote host使用 JSch sudo 示例和 Channel.setPty 在远程主机上运行 sudo 命令
【发布时间】:2012-08-11 17:15:13
【问题描述】:

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

http://www.jcraft.com/jsch/examples/Sudo.java.html

并对其进行了一些更改并删除了所有对话框,因为我必须将它用于使用 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);
    }
  }
}

但我得到了错误

抱歉,您必须有一个 tty 才能运行 sudo

我也尝试使用((ChannelExec) channel).setPty(true),但我可以运行一次程序,但下一次,对于同一个 EC2 实例,我收到以下错误,但对于新实例,它第一次运行正常。

com.jcraft.jsch.JSchException: Session.connect: java.io.IOException: End of IO Stream Read
sudo mkdir /data
Exception in thread "main" com.jcraft.jsch.JSchException: session is down
        at com.jcraft.jsch.Session.openChannel(Session.java:791)

然后我也无法从命令行 ssh 远程主机。

有人可以指导我在远程主机上运行sudo 命令需要做什么。

【问题讨论】:

  • 我遇到了同样的错误,我应该设置 setPty(true) 还是 setPty(false) ?有些人建议使用 false 来解决这个问题。这两个选项似乎都不能解决这个问题。谁能帮我解决这个问题?

标签: java ssh amazon-ec2 sudo jsch


【解决方案1】:

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

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

try( InputStream in=channel.getInputStream() ) {
    try( OutputStream out = channel.getOutputStream() ) {
    ...
    }
}

或过去版本中的 try...finally 块模式。

【讨论】:

    【解决方案2】:

    设置后

    ((ChannelExec) channel).setPty(true)
    

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

    【讨论】:

      【解决方案3】:

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

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

      Defaults requiretty
      

      但是 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 在不提示密码的情况下工作,你需要从 sudoers 文件中禁用 requiretty...使用 visudo 命令如下

      Defaults !requiretty
      

      #Defaults requiretty
      

      【讨论】:

        猜你喜欢
        • 2017-09-14
        • 2016-09-07
        • 1970-01-01
        • 2019-04-07
        • 1970-01-01
        • 2012-07-29
        • 1970-01-01
        • 2012-12-22
        • 2014-04-30
        相关资源
        最近更新 更多