bash脚本,自动输入sudo的密码

来源:互联网 发布:手机自动升级软件 编辑:程序博客网 时间:2024/05/16 17:24
解决方法: echo + | (管道)

开始使用 

[plain] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. echo "admin" | sudo service tomcat7 stop  

始终提示输入密码

后来查看了下sudo命令的使用

[plain] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. 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.

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

[plain] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. echo "admin" | sudo -S service tomcat7 stop  

OK可以work

PS:网上说Shell 的expect工具,专门用来实现自动交互功能的,由于我的需求挺简单的,暂时用不到,后面有用的话,再做研究。
0 0
原创粉丝点击