expect
2019-04-08 本文已影响0人
wanncy
expect
遇到的问题: 需要在 shell 脚本中自动输入密码切换成 root 用户(虽然这不是一种安全的方式)
尝试:
- <<< 重定向加 here document的方式:
command <<<EOF
>xxxxxx
>EOF
这种方式适用于那些不需要提示符的输入,即 command 之后直接跟 stdin;
expect
适用于根据输出提示字符进行对应的输入,这有点像 pwn 里的 exp 的常用方法
#!/usr/bin/expect
set password xxxxxx
set sudoi /xxxxx/batchTouch.sh
spawn sudo -i
set timeout 300
expect "password for xxxxxx:"
send "$password\r"
expect "]# "
send "/bin/bash ${sudoi}\r"
#interact
send "exit\r"
expect eof
exit
这个是常用的在本机登录root,并执行 batTouch.sh 脚本;
另外常用 expect 的方式就是 ssh 的连接
#!/usr/bin/expect
set IP [lindex $argv 0]
set USER [lindex $argv 1]
set PASSWD [lindex $argv 2]
set CMD [lindex $argv 3]
spawn ssh $USER@$IP $CMD
expect {
"(yes/no)?" {
send "yes\r"
expect "password:"
send "$PASSWD\r"
}
"password:" {send "$PASSWD\r"}
"* to host" {exit 1}
}
expect eof