簡體   English   中英

使用Jsch以root用戶身份執行bash命令-Java

[英]Executing the bash commands as a root user using jsch - java

我有一個場景,其中一些命令需要以root用戶身份執行(執行$ sudo su而不是sudo $ cmd之后)。 同樣,我無法在jsch上進行操作。 有人可以提供一種以root用戶身份登錄后執行某些命令的方法嗎? 或任何等效的庫也可以。

給定我正在嘗試的代碼示例,操作是

尾-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:權限被拒絕

第二個代碼樣本

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();
}

}

即使不是太復雜,通過在Android中自己運行shell命令,在大多數情況下,我們為此使用一個庫,因此Libsu可以很好地工作。 您應該考慮使用它,並且在root下調用沒有問題。

另一方面,我建議嘗試在另一個庫RxShell中使用Rx操作實現相同的實現(因為它很簡單)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM