谢谢楼上,已经陆续有朋友介绍一些思路,有些朋友也是用shell来修改,但我shell目前更不熟。
正好自己刚学了点expect,也是想利用这个案例来强化一下自己的实践能力吧。 作者: YuriGagarin 时间: 2014-01-13 08:38
[root@M-SERVER fenfa_view]# cat view_client_info.sh
#!/bin/sh
. /etc/init.d/functions
for ip in `cut -d " " -f1 iplist`
do
password=`grep $ip iplist|cut -d " " -f2`
expect lipeixue.exp ${ip} ${password}
done
[root@M-SERVER fenfa_view]# cat lipeixue.exp
#!/usr/bin/expect
#define var
set ip [lindex $argv 0]
set password [lindex $argv 1]
spawn ssh -P22 root@$ip free -m
expect "password"
send "$password\r"
expect eof
exit
[root@M-SERVER fenfa_view]# sh view_client_info.sh
spawn ssh -P22 root@192.168.1.183 free -m
ssh: connect to host 192.168.1.183 port 22: No route to host
send: spawn id exp6 not open
while executing
"send "$password\r""
(file "lipeixue.exp" line
spawn ssh -P22 root@192.168.1.182 free -m root@192.168.1.182's password:
total used free shared buffers cached
Mem: 1010 67 943 0 8 40
-/+ buffers/cache: 17 992
Swap: 2303 0 2303
经过朋友的帮助,问题算是得到初步解决。但我还想再进一步,如何才能把因为主机没有开机,或者无法连接到主机产生的输出去掉,然后再输出1个简单点的输出说明呢?
就是想把如下部分删除,或者只要这里的第1行“No route to host”也行。
ssh: connect to host 192.168.1.183 port 22: No route to host
send: spawn id exp6 not open
while executing
"send "$password\r""
(file "lipeixue.exp" line 作者: expert1 时间: 2014-02-27 11:11