使用JSch sudo示例和Channel.setPty在远程主机上运行sudo命令
我在以下链接中使用了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)
解决了这个问题。
我认为您收到此错误是因为您没有关闭代码中的流。 如果您使用的是Java 1.7,则可以使用资源块,如下所示:
try( InputStream in=channel.getInputStream() ) { try( OutputStream out = channel.getOutputStream() ) { ... } }
或者尝试…来自过去版本的最终块模式。
设定后
((ChannelExec) channel).setPty(true)
报告的问题在我的代码中得到了解决。
默认情况下,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利用相同的function并尝试发送密码。 如果你想让Jsch在没有提示密码的情况下工作,你需要从sudoers文件中禁用requiretty …使用visudo命令如下
Defaults !requiretty
要么
#Defaults requiretty