我想实现下面的效果
ssh [email protected] (密码 admin) 登录后,运行 ls ,然后输入 exit 退出 最后把 ssh 的整个输出取出来
#!/usr/bin/expect -f set timeout 10 spawn ssh [email protected] expect { "*yes/no" { send "yes\n"; exp_continue } "*password:" { send "admin\n" } "# " { puts $expect_out(buffer) send "exit\n" expect eof } }
但是上面的代码好像有点问题,没法实现我想要的效果,请问这个应该怎么写?
1 SpicyCat 2016-03-23 18:54:08 +08:00 ![]() |
3 truehyp 2016-03-24 14:35:34 +08:00 最近东拼西凑出来的一个脚本,还没改完,可以批量操作,所有操作记录都在 log 文件里。打算再改改,用 scp 将所有输出取回本机。 ________________ #!/bin/sh cmdfile=cmd.txt ipfile=ip.txt #run expect function function run() { expect <<-END set timeout 30 set f [open $cmdfile r] spawn ssh -l $2 $1 expect { "yes/no)*" {send "yes\r";exp_continue} "*?assword:*" {send "$3\r";exp_continue} "]#*" {while {[gets \$f line] >= 0} {send "\$line\r"}} } send "exit\r" expect eof spawn scp $1:~/log.txt ./ expect { "yes/no)*" {send "yes\r";exp_continue} "*?assword:*" {send "$3\r";exp_continue} } END return; } for i in `cat $ipfile` do ip=`echo $i | awk -F "|" '{print $1}'` user=`echo $i | awk -F "|" '{print $2}'` passwd=`echo $i| awk -F "|" '{print $3}'` date >> log; run $ip $user $passwd >> log; done |