免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1416 | 回复: 0
打印 上一主题 下一主题

[其他] expect如何取远程主机上某个文件的内容 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2013-03-19 10:13 |只看该作者 |倒序浏览
大家好,我写了一个脚本来检测网卡速率的变化,如果与原来速率不一样,发出告警,现在将脚本粘贴如下:
1、主脚本:
#!/bin/bash
net_log_dir="/home/iaasmonitor/net_monitor/net_log"
cd /home/iaasmonitor/net_monitor
rm -f speed_err.txt drop_err.txt mail_speed.txt mail_drop.txt
grep -v \^# host_ip.txt > .host_vt.txt
for i in `cat .host_vt.txt`
do
ip=`echo $i|cut -f1 -d":"`
ssh_port=`echo $i|cut -f2 -d":"`
hostname=`echo $i|cut -f3 -d":"`
ssh_user=`echo $i|cut -f4 -d":"`
ssh_pwd=`echo $i|cut -f5 -d":"`
root_pwd=`echo $i|cut -f8 -d ":"`
export cmdfile="get_netinfo.txt"
speed_err=`./net_expect.sh $ip $ssh_port $ssh_user $ssh_pwd $root_pwd $cmdfile`
echo "`date "+%Y-%m-%d %k:%M:%S"` $hostname $ip $speed_err">>/home/iaasmonitor/net_monitor/net_log/`date "+%Y%m%d"`_speed_err.txt
echo "`date "+%Y-%m-%d %k:%M:%S"` $hostname  $ip  $speed_err" >>speed_err.txt

2、获取网卡速率变化信息的脚本(get_netinfo.txt)
#!/bin/sh
cd /tmp
rm -rf speed_err.txt
if [ ! -f net_speed.txt ]; then
sed 's/:/ /' /proc/net/dev|awk '{print $1}'|sed '1,2d'|egrep -v "lo|sit0|bond0|vir" > net_device.txt
for i in `cat net_device.txt`
do
speed=`ethtool $i|grep 'Speed'|sed -e 's/^.*\ //g'`
echo "$ispeed">>net_speed.txt
done
fi
for i in `cat net_speed.txt`
do
dev=`echo $i|cut -f1  -d":"`
old_speed=`echo $i|cut -f2  -d":"`
new_speed=`ethtool $dev|grep 'Speed'|sed -e 's/^.*\ //g'`
if [ "$old_speed" != "$new_speed" ]; then
echo "$dev $old_speed become $new_speed">>speed_err.txt
fi
done
cat  speed_err.txt -------这样写获取到监控主机上面的信息有expect验证与登录主机一系列的过程
3、net_expect.sh脚本:
#!/usr/bin/expect -f
set timeout 50
set ip [lindex $argv 0]
set ssh_port [lindex $argv 1]
set ssh_user [lindex $argv 2]
set ssh_pwd [lindex $argv 3]
set root_pwd [lindex $argv 4]
set cmdfile [ open [ lindex $argv 5] ]
spawn ssh -p $ssh_port -l $ssh_user $ip
expect {
     "yes/no)?\ " {send "yes\r";exp_continue}
     "*assword:\ " {send "$ssh_pwd\r"}
}
expect "*]$\ " {send "su - root\r"}
expect "*assword:\ " {send "$root_pwd\r"}
expect "*]#\ " {
      while {[gets $cmdfile cmd] >=0} {
      send "$cmd\r"
      }
}
expect "*]#\ " {send "exit\r"}
expect "*]$\ " {send "exit\r"}
close $cmdfile
expect eof
4、host_ip.txt的信息如下:
格式形式:
IP地址:ssh端口:主机名称:普通用户:密码:百分比:业务:root密码
如:
192.168.1.5:22:test-CACHE01:support:support:null:test:123456

脚本上述,可以将网卡的变化情况在被监控主机192.168.1.5上面采集下来,放在/tmp/speed_err.txt文件中,我想将这个文件里面的信息cat出来放到监控主机某个文件下面,但是在获取在获取网卡速率变化信息的脚本get_netinfo.txt中增加最后一行,获取一些无用的信息,有没有办法简单地只取出我需要的只是速率变化的情况出来?
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP