使用sesu访问在远程Linux上运行命令的Java代码

时间:2015-11-10 09:39:27

标签: java ssh

我需要使用我的凭据登录到linux框的java代码,然后执行sesu,然后执行shell脚本。如果仅用于sesu用户,则执行shell脚本的权限,因此在登录后进行sesu-ing是至关重要的。我使用了以下代码,可以帮助我使用我的凭据范围执行命令,但是,我需要在登录后登录sesu。请建议一个方法。 我尝试在teh命令列表中添加sesu命令,但它会提示输入密码。我想要一种方法来传递密码并完全自动化它。

    import java.io.InputStream;
    import com.jcraft.jsch.Channel;
    import com.jcraft.jsch.ChannelExec;
    import com.jcraft.jsch.JSch;
    import com.jcraft.jsch.Session;

    public class SSHCommandExecutor {
        /** * @param args */
        public static void main(String[] args) {
            String host = "xxxxxxx";
            String user = "xxxxxxx";
            String password = "xxxxxxx";
            String command1 = "cd /test; ./test.sh";
            try {
            java.util.Properties config = new java.util.Properties();
            config.put("StrictHostKeyChecking", "no");
            JSch jsch = new JSch();
            Session session = jsch.getSession(user, host, 22);
            session.setPassword(password);
            session.setConfig(config);
            session.connect();
            System.out.println("Connected");
            Channel channel = session.openChannel("exec");
            ((ChannelExec) channel).setCommand(command1);
            channel.setInputStream(null);
            ((ChannelExec) channel).setErrStream(System.err);
            InputStream in = channel.getInputStream();
            channel.connect();
            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();
            System.out.println("DONE");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

变体A:一直使用JSch

这个最小脚本(test.sh)在输出一行数据之前从stdin请求输入:

#!/bin/bash
echo -n "Is this a good question (y/n)? "
read answer
if echo "$answer" | grep -iq "^y" ;then
    echo Yes
else
    echo No
fi

因此它应该等同于您的呼叫请求密码。现在看一下如何将数据发送到该进程的代码

String command1 = "cd /home/jan; ./test.sh";
try {
    java.util.Properties config = new java.util.Properties();
    config.put("StrictHostKeyChecking", "no");
    JSch jsch = new JSch();
    Session session = jsch.getSession(user, host, 22);
    session.setPassword(password);
    session.setConfig(config);
    session.connect();
    System.out.println("Connected");
    ChannelExec channel = (ChannelExec)session.openChannel("exec");
    OutputStream o = channel.getOutputStream();
    PrintWriter pw = new PrintWriter(o);
    InputStream in = channel.getInputStream();
    ((ChannelExec) channel).setCommand(command1);

    channel.connect();

    // 1 - Reading the prompt to input password
    byte[] buf = new byte[255];
    int len = in.read(buf);
    System.out.println(new String(buf,0,len));

    // 2 - Send (password) data and flush stream
    pw.println("y");
    pw.flush();

    // 3 - Read result
    BufferedReader br = new BufferedReader(new InputStreamReader(in));
    System.out.println(br.readLine());

    // 4 - Clean up
    channel.disconnect();
    session.disconnect();

变体B:外壳魔法

String command1 = "cd /test; echo 'password' | ./test.sh";

String command1 = "cd /test; ./test.sh <<< 'password'";

(因为你需要在那里指定正确的密码)

要使用root权限运行远程脚本,即使登录用户没有这些权限,也请在此处查看:

https://askubuntu.com/questions/155791/how-do-i-sudo-a-command-in-a-script-without-being-asked-for-a-password