expect

当我们需要通过命令行登录多个设备时,通常我们会选择 ssh秘钥ansiable 登录expect实现了类似的功能。其主要实现命令的自动应答

exp_continue 保持终端不断开

#!/bin/bash
#Usage:To get message of H3C's Version
#create at 20190826

rm -f version.txt err.txt
exec &>./version.txt
[ -f /etc/init.d/functions ] && source /etc/init.d/functions
exp(){
/usr/bin/expect<<EOF
               spawn ssh admin@$n
                expect {
                        "yes/no)?" {send "yes\r";exp_continue}
                        "password:" {send "passwd\r";exp_continue}
                        "gain" {break}
                        }
                expect {
                        ">" {send "display version \r\r"}
                        }
                expect {
                        return
                        }
EOF
}

for n in `cat iplist`
do
exp &>/dev/null
if [ $? -ne 0 ];then
action "$n is unknown" /bin/false
else
#168.179.23.233  <A018>display   MSR900
exp|awk -v n=$n 'BEGIN{printf n"\t"}; /display/{printf $1 "\t"};/MSR/{printf $2 "\n"}'|sed 's/<//;s/>//;s/display//' 
#exp|awk -v n=$n 'BEGIN{printf "%6s\t" IP shop version}; /display/{printf $1 "\t"};/MSR/{printf $2 "\n"}'|sed 's/<//;s/>//;s/display//' 
fi
done
上一篇:ssh自动登录实现方法


下一篇:Postman_Test代码片段整理