bash指令碼,自動輸入sudo的密碼

2021-06-19 22:09:51 字數 599 閱讀 4070

解決方法:echo + | (管道)開始使用 

echo "admin" | sudo service tomcat7 stop

始終提示輸入密碼

後來檢視了下sudo命令的使用

man sudo

發現有如下的解釋:

-s          the -s (stdin) option causes sudo to read the password from

the standard input instead of the terminal device.  the

password must be followed by a newline character.

因此,之前的命令修改如下:

echo "admin" | sudo -s service tomcat7 stop

ok可以work

ps:網上說shell 的expect工具,專門用來實現自動互動功能的,由於我的需求挺簡單的,暫時用不到,後面有用的話,再做研究。

bash指令碼,自動輸入sudo的密碼

解決方法 echo 管道 開始使用 echo admin sudo service tomcat7 stop 始終提示輸入密碼 後來檢視了下sudo命令的使用 man sudo 發現有如下的解釋 s the s stdin option causes sudo to read the passwor...

sudo 提權時自動輸入密碼

嗯哼,辦法是有的。目前找到兩種解決方案,方案一親測有效,方案二未試過,僅做記錄。方案一 有的應用自帶選項,可以直接從標準輸入讀入資料,這時候只需要結合管道就好了。比如sudo的 s引數,以及passwdde stdin引數。舉個例子,使用該方法執行netease cloud music。使用sudo...

shell指令碼 自動輸入密碼

平時在控制台輸入指令如 sudo ssh ftp或者修改admin許可權的檔案時候都會要求輸入password,但是在she ll指令碼執行過程中該如何互動實現自動輸入密碼呢?下面總結三種實現方法。shell用重定向作為標準輸入的用法是 cmd 實現ftp自動登入並執行ls指令的用法如下 其中pet...