Passwords jsch sudo不需要密码
我要运行以下命令: sshsudouser@host1 sudo mkdir aaa sudoUser可以在不输入密码的情况下运行sudo命令。我参考了以下链接:,和。我写了剧本。但在我运行下面的代码之后,它总是挂在那里 那么,我如何处理没有密码的sudo场景呢Passwords jsch sudo不需要密码,passwords,sudo,jsch,Passwords,Sudo,Jsch,我要运行以下命令: sshsudouser@host1 sudo mkdir aaa sudoUser可以在不输入密码的情况下运行sudo命令。我参考了以下链接:,和。我写了剧本。但在我运行下面的代码之后,它总是挂在那里 那么,我如何处理没有密码的sudo场景呢 import com.jcraft.jsch.*; import java.io.*; public class sudo{ public static void main(String[] arg){ try{
import com.jcraft.jsch.*;
import java.io.*;
public class sudo{
public static void main(String[] arg){
try{
JSch jsch=new JSch();
String host="hostip";
Session session=jsch.getSession("user", host, 22);
session.setPassword("pass");
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
String command="mkdir try";
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);
((ChannelExec) channel).setPty(true);
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);
}
}
}
import com.jcraft.jsch.*;
导入java.io.*;
公共级sudo{
公共静态void main(字符串[]arg){
试一试{
JSch JSch=新的JSch();
字符串host=“hostip”;
Session Session=jsch.getSession(“用户”,主机,22);
session.setPassword(“pass”);
java.util.Properties config=new java.util.Properties();
配置放置(“检查”、“否”);
session.setConfig(config);
session.connect();
String命令=“mkdir try”;
字符串sudo_pass=null;
sudo_pass=“”;
Channel=session.openChannel(“exec”);
//曼苏多
//-S-S(stdin)选项使sudo从
//标准输入,而不是终端设备。
//-p-p(提示)选项允许您覆盖默认值
//密码提示,并使用自定义密码提示。
setCommand(“sudo-S-p”“”+命令);
((ChannelExec)channel).setPty(true);
InputStream in=channel.getInputStream();
OutputStream out=channel.getOutputStream();
((ChannelExec)channel.setErrStream(System.err);
channel.connect();
out.write((sudo_pass+“\n”).getBytes();
out.flush();
字节[]tmp=新字节[1024];
while(true){
while(in.available()>0){
inti=in.read(tmp,0,1024);
如果(i在这条线上
((ChannelExec)channel).setCommand("sudo -S -p '' "+command);
您正在设置-S
和-p
选项,这可能会导致它挂起,因为它正在等待密码输入。我的问题已解决。以下是我的发现
在/etc/sudoers
中,如果注释了Defaults requirety
,我们不能使用((ChannelExec)channel).setPty(true);
,否则它将挂在那里。如果requirety已启用,我们需要将pty设置为true
如果您的sudoer不需要密码就可以切换到root,您仍然需要给sudoer密码才能运行((ChannelExec)channel).setCommand(“sudo-S-p”“”+命令);
3.如果您多次更改了/etc/sudoers
文件,最好重新启动主机,那么在您多次更改文件后,主机可能会感到困惑。您的远程sudoers文件是否设置了requirety
设置?可能需要将其关闭requirety已注释。在文件/etc/sudoers:#requiretty尝试为该命令或用户删除它…或使用!requiretty
我更改为默认值!requiretty。它仍然挂在那里。我发现,如果我不添加((ChannelExec)channel.setPty(true);
,它将不会挂在那里。相反,它将很快返回xit状态:1
。如果我只输入((ChannelExec)channel).setCommand(“sudo”+命令),它将有一个“密码”提示。