linux expect自动登陆远程服务器 批量管理服务器

#!/usr/bin/expect
 
set ipaddress [lindex $argv 0]
set passwd [lindex $argv 1]
set timeout 3
 
spawn ssh root@${ipaddress}
 
expect {
        "yes/no" 
                {
                        send "yes\r";exp_continue
                }
 
        "*assword"
                {
                        send "${passwd}\r"
                }
}
expect "*from*"
send "mkdir -p /home/hahahahha"   #在远程服务器创建/home/hahahahha 的文件夹
expect "]#"
expect eof

 

 
 
#
for i in `awk '{print $1}' passwd.txt`
do
        j=`awk -v I="$i" '{if(I=$i)print $2}' passwd.txt`
        expect /root/2_ssh.exp $i $j
done

 

posted @ 2017-08-01 17:00  Ray雷  阅读(241)  评论(0编辑  收藏  举报