linux/shell/bash 自动输入密码或文本

linux有些命令需要输入密码,比如ssh或su,又不能通过参数指定,正常只能手动输入。这让人多少有些懊恼,尽管这样很安全!

破解:expect

默认没这个东西,需要安装

 

apt/yum install -y expect

好了,可以使用了,先写个shell脚本

#!/usr/bin/expect#用expect执行下面脚本
spawn su#执行su命令
expect "Password:"#看到这样的文本时
exp_send "123123\r"#输入密码
interact#进入交互状态

vi su后将上面内容复制/输入即可

chmod +x su

添加执行权限

./su

运行,即可进入expect的交互环境。

 

push.sh

#!/usr/bin/expect
spawn git pull --rebase
expect "Username for 'https://github.com':"
exp_send "iqiy\r"
expect "Password for 'https://iqiy@github.com':"
exp_send "xxx\r"
interact

  

posted @ 2019-12-02 15:28  瘋耔  阅读(1464)  评论(0编辑  收藏  举报
跳至侧栏