expect -c "
spawn ssh-copy-id -i /root/.ssh/id_rsa.pub root@$ip
expect {
\"*yes/no*\" {send \"yes\r\"; exp_continue}
\"*password*\" {send \"$password\r\"; exp_continue}
\"*Password*\" {send \"$password\r\";}
}
"
done
for h in $(cat /scripts/ip.txt|cut -f1 -d":")
do
ssh root@$h ‘ ifconfig ‘
#如果命令是多行的,请参照下面
#ssh root@$h ‘此处写要执行的命令1‘
#ssh root@$h ‘此处写要执行的命令2‘
#ssh root@$h ‘此处写要执行的命令3‘
done
[root@Ansible scripts]# cat ip.txt
192.168.0.156:hwg123
192.168.0.157:hwg123
4,最后执行脚本即可
[root@Ansible scripts]# sh fenfa.sh
spawn ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.0.156
Now try logging into the machine, with "ssh ‘root@192.168.0.156‘", and check in:
.ssh/authorized_keys
to make sure we haven‘t added extra keys that you weren‘t expecting.
spawn ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.0.157
Now try logging into the machine, with "ssh ‘root@192.168.0.157‘", and check in:
.ssh/authorized_keys
to make sure we haven‘t added extra keys that you weren‘t expecting.
eth0 Link encap:Ethernet HWaddr 00:50:56:21:DA:F4
inet addr:192.168.0.156 Bcast:192.168.0.255 Mask:255.255.255.0
inet6 addr: fe80::250:56ff:fe21:daf4/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:58225 errors:0 dropped:0 overruns:0 frame:0
TX packets:351 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:3662557 (3.4 MiB) TX bytes:45504 (44.4 KiB)
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:0 (0.0 b) TX bytes:0 (0.0 b)
eth0 Link encap:Ethernet HWaddr 00:50:56:35:D4:31
inet addr:192.168.0.157 Bcast:192.168.0.255 Mask:255.255.255.0
inet6 addr: fe80::250:56ff:fe35:d431/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:228313 errors:0 dropped:0 overruns:0 frame:0
TX packets:6336 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:22630600 (21.5 MiB) TX bytes:443245 (432.8 KiB)
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:19 errors:0 dropped:0 overruns:0 frame:0
TX packets:19 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:1292 (1.2 KiB) TX bytes:1292 (1.2 KiB)
当然这个比较low,以后有时间再改进一下。
原文地址:http://blog.51cto.com/hwg1227/2096907