分类:
2008-10-13 09:31:30
VIO学习之expect完结
继《VIO学习之expect进阶》(http://www.cublog.cn/u/739/showart.php?id=734229)之后,工作中发现约有9%的LPAR不能获得相关的信息,且有问题的LPAR并不是固定的,问题时有时无,最终查明原因是send太快。问题的详细描述可见 。从这里可以总结出两点:
1、遇到问题,需要自己动手调试,expect的调试方式就是“expect -d”,看得懂“expect -d”的信息就能找到问题的根源。
2、expect脚本中很多内容不用自己写,把autoexpect自动生成的内容稍做改动,就好了。只要能看得懂,那一切都很简单。
最后解释下/bin/dailyck.exp的思路,照搬这个思路可以写出各种功能的脚本:批量创建用户(每台机器上创建同样的用户),批量改root密
码,批量开通ftp记日志功能,批量检查IPSEC、SNA、网卡禁用工作状态,等。因此,本文叫做《VIO学习之expect完结》。脚本/bin
/dailyck.exp实际上就做了三件事:
1、把命令及执行结果记录到本地的一个文本文件中
log_file $logname
2、不管三七二十一,一登录到远程机器,就把提示符改成“$”
export PS1=$
3、把所有检查内容的相关命令都写到下面“......”处
ksh <<'!!!'
echo \n
......
echo \n
!!!
打印两个空行的作用就是为了方便/bin/dailyck.ksh(《VIO学习之expect进阶》中有第一版的/bin/dailyck.ksh)通过grep -p来取我们关心的内容。
cat <<'!!' >/bin/dailyck.exp
#!/usr/bin/expect -f
#DATE: 2008-06-24
#email:
#blog: http://www.cublog.cn/u/739/
set timeout 10
set username dailyck
set passwd 此处需要改成真密码
set logname /home/padmin/dailyck.log
match_max 10000
log_user 0
spawn telnet [lindex $argv 0]
expect "ogin:"
sleep .1
send -- "$username\r"
expect "assword:"
sleep .1
send -- "$passwd\r"
sleep .1
log_file $logname
expect {
""
{send -- "export PS1=$\r"}
"#"
{send -- "export PS1=$\r"}
"invalid login name or password"
{send_log "\n\n[lindex $argv 0] The password is wrong\n\n"; exit 1}
timeout
{send_log "\n\n[lindex $argv 0] Timeout\n\n"; exit 2}
eof
{send_log "\n\n[lindex $argv 0] Connection to host failed: expect_out(buffer)\n\n"; exit 3}
}
expect -exact "\r
\$"
sleep .1
log_user 1
sleep .1
send -- "ksh <<'!!!'\r"
expect -exact "\r
> "
send -- "echo \""
expect -exact "\r
> "
send -- "host `hostname`\r"
expect -exact "\r
> "
send -- "TODAY=`date +\"%m%d\"`\r"
expect -exact "\r
> "
send -- "YESTERDAY=`TZ=+24 date +\"%m%d\"`\r"
expect -exact "\r
> "
send -- "errpt | awk '\$2 ~ YESTERDAY\"......\" || \$2 ~ TODAY\"......\"' YESTERDAY=\$YESTERDAY TODAY=\$TODAY | "
expect -exact "\r
> "
send -- "grep -v '\[PT\] S SYSXDLCI'\r"
expect -exact "\r
> "
send -- "df | awk 'NR>1, sub(/%/,\"\",\$4){if ( \$4 > 85 ) print \$4\"%\\t\"\$NF}'\r"
expect -exact "\r
> "
send -- "lspath | grep -v ^Enabled\r"
expect -exact "\r
> "
send -- "today=`date +\"%b %d\"`\r"
expect -exact "\r
> "
send -- "yesterday=`TZ=+24 date +\"%b %d\"`\r"
expect -exact "\r
> "
send -- "last | awk 'BEGIN {today='\\\"\"\$today\"; yesterday='\\\"\"\$yesterday\"} "
expect -exact "\r
> "
send -- "\$0 !~ today && \$0 !~ yesterday {exit}; NF==9 {sub(/\\/.*/,\"\",\$2); sub(/rsh.*/,\"rsh\",\$2); "
expect -exact "\r
> "
send -- "print \$4,\$5,\$3,\$2,\$1}; NF<9 {print}' | grep -v \"9\\.1\\.7\[23\]\\.241 pts dailyck\" | sort -r | uniq -c\r"
expect -exact "\r
> "
send -- "wait\r"
expect -exact "\r
> "
send -- "today=`date +\"%b %e\"`\r"
expect -exact "\r
> "
send -- "yesterday=`TZ=+24 date +\"%b %e\"`\r"
expect -exact "\r
> "
send -- "awk '{ a\[NR\]=\$0} END{ for(i=NR;i>0;i--) print a\[i\] }' /var/ftp.log |\\\r"
expect -exact "\r
> "
send -- "awk 'BEGIN {FS=\""; today='\\\"\"\$today\"; yesterday='\\\"\"\$yesterday\"} "
expect -exact "\r
> "
send -- "\$1 !~ today && \$1 !~ yesterday {exit}; !/file local \$/ && / ftp/ {print \$2}' |\\\r"
expect -exact "\r
> "
send -- "awk '{ a\[NR\]=\$0} END{ for(i=NR;i>0;i--) print a\[i\] }' |\\\r"
expect -exact "\r
> "
send -- "sed -e 's/ connection from .* at //g' -e 's/ FTP LOGIN FROM //g' "
expect -exact "\r
> "
send -- "-e 's/FTPD: EXPORT file local/ DOWNLOAD/g' -e 's/FTPD: IMPORT file local/ UPLOAD/g'\r"
expect -exact "\r
> "
send -- "wait\r"
expect -exact "\r
> "
send -- "echo \""
expect -exact "\r
> "
send -- "!!!\r"
expect -exact "\r
\$"
sleep .1
log_file
send -- "exit\r"
interact
!!
chmod +x /bin/dailyck.exp
========================================================================
任何形式的转载,请写明出处:
email:
blog: http://www.cublog.cn/u/739/
========================================================================