Java Process 創建linux用戶

通過調用shell腳本方式,傳入username和password方式產生用戶


#!/bin/sh
useradd $1;
echo $2|passwd --stdin $1

public static void main(String[] args) {
    InputStream in = null;
    try{
        File file = new File("/");//進入根目錄
        Process pro = Runtime.getRuntime.exec("./test.sh test testpwd", null, file);

        pro.waitFor();
        in = pro.getInputStream();
        BufferedReader read = new BufferedReader(new InputStreamReader(in));
        String result = null;
        while((result = read.readLine()) != null) 
            System.out.println(result);
    } catch (Exception e) {
        e.printStackTrace();
    }
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章