java调用sudo命令,执行带有“sudo”命令的linux shell命令。使用java,无需输入所需的密码...

Hi I'm using ganymed-ssh2 java library to execute remote commands in linux workstation with success.

But now there is a situation where I need to execute a command but this require that i put some password...like:

sudo /usr/bin/some_process

please enter your password:

I used to implement remote cmd execution in this way:

Session sess = conn.openSession();

sess.execCommand("uname -a && date && uptime && who");

System.out.println("Here is some information about the remote host:");

InputStream stdout = new StreamGobbler(sess.getStdout());

BufferedReader br = new BufferedReader(new InputStreamReader(stdout));

while (true)

{

String line = br.readLine();

if (line == null)

break;

System.out.println(line);

}

System.out.println("ExitCode: " + sess.getExitStatus());

sess.close();

conn.close();

I'm afraid that it's not possible to execute commands that require a password with this library.

Someone could give me a solution or alternative to allow this ???

Thx!

解决方案

assuming there is a sess.getStdin(), you should be able to pipe in a password to the sudo command.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值