简体   繁体   English

如何在不使用Runtime.getRuntime()。exec()重写sudo的情况下运行sudo命令

[英]How to run sudo command without rewriting sudo with Runtime.getRuntime().exec()

Is it possible to make a terminal in java where a user can make a sudo command without rewriting sudo as gksudo or doing anything else like in a basic terminal. 是否有可能在Java中创建一个终端,使用户可以在不使用sudo的形式将sudo重写为gksudo的情况下执行sudo命令或在基本终端中进行其他操作。 I don't want to rewrite the command because I don't like to change the command a user executes. 我不想重写命令,因为我不想更改用户执行的命令。 I know that sudo password must be presented thought the keyboard, but can u do that with java or is it impossible. 我知道必须在键盘上输入sudo密码,但是您可以使用Java来做到这一点吗?还是不可能。 I don't have an example because I don't want to start on a program that don't work, but I did some research but all the examples is about running sudo directly from java without asking the user for the password. 我没有示例,因为我不想在无法运行的程序上启动,但是我进行了一些研究,但是所有示例都是关于直接从Java运行sudo而不要求用户输入密码。

Without System.loadLibary() no. 如果没有System.loadLibary(),则不会。 Termial has special function, for example set colors, set cursor position (used in apt-get install). Termial具有特殊功能,例如设置颜色,设置光标位置(在apt-get install中使用)。 But you can use this: 但是您可以使用以下命令:

package me.barwnikk.library.linuxcommandroot;

import java.awt.BorderLayout;
import java.io.IOException;
import java.io.InputStream;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;

public class LinuxCommand {
    static InputStream is;
    static byte[] buff = new byte[8192];
    static int n;
    public static String getPasswdForRoot() throws IOException {
        Process p = Runtime.getRuntime().exec(new String[]{"sh","-c","sudo -S id"});
        is = p.getErrorStream();
        n = is.read(buff, 0, 8192);
        String text = new String(buff,0,n);
        if(text.contains("root"))return null; //not set password
        JPanel panel = new JPanel(new BorderLayout());
        JLabel lab = new JLabel(text);
        panel.add(lab,BorderLayout.NORTH);
        JPasswordField password = new JPasswordField();
        panel.add(password,BorderLayout.SOUTH);
        JOptionPane.showMessageDialog(null, panel);
        byte[] passwd = (new String(password.getPassword())+"\r\n").getBytes();
        p.getOutputStream().write(passwd);
        p.getOutputStream().flush();
        n = is.read(buff, 0, 8192);
        if(n==-1) return new String(password.getPassword());
        text = new String(buff,0,n);
        while(true) {
            lab.setText(text);
            JOptionPane.showMessageDialog(null, panel);
            p = Runtime.getRuntime().exec(new String[]{"sh","-c","sudo -S id"});
            is = p.getErrorStream();
            n = is.read(buff, 0, 8192);
            passwd = (new String(password.getPassword())+"\n").getBytes();
            p.getOutputStream().write(passwd);
            p.getOutputStream().flush();
            n = is.read(buff, 0, 8192);
            if(n==-1) return new String(password.getPassword());
            text = new String(buff,0,n);
        }
    }
    public static Process runFromRoot(String command, String password) throws IOException {
        byte[] passwd = (password+"\n").getBytes(); //for OutputStream better is byte[]
        Process p = Runtime.getRuntime().exec(new String[]{"sh","-c","sudo -S "+command});
        p.getOutputStream().write(passwd);
        p.getOutputStream().flush();
        return p;
    }
}

It's a mini api to get root password (user must write correct). 这是一个用于获取root密码的迷你api(用户必须输入正确的密码)。 Sample of usage: 用法样本:

public static void main(String[] args) throws IOException, InterruptedException {
    String password = LinuxCommand.getPasswdForRoot();
    System.out.println("stdout of 'id':");
    Process p = LinuxCommand.runFromRoot("id",password);
    System.out.print(streamToString(p.getInputStream()));
    System.out.println("stdout of 'fdisk -l':");
    p = LinuxCommand.runFromRoot("fdisk -l",password);
    System.out.print(streamToString(p.getInputStream()));
}

Method streamToString: 方法streamToString:

public static String streamToString(InputStream stream) {
    String read = "";
    try {
        while((n=stream.read(buff, 0, 8192))!=-1) {
            read+=new String(buff,0,n);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return read;
}

Sample return in my test (in polish): 我的测试中的样品退货(波兰语):

stdout of 'id':
uid=0(root) gid=0(root) grupy=0(root)
stdout of 'fdisk -l':

Disk /dev/sda: 640.1 GB, 640135028736 bytes
głowic: 255, sektorów/ścieżkę: 63, cylindrów: 77825, w sumie sektorów: 1250263728
Jednostka = sektorów, czyli 1 * 512 = 512 bajtów
Rozmiar sektora (logiczny/fizyczny) w bajtach: 512 / 4096
Rozmiar we/wy (minimalny/optymalny) w bajtach: 4096 / 4096
Identyfikator dysku: 0xc56b9eef

Urządzenie Rozruch   Początek      Koniec   Bloków   ID  System
/dev/sda1            2048    37064703    18531328   27  Hidden NTFS WinRE
/dev/sda2   *    37064704    37269503      102400    7  HPFS/NTFS/exFAT
/dev/sda3        37269504   456711884   209721190+   7  HPFS/NTFS/exFAT
/dev/sda4       456711946  1250258624   396773339+   f  W95 Rozsz. (LBA)
Partycja 4 nie zaczyna się na granicy bloku fizycznego.
/dev/sda5       456711948   810350729   176819391    7  HPFS/NTFS/exFAT
Partycja 5 nie zaczyna się na granicy bloku fizycznego.
/dev/sda6       810350793   862802954    26226081    7  HPFS/NTFS/exFAT
Partycja 6 nie zaczyna się na granicy bloku fizycznego.
/dev/sda7       862803018  1020078408    78637695+  83  Linux
Partycja 7 nie zaczyna się na granicy bloku fizycznego.
/dev/sda8      1020079368  1229791814   104856223+   7  HPFS/NTFS/exFAT
/dev/sda9      1229791878  1250258624    10233373+   7  HPFS/NTFS/exFAT
Partycja 9 nie zaczyna się na granicy bloku fizycznego.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM