如何在bash脚本中执行sudo时自动输入用户密码?

老邓 2010-09-24 07:24:46
例如:
./configure --prefix=/usr --with-contrib-plugins=all,-byogames,-Cccc,-cbkoders,-codesnippets,-copystrings,-envvars,-headerfixup,-libfinder,-NassiShneiderman,-exporter
make -j2
sudo make install
read -n1 -p "Press any key to continue..."

其中有一条指令为:sudo make install
那么,在执行这一指令时是需要输入用户密码的,这里就避免不了存在交互。
如何实现此时自动输入密码?
或者让只在这一次sudo无需输入密码?
...全文
14368 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
wqc02 2013-04-22
  • 打赏
  • 举报
回复
在shell脚本中需要用root用的来执行指令: sudo 自动输入密码 echo "password" | sudo -S netstat -tlnp -S The -S (stdin) option causes sudo to read the password from the standard input instead of the terminal device.
老邓 2010-09-25
  • 打赏
  • 举报
回复
谢谢主席!
测试通过,build.sh内容:
./configure --prefix=/usr --with-contrib-plugins=all,-byogames,-Cccc,-cbkoders,-codesnippets,-copystrings,-envvars,-headerfixup,-libfinder,-NassiShneiderman,-exporter
make -j2
./install.expect
read -n1 -p "Press any key to continue..."

install.expect内容:
#!/usr/bin/expect
set timeout 60
spawn sudo make install
expect "Password:"
send "fei\r"
interact
steptodream 2010-09-25
  • 打赏
  • 举报
回复
make.ep

#!/usr/bin/expect
set timeout 3600
spawn sudo make install
expect "Password:"
send "fei\r"
interact


#chmod +x make.ep

make.sh

#!/bin/sh
./configure --prefix=/usr --with-contrib-plugins=all,-byogames,-Cccc,-cbkoders,-codesnippets,-copystrings,-envvars,-headerfixup,-libfinder,-NassiShneiderman,-exporter
make -j2
./make.ep
steptodream 2010-09-25
  • 打赏
  • 举报
回复
在shell脚本里可以调用expect脚本啊 expect脚本结束后 shell脚本会继续执行
老邓 2010-09-25
  • 打赏
  • 举报
回复
不过,我最开始要实现的功能仍然无法解决。
因为无法实现bash脚本与expect脚本的结合使用。
这个脚本会失败:
#!/usr/bin/expect
set timeout 3600
./configure --prefix=/usr --with-contrib-plugins=all,-byogames,-Cccc,-cbkoders,-codesnippets,-copystrings,-envvars,-headerfixup,-libfinder,-NassiShneiderman,-exporter
make -j2
spawn sudo make install
expect "Password:"
send "fei\r"
interact
老邓 2010-09-25
  • 打赏
  • 举报
回复
这个脚本搞定了pacman下的自动更新:
#!/usr/bin/expect
set timeout 60
spawn sudo pacman -Syu
expect "Password:"
send "fei\r"
set timeout 60
expect "Proceed with installation?"
send "Y\r"
interact
老邓 2010-09-25
  • 打赏
  • 举报
回复
谢谢,帖点更详细的expect用法指南:

使用expect实现自动登录的脚本,网上有很多,可是都没有一个明白的说明,初学者一般都是照抄、收藏。可是为什么要这么写却不知其然。本文用一个最短的例子说明脚本的原理。
  脚本代码如下:
  ##############################################
  #!/usr/bin/expect
  set timeout 30
  spawn ssh -l username 192.168.1.1
  expect "password:"
  send "ispass\r"
  interact
  ##############################################
  1. [#!/usr/bin/expect]
  这一行告诉操作系统脚本里的代码使用那一个shell来执行。这里的expect其实和linux下的bash、windows下的cmd是一类东西。
  注意:这一行需要在脚本的第一行。
  2. [set timeout 30]
  基本上认识英文的都知道这是设置超时时间的,现在你只要记住他的计时单位是:秒
  3. [spawn ssh -l username 192.168.1.1]
  spawn是进入expect环境后才可以执行的expect内部命令,如果没有装expect或者直接在默认的SHELL下执行是找不到spawn命令的。所以不要用 “which spawn“之类的命令去找spawn命令。好比windows里的dir就是一个内部命令,这个命令由shell自带,你无法找到一个dir.com 或 dir.exe 的可执行文件。
  它主要的功能是给ssh运行进程加个壳,用来传递交互指令。
  4. [expect "password:"]
  这里的expect也是expect的一个内部命令,有点晕吧,expect的shell命令和内部命令是一样的,但不是一个功能,习惯就好了。这个命令的意思是判断上次输出结果里是否包含“password:”的字符串,如果有则立即返回,否则就等待一段时间后返回,这里等待时长就是前面设置的30秒
  5. [send "ispass\r"]
  这里就是执行交互动作,与手工输入密码的动作等效。
  温馨提示: 命令字符串结尾别忘记加上“\r”,如果出现异常等待的状态可以核查一下。
  6. [interact]
  执行完成后保持交互状态,把控制权交给控制台,这个时候就可以手工操作了。如果没有这一句登录完成后会退出,而不是留在远程终端上。如果你只是登录过去执行
  #!/usr/bin/expect #注意安装的路径,不确定 whereis expect 一下
  # Change a login shell to bash
  set user [lindex $argv 0]
  spawn bash $user
  expect "]:"
  send "/bin/bash "
  expect eof
  exit
wind_199 2010-09-25
  • 打赏
  • 举报
回复
sudo make install<<EOF
your-password
EOF
louyong0571 2010-09-24
  • 打赏
  • 举报
回复
就听说过expect,还没用过,长见识了
freetstar 2010-09-24
  • 打赏
  • 举报
回复
貌似有个方法是使用expect。。
老邓 2010-09-24
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 steptodream 的回复:]

引用 4 楼 loaden 的回复:
还是访问密码

不知道哪里写的还有问题?

按我五楼说的来
修改/etc/sudoers 追加
your-user ALL=NOPASSWD:/path/make

这样your-user在执行/path/make的时候就不用密码了
[/Quote]
这个方法我倒是知道的。
我是想知道 就 这 一 次 不 需要密码。
其他的还是需要密码的。
steptodream 2010-09-24
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 loaden 的回复:]
还是访问密码

不知道哪里写的还有问题?
[/Quote]
按我五楼说的来
修改/etc/sudoers 追加
your-user ALL=NOPASSWD:/path/make

这样your-user在执行/path/make的时候就不用密码了
steptodream 2010-09-24
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 masmaster 的回复:]

visudo
[/Quote]
对头 修改/etc/sudoers

your-user ALL=NOPASSWD:/path/make
老邓 2010-09-24
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 steptodream 的回复:]

sudo make install<<EOF
your-password
EOF
[/Quote]
还是访问密码。
sudo pacman -Syu<<EOF
fei
EOF
read -n1 -p "Press any key to continue..."

不知道哪里写的还有问题?
steptodream 2010-09-24
  • 打赏
  • 举报
回复
sudo make install<<EOF
your-password
EOF
masmaster 2010-09-24
  • 打赏
  • 举报
回复
visudo
DIE654456 2010-09-24
  • 打赏
  • 举报
回复
echo "你的密码" |sudo make install
这样?

19,611

社区成员

发帖
与我相关
我的任务
社区描述
系统使用、管理、维护问题。可以是Ubuntu, Fedora, Unix等等
社区管理员
  • 系统维护与使用区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧