自動輸入密碼使用root許可權開啟shell指令碼

dbdxnuliba發表於2020-12-03

不管用那種方法sudo 後買年都有用到引數-S,這個引數是讓sudo從標準輸入流讀取而不是終端裝置.

1.使用echo和管道命令

#!/bin/bash
echo password | sudo -S service runtime* status
#!/usr/bin/env python
#-*- coding: utf-8 -*-

from Tkinter import *
import subprocess
import sys

def start_shell():

subprocess.call("gnome-terminal -x bash -ic 'cd /home/xxx/ws_proj/src/deploy/; echo '123' | sudo -S ./start_all.sh;'", shell=True)  #注意密碼為123

if __name__ == "__main__":
    root.geometry("800x4000")
    root.title("system")
    Label(root, text="deploy", width = 10, height = 3, font=("blue", 10)).pack()
    new_fm = Frame(root)
    fm1 = Frame(new_fm)
    Label(fm1, text="version1", width = 10, height = 3, font=("blue", 10)).pack()
    start_shell = Button(fm1, text = "shell開啟", width = 20, height = 4, command = start_shell)
    fm1.pack(side=LEFT, fill=BOTH, expand=YES)
    start_shell.pack()

2使用文字塊重定向

#!/bin/bash
sudo -S service talend-runtime* status<<EOF
password
EOF

 

相關文章