一文件,格式如下:
192.168.1.1 command1
192.168.1.2 command2
192.168.1.3 command3
192.168.1.4 command4
……….
第一列是写主机IP列表,第二列是不同的命令
现在求的脚本要实现这样的功能:
要在一台主控机上分别登陆第一列列出的ip在该台主机上执行对应的command
这个问题不管两台主机之间有没有做过信任,expect + awk 都可以搞定
解:
假设用户root,端口22。
ssh.exp文件:
CODE
#!/usr/bin/expect
set timeout 100
set ip [lindex $argv 0]
set command [lindex $argv 1]
spawn ssh root@$ip
expect "#"
send "$command\r"
expect "#"
send "exit\r"
info.txt文件:
192.168.1.1 echo "haha" > file1.txt
192.168.1.2 echo "haha" > file2.txt
shell下执行
CODE
awk ‘{ip=$1;$1="";command=$0;while("./ssh.exp "ip" \""command"\" " | getline){next}}’ info.txt
说明一点,这里为什么不用shell作循环而选用awk,因为shell作循环效率太差…
如果您喜欢本文,欢迎订阅我的文章:
http://feed.jiayii.com
转载请注明出处:http://www.jiayii.com/awk-expect-example/
谢谢您的关注。 : @jiayisuse : jiayisuse#gmail.com
转载请注明出处:http://www.jiayii.com/awk-expect-example/
谢谢您的关注。 : @jiayisuse : jiayisuse#gmail.com


貌似可以将列表上的机子当肉鸡?~
[Reply]
额,要不互相做过信任,要不就得有密码。。。
[Reply]
肉鸡啊
[Reply]
会报错, bash: {next} 未知预料。。。
[Reply]
jiayi Reply:
September 8th, 2010 at 20:22
@yuanzheng,
前面的ssh.exp跟info.txt文件要先建好。。
[Reply]