代码改变世界

expect ssh 上多个机器查询信息

2019-08-23 14:16  Loull  阅读(345)  评论(0)    收藏  举报
#/bin/shell

set ip [lindex $argv 0]

set timeout 2
set PW "xxx"
spawn ssh user@$ip
expect {
    "yes" {
           send "yes\n"
           expect  "password*:"
           send "$PW\r"
        }
    "password*:"
    {
        send "$PW\r";
        exp_continue
    }
    "]"
}
# send " ls /home/admin/logs/antdnsfilter/ \r"
send " tail -100 /home/admin/logs/antdnsfilter/error.log\n"

expect eof
exit

以上是listip脚本,

 

下面的脚本 执行expect listip $ip 查看每个机器的信息。

#/bin/shell

ip_array=("100.88.9.135" "100.88.8.195" "100.88.8.73" "100.88.9.106" "100.88.11.146" "100.88.10.110" "100.88.8.151" "100.88.8.190" "100.88.8.163" "100.88.10.88" "100.88.11.49" "100.88.9.76" "100.88.9.161" "100.88.8.196" "100.88.10.148" "100.88.11.147" "100.88.8.52" "100.88.8.85" "100.88.9.147" "100.88.8.100" "100.88.11.88" "100.88.10.109" "100.88.11.8" "100.88.8.92" "100.88.10.7" "100.88.9.138" "100.88.8.102" "100.88.8.112" "100.88.8.158" "100.88.9.83" "100.88.9.141" "100.88.9.163" "100.88.11.182" "100.88.9.149" "100.88.10.71" "100.88.11.58" "100.88.8.127" "100.88.10.150" "100.88.9.170" "100.88.8.138" "100.88.9.117" "100.88.10.53" "100.88.9.131" "100.88.8.94" "100.88.8.79" "100.88.11.84" "100.88.8.140" "100.88.10.92" "100.88.8.99" "100.88.10.77" "100.88.10.130" "100.88.10.64" "100.88.9.118" "100.88.9.112" "100.88.11.133" "100.88.9.144" "100.88.9.133" "100.88.11.134" "100.88.9.169" "100.88.8.156" "100.88.8.123" "100.88.10.160" "100.88.9.166" "100.88.10.34" "100.88.9.136" "100.88.9.152" "100.88.10.42" "100.88.10.157" "100.88.9.153" "100.88.9.87" "100.88.10.56" "100.88.10.123" "100.88.11.28" "100.88.11.52" "100.88.10.168" "100.88.9.137" "100.88.11.11" "100.88.9.171" "100.88.8.103" "100.88.9.130" "100.88.8.165" "100.88.9.125" "100.88.10.5" "100.88.8.119" "100.88.8.181" "100.88.10.108" "100.88.8.74" "100.88.9.101" "100.88.9.129" "100.88.10.198");
# ip_array=("100.88.9.135" "100.88.8.195");

# set name [lindex $argv 0]
# send $name

for ip in ${ip_array[*]}
do 
    echo $ip
    expect listip $ip
done