【问题标题】:Executing sudo using SSH "exec" channel in JSch在 JSch 中使用 SSH“exec”通道执行 sudo
【发布时间】:2019-02-28 02:35:58
【问题描述】:

我正在使用我在其中传递以下命令的文件:

  1. hostname
  2. pwd
  3. pbrun su - fclaim
  4. whoami
  5. cd ..
  6. pwd

在下面添加 Java 代码:

for (String command1 : commands) {

    Channel channel=session.openChannel("exec");
    ((ChannelExec)channel).setCommand(command1);

    in=channel.getInputStream();
    channel.connect();
    byte[] tmp=new byte[1024];
    while(true){
      while(in.available()>0){
        int i=in.read(tmp, 0, 1024);
        if(i<0)
            break;
        System.out.println(new String(tmp, 0, i));
      }
      if(channel.isClosed()){
        break;
      }
    }
    channel.setInputStream(null);
    channel.disconnect();
}

但是我得到了这个输出:

  1. 一些主机名
  2. /home/imam
  3. 缺少输出
  4. imam
  5. 缺少输出
  6. /home/imam

【问题讨论】:

    标签: java shell ssh jsch


    【解决方案1】:

    您的代码在隔离环境中执行每个命令。所以你的第二个whoami 没有像你希望的那样在pbrun su 内运行。

    pbrun su 执行一个新的 shell。

    要向 shell 提供命令,您可以:

    一般来说,我推荐第一种方法,因为它使用定义更好的 API(命令行参数)。

    【讨论】:

      【解决方案2】:

      我遇到了类似的问题,并在@Martin 的帮助下得到了解决,大量的研发和扫描 SO 链接。这是对我有用的程序。

      public class SSHConn {
      
          static Session session;
          static String suCmds = "su - simba -c \"whoami ; pwd\"";
          static String[] commands = {"whoami", suCmds};
      
          public static void main(String[] args) throws Exception {
              open();
              runCmd(commands);
              close();
          }
      
          public static void runCmd(String[] commands) throws JSchException, IOException {
              for (String cmd : commands) {
                  System.out.println("\nExecuting command: " + cmd);
                  Channel channel = session.openChannel("exec");
                  ((ChannelExec) channel).setCommand(cmd);
                  InputStream in = channel.getInputStream();
                  OutputStream out = channel.getOutputStream();
                  channel.connect();
                  //passing creds only when you switch user
                  if (cmd.startsWith("su -")) {
                      System.out.println("Setting suPasswd now....");
                      out.write((Constants.suPasswd + "\n").getBytes());
                      out.flush();
                      System.out.println("Flushed suPasswd to cli...");
                  }
                  captureCmdOutput(in, channel);
                  channel.setInputStream(null);
                  channel.disconnect();
              }
          }
      
          public static void captureCmdOutput(InputStream in, Channel channel) throws IOException {
              System.out.println("Capturing cmdOutput now...");
              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()) {
                      break;
                  }
                  try {
                      Thread.sleep(1000);
                  } catch (Exception ee) {
                      System.out.println(ee.getMessage());
                  }
              }
          }
      
          public static void open() throws JSchException {
              JSch jSch = new JSch();
              session = jSch.getSession(Constants.userId, Constants.host, 22);
              Properties config = new Properties();
              config.put("StrictHostKeyChecking", "no");
              session.setConfig(config);
              session.setPassword(Constants.userPasswd);
              System.out.println("Connecting SSH to " + Constants.host + " - Please wait for few seconds... ");
              session.connect();
              System.out.println("Connected!\n");
          }
      
          public static void close() {
              session.disconnect();
              System.out.println("\nDisconnected channel and session");
          }
      
      }
      

      输出:

      Connecting SSH to my-unix-box.net - Please wait for few seconds... 
      Connected!
      
      Executing command: whoami
      Capturing cmdOutput now...
      john
      
      Executing command: su - simba -c "whoami ; pwd"
      Setting suPasswd now....
      Flushed suPasswd to cli...
      Capturing cmdOutput now...
      simba
      /home/simba
      
      Disconnected channel and session
      

      【讨论】:

      • 你为我节省了大量的工作,谢谢分享! :)))
      猜你喜欢
      • 2013-10-14
      • 2019-10-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-25
      • 1970-01-01
      • 1970-01-01
      • 2015-05-05
      相关资源
      最近更新 更多