免费注册 查看新帖 |

Chinaunix

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

[网络相关] 请教各位大侠,关于自动登陆设备执行命令 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2013-04-27 20:18 |只看该作者 |倒序浏览
我的需求是这样的,写一个脚本自动登录到网络设备,执行几条命令,然后将其中的一些文件put到ftp_server上。里面有一个问题是在执行一条命令时,需要等待一段时间,这个时间不固定。

下面是我写的脚本及出现的问题,初次接触linux脚本,请各位大侠帮忙看看该怎么弄,谢谢!!:

设备名称脚本:list.conf

sysname1 192.168.170.2 h3c h3c

执行脚本:test.sh

#! /bin/sh
while read ip
do
./test.exp $ip
done < list.conf

命令脚本:test.exp

#!/usr/bin/expect

set sysname [lindex $argv 0]
set ip [lindex $argv 1]
set username [lindex $argv 2]
set password [lindex $argv 3]
#set superpassword [lindex $argv4]

set ftpserver 192.168.170.46
set ftpuser h3c
set ftppasswd h3c

log_file /root/log/$ip.log
spawn telnet $ip
expect "*name:"
send "$username\r"
expect "*word:"
send "$password\r"
expect "*>"
send "screen-length disable\r"
expect "*>"
send "sys\r"
expect "*]"
#save logbuffer
send "logfile save\r"
expect "*]"
send "_h\r"
expect "cmd]"
#save diagfile buffer
send "_diagnose-file save\r"
expect "*]"
#save default.diag

前面这些都执行的比较顺利,下面可以执行完命令,由于这里的display  diagnostic-information需要一段时间,这里不知道等多久,使用expect等待字符也没有用直接回跳到下一个ip再执行上面的动作

个人的感觉应该是系统会有一个超时机制,由于这里执行命令需要一段时间(但是这个时间不固定没办法用timeout来等),超过了这个超时时间,命令自动向下执行了,但是不知道怎么回继续往下面expect下面执行,感觉只要前面出现过,就算执行过一样。。。这里搞了个八小时,还是没搞定,哪个大侠给指条明路吧,感谢!!

send "display diagnostic-information\r"
expect "*/N]:"
send "y\r\r\r"
expect {
  "*N]:" {send "y\r"}
  "successfully." {}
}

expect "*]"

send "quit\r"
#access CF Card to send files to FTP Server
expect "*>"
send "cd cfa1:/\r"
expect "cfa1: FAT*"
send "cd logfile/\r"
expect "*>"
send "ftp $ftpserver\r"
expect "*e)):"
send "$ftpuser\r"
expect "*assword:"
send "$ftppasswd\r"
expect "*tp]"
send "mkdir $sysname\r"
expect "*successfully"
send "\r\r"
send "put logfile1.log\r"
expect "*tp]"
send "put /diaglog/diagfile1.log\r"
expect "*tp]"
send "put /default.diag\r"
expect "*tp]"
send "bye\r"
expect "*>"
send "quit\r"
expect eof

执行和报错:

Username:h3c
Password:
<switch>screen-length disable
% Screen-length configuration is disabled for current user.
<switch>sys
System View: return to User View with Ctrl+Z.
[switch]logfile save
Saved the log file buffer to file cfa1:/logfile/logfile7.log successfully.
[switch]_h
Now you enter a hidden command view for developer's testing, some commands may
affect operation by wrong use, please carefully use it with our engineer's
direction.
[switch-hidecmd]_diagnose-file save
Info: Save all the contents in the diagnostic file buffer into file cfa1:/diaglog/diagfile10.log successfully.
[switch-hidecmd]display diagnostic-information
Save or display diagnostic information (Y=save, N=display)? [Y/N]:y
Please input the file name(*.diag)[cfa1:/default.diag]:
The file already exists, overwrite it? [Y/N]:
Before pressing ENTER you must choose 'YES' or 'NO'[Y/N]:y
Diagnostic information is outputting to cfa1:/default.diag.
Please wait...
Save successfully.
[switch-hidecmd]cd logfile/
        ^
% Unrecognized command found at '^' position.
[switch-hidecmd]^Cspawn telnet
usage: telnet [-l user] [-a] host-name [port]
send: spawn id exp5 not open
    while executing
"send "$username\r""
    (file "./test.exp" line 16)
[root@rconfig50 ~]# vim test.exp

论坛徽章:
0
2 [报告]
发表于 2013-04-27 21:06 |只看该作者
刚刚增加了两句“下面加粗”,命令那个可以顺利执行。  但是这个timeout 300 很奇怪,我这里就是在尝试。。。。 但是不知道为什么

set timeout 300
expect "*]"

send "quit\r"
send "quit\r"
#access CF Card to send files to FTP Server
expect "*>"
send "cd cfa1:/\r"
exec sleep 10
send "dir\r"

论坛徽章:
0
3 [报告]
发表于 2013-05-11 15:28 |只看该作者
回复 1# postskyl


    check 一下 username是不是空值

论坛徽章:
0
4 [报告]
发表于 2013-05-11 23:49 |只看该作者
回复 3# fufelixzh

谢谢! 问题已经解决了。不是username的问题 ,最终将timeout 改成-1 就ok了:)


   
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP