我有三臺機器,我需要給他們一個接一個,然後sudo一個命令。JSch:sudo <某些命令>有時不能正常工作
我需要的JSch做以下的事情:
ssh <user>@<machine>
sudo <some command>
這是代碼塊我如何使用運行JSch:
String homeFolder = System.getProperty("user.home");
JSch jsch = new JSch();
jsch.addIdentity(homeFolder + "/.ssh/id_rsa.pub");
jsch.setKnowHosts(homeFolder + "/.ssh/known_hosts");
final Session session = jsch.getSession(user, machine);
session.connect();
StringBuilder response = new StringBuilder();
try {
final Channel channel = session.openChannel("exec");
((ChannelExec) channel).setCommand("sudo /etc/init.d/eedir status")
channel.connect();
try {
InputStream in = channel.getInputStream();
OutputStream out = channel.getOutputStream();
out.write((password + System.getProperty("line.separator")).getBytes());
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
String line = null;
while (null != (line = reader.readLine())) {
System.out.println(line);
}
} finally {
channel.disconnect();
}
} finally {
session.disconnect();
}
我的問題是,我可以成功地使用這個程序在我擁有的三臺機器中的兩臺上獲得eedir服務的狀態,但不是最後一臺。
我可以ssh到遠程機器用給定的用戶名,然後直接運行下面的命令:
sudo /etc/init.d/eedir status
在SSH中使用的用戶名在我/etc/sudoers
已經配置。
所以,我的問題是:
爲什麼JSch可以在兩個三臺機的運行這個程序,而其餘 一個不能?這些機器上的sudoers配置完全相同。
而另一個問題是:爲什麼真機 上,sudo命令可以無需再次輸入密碼,如果我配置的當前 用戶在sudoers中運行,而JSch須藤需要在程序中的密碼?
任何方向真的很感激。如果在我目前的情況下可能存在一些潛在的錯誤,歡迎大家指出。
謝謝。
我從Ubuntu切換到CentOS時出現此問題。這個答案確實有幫助。 – Burt 2017-11-30 07:12:34