shell---远程取数据的脚本.

远程自动脚本两个脚本执行自动化,首先定义一个文件用来存放你要访问的服务器的地址端口用户名密码 本机地址
格式如下:

ip port username password localhostIP

保存为server.conf

shell配合expect, shell用作循环,expect用作交互。

1.用shell写一个while循环。

#!/bin/bash
#by osx1260@163.com
filename="server.conf"
while read line
    do
      ./expect.exp
done < $filename         

2.写一个expect.exp

#!/usr/bin/expect -f
set ipaddress [lindex $argv 0]
set port [lindex $argv 1]
set username [lindex $argv 2]
set passwd [lindex $argv 3]
set localIP [lindex $argv 4]
set timeout 20
spawn ssh $ipaddress -p$port -l$username
        expect {
"yes/no" { send "yes\r";exp_continue }
"assword:" { send "$passwd\r" }
        }
        expect -re "\](\$|#) "
send "xm list &>/root/check-network.log \r"
        expect -re "\](\$|#) "
send "sar -n DEV 3 5 | grep vif* | awk '{print \$2,\$5,\$6}' & >> /root/check-network.log \r"
#       expect -re "\](\$|#) "
#send "sort -t\" \" -k 2n -u /root/test.log @>>/root/check-network.log \r"
        expect -re "\](\$|#) "
send "scp -r /root/check-network.log $username@$localIP:/var/log/check-network.log \r"
        expect {
"assword:" { send "$passwd\r" }
                }

        expect -re "\](\$|#) "
send "rm -fr /root/test.log \r"
send "rm -fr /root/check-network.log \r"
send "exit\r"

有时间了写具体的过程注释~~这里留做自己的笔记

posted @ 2013-05-10 15:43  osxlinux  阅读(361)  评论(0编辑  收藏  举报