【问题标题】:Executing the bash commands as a root user using jsch - java使用 jsch - java 以 root 用户身份执行 bash 命令
【发布时间】:2019-12-20 00:16:34
【问题描述】:

我有一个场景,一些命令需要以 root 用户身份执行(在执行 $ sudo su 而不是 sudo $ cmd 之后)。同样,我无法在 jsch 上进行操作。有人可以提供一种在以 root 身份登录后执行某些命令的方法。或者任何等效的库也可以。

鉴于我正在尝试的代码示例和操作是

tail -0f /var/log/xx/xx/original.log > /var/log/xx/xx/copy.txt

public static String runCommandAsrootUser(String user, String password, String host, String command) {
    Properties config = new Properties();
    config.put("StrictHostKeyChecking", "no");
    JSch jsch = new JSch();
    Session session;
    try {
        session = jsch.getSession(user, host, 22);
        session.setPassword(password);
        session.setConfig(config);
        session.connect();
        System.out.println("Connected to " + host);
        Channel channel = session.openChannel("exec");

        channel.setInputStream(null);
        OutputStream out = channel.getOutputStream();
        ((ChannelExec) channel).setErrStream(System.err);
        InputStream in = channel.getInputStream();
        ((ChannelExec) channel).setPty(true);

        ((ChannelExec) channel).setCommand("sudo su -c "+ command);

        channel.connect();

        out.write((password + "\n").getBytes());
        out.flush();
        System.out.println("Completed");

        byte[] tmp = new byte[1024];
        int count = 0;
       while(true) {
           count++;
            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;
            }
        }
       System.out.println("Count: "+count);
        channel.disconnect();
        session.disconnect();
        System.out.println("DONE");
    } catch (Exception e) {
        e.printStackTrace();
    }

    return "";
}

输出显示 /var/log/xx/xx/copy.txt: Permission denied

第二个代码示例

public static void runCommands(String username, String password, String ip, String command){
try {
    JSch jsch = new JSch();
    Session session = jsch.getSession(username, ip, 22);
    session.setPassword(password);
    setUpHostKey(session);
    session.connect();

    Channel channel=session.openChannel("shell");//only shell  
    channel.setOutputStream(System.out); 
    PrintStream shellStream = new PrintStream(channel.getOutputStream());  // printStream for convenience 
    channel.connect(); 
    shellStream.println("sudo su");  // Successfully executed
    shellStream.flush();
    Thread.sleep(5000);
    shellStream.println("ciscotxbu"); // Successfully executed
    shellStream.flush();
    Thread.sleep(5000);
    shellStream.println(command);  // ---> Not executed on the root shell.
    shellStream.flush();

    channel.disconnect();
    session.disconnect();
} catch (Exception e) { 
    System.err.println("ERROR: Connecting via shell to "+ ip);
    e.printStackTrace();
}

}

【问题讨论】:

  • edit您的问题包含您程序中的相关代码,即使它不起作用。

标签: java android selenium jsch jcabi


【解决方案1】:

即使它不是太复杂,通过在 Android 中自己运行 shell 命令,在大多数情况下,我们使用一个库,Libsu 对我们来说工作得很好。你应该考虑使用它,在root下调用没有问题。

另一方面,我建议在另一个库 RxShell 中尝试使用 Rx 操作进行相同的实现(因为它非常简单)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-02-10
    • 1970-01-01
    • 2017-11-05
    • 1970-01-01
    • 1970-01-01
    • 2019-01-12
    • 1970-01-01
    相关资源
    最近更新 更多