使用Jsch sudo的例子,channel.setpty的远程主机上运行sudo命令 [英] use of Jsch sudo example and channel.setpty for running sudo command on remote host

查看:3268
本文介绍了使用Jsch sudo的例子,channel.setpty的远程主机上运行sudo命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用下面的链接下Jsch须藤例如:

I have used Jsch sudo example under following link:

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

和改变了一点点,然后所有的对话框中摆脱,因为我必须使用它使用腻子EC2实例。

and changed it a bit and get rid of all the dialogs as I have to use it for EC2 instances using putty.

现在我的code是这样的:

Now my code looks like this:

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="mkdir /data";
      String command="sudo mkdir /data";
      String sudo_pass=null;

      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)信道).setPty(真),但我可以运行在下一次程序一次,但,对于相同的EC2实例,我得到下面的错误,但对于新的实例,它工作正常的第一次了。

I also tried to use ((ChannelExec) channel).setPty(true) but I can run the program once but next time, for same EC2 instance, I get the following error but for new instance it works fine for first time again.

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命令。

And then also I could not ssh the remote host from command line as well.
Can someone please guide me that what I need to do to run sudo command on remote host.

推荐答案

设置完成后

((ChannelExec) channel).setPty(true)

报告的问题在我的code得到有效解决。

the reported problem have in my code got solved.

这篇关于使用Jsch sudo的例子,channel.setpty的远程主机上运行sudo命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆