JSch:sudo< some命令>有时不起作用 [英] JSch: sudo <some command> sometimes not working

查看:113
本文介绍了JSch:sudo< some命令>有时不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有三台机器,我需要一个一个地ssh到它们,然后对命令执行sudo.

I have three machines and I need to ssh to them one by one, and then sudo a command.

我需要JSch做以下事情:

I need the JSch to do the following things:

ssh <user>@<machine>
sudo <some command>

这是我用来运行JSch的代码块:

This is the code block how I use to run 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服务的状态,而在最后一台机器上却没有.

My problem is that I can use this program successfully to get the status of the eedir service on two of the three machines I have, but not the last one.

我可以使用给定的用户名SSH到远程计算机,然后直接运行此命令:

I can ssh to the remote machine with the given user name and then run this command directly:

sudo /etc/init.d/eedir status

在我的/etc/sudoers中已经配置了ssh中使用的用户名.

The username used in ssh is already configured in my /etc/sudoers.

所以,我的问题是:

  1. 为什么JSch可以在三台机器中的两台上运行该程序,而 休息一个不能吗? sudoers的配置完全相同 这些机器.

  1. Why JSch can run this program on two of the three machines while the rest one cannot? The sudoers' configuration is exactlly the same on these machines.

另一个问题是:为什么在真实计算机上使用sudo 如果配置了当前版本,则无需再次输入密码即可运行 sudoers中的用户,而JSch sudo需要程序中的密码吗?

And another question is: why sudo on the real machine can run without typing the password again if I configured the current user in sudoers, while JSch sudo require the password in the program?

任何方向都非常感谢.如果在当前情况下可能存在一些潜在的错误,欢迎大家指出.

Any directions are really appreciated. If there are some potential errors that I might have under my current circumstances, welcome everybody to point them out.

谢谢.

推荐答案

  1. 从故障机器上的jsch sudo尝试中得到什么输出?要获取错误输出,您可能需要:

  1. what output do you get from the jsch sudo attempt on the failed machine? to get the error output you may need:

(ChannelExec)commandChannel.getErrStream();

ssh服务器可能具有不同的设置.在我的服务器上,您需要pty:

the ssh server probably has different settings. on my server you need pty:

(ChannelExec)commandChannel.setPty(true);
(ChannelExec)commandChannel.setPtyType("VT100");

  • 通常,您只需要在sudo的每个会话中键入一次密码即可.也许在真实机器上,您已经执行过sudo一次,但尚未注销.上面的代码每次都会重新连接,因此需要每次发送密码.

  • generally you only need to type your password once per session for sudo. Maybe on the real machine you had already done sudo once and had not logged out. The above code reconnects each time so it needs to send the password every time.

    这篇关于JSch:sudo&lt; some命令&gt;有时不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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