[root@localhost ~]# cat bash.sh
#!/bin/bash
for i in $(awk '{print $1}' passwd.txt)
do
j=$(awk -v I="$i" '{if(I==$1)print $2}' passwd.txt) //将AWK取出的值,赋值给j
expect /root/login.exp $i $j //expect调用 login.exp 脚本,并带上参数i,j
done
#[root@localhost ~]# cat login.exp
#! /usr/bin/expect
##by Lightman
set ipaddress [lindex $argv 0] //设置ipaddress变量,采用参数0
set password [lindex $argv 1] //设置ipaddress变量,采用参数0
spawn ssh ii@$ipaddress //spwan 激活ssh程序进行交互式运行
expect {
"yes/no" {send "yes\r"} //捕捉yes/no的字符串,并发送send命令
"*assword:" {send "$password\r"} //捕捉*assword的字符串,并发送send命令
}
expect "]*" //捕捉shell提示符($、#)
send "date\r"
send "sleep 5\r"
#send "su - ii\r"
send "su - root\r"
expect "*assword:"
send "echo\“hello \” >> /root/temp.txt\r" //send 传送命令时,如遇到有双引号之类的 ,要用 "\" 进行转义
send "sleep 5\r"
send "exit\r"
send "sleep 5\r"
send "exit\r"
interact //terminal保持停留在当前服务器
[root@localhost ~]# cat passwd.txt
192.168.56.1 12345623
192.168.195.128 123456
192.168.177.128 1234563
阅读(8654) | 评论(0) | 转发(0) |