免费注册 查看新帖 |

Chinaunix

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

【求助】 expect 怎么读文件里面的内容 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-11-11 02:11 |只看该作者 |倒序浏览
ip1.txt文件内容如下

ip  password
ip  password
ip  password
ip  password
ip  password

怎么把ip 和密码读出来并且访问它

用这段不行

#!/usr/bin/expect -f
set file [open ip1.txt r]
spawn ssh -p 57891 root@×.×.×.×
expect {
"*(yes/no)?" {send "yes\r"}
"*password:" {send "12345678\r"}
}
expect "*~]$"
while{[gets $file line]>=0}
{
send "scp -P 57891 root@[lindex $line 0]:./*\[0-9t\].tracert*  /home/root/popresult\r"
expect {
"*(yes/no)?" {send "yes\r"}
"*password:" {send "[lindex $line 1]\r"}
}
expect "*100%"
expect  eof
close
}
close $file





expect -d 调试信息如下
expect version 5.42.1
argv[0] = expect  argv[1] = -d  argv[2] = expect.sh  
set argc 0
set argv0 "expect.sh"
set argv ""
executing commands from command file expect.sh
spawn ssh -p 57891 root@×.×.×.×
parent: waiting for sync byte
parent: telling child to go ahead
parent: now unsynchronized from child
spawn: returns {30204}

expect: does "" (spawn_id exp5) match glob pattern "*(yes/no)?"? no
"*password:"? no
root@×.×.×.×'s password:
expect: does "root@×.×.×.×'s password: " (spawn_id exp5) match glob pattern "*(yes/no)?"? no
"*password:"? yes
expect: set expect_out(0,string) "root@×.×.×.×'s password:"
expect: set expect_out(spawn_id) "exp5"
expect: set expect_out(buffer) "root@×.×.×.×'s password:"
send: sending "12345678\r" to { exp5 }

expect: does " " (spawn_id exp5) match glob pattern "*~]$"? no


expect: does " \r\n" (spawn_id exp5) match glob pattern "*~]$"? no
Last login: Tue Nov 11 01:54:06 2008 from 222.141.223.144

expect: does " \r\nLast login: Tue Nov 11 01:54:06 2008 from 222.141.223.144\r\r\n" (spawn_id exp5) match glob pattern "*~]$"? no
[root@sh191 ~]$
expect: does " \r\nLast login: Tue Nov 11 01:54:06 2008 from 222.141.223.144\r\r\n\u001b]0;root@sh191:~\u0007[root@sh191 ~]$ " (spawn_id exp5) match glob pattern "*~]$"? no
expect: timed out
invalid command name "while{28>=0}"
    while executing
"while{[gets $file line]>=0}"
    (file "expect.sh" line 10)

我这个ip1.txt文件有101行 怎么会是while{28>=0} 不明白

论坛徽章:
8
摩羯座
日期:2014-11-26 18:59:452015亚冠之浦和红钻
日期:2015-06-23 19:10:532015亚冠之西悉尼流浪者
日期:2015-08-21 08:40:5815-16赛季CBA联赛之山东
日期:2016-01-31 18:25:0515-16赛季CBA联赛之四川
日期:2016-02-16 16:08:30程序设计版块每日发帖之星
日期:2016-06-29 06:20:002017金鸡报晓
日期:2017-01-10 15:19:5615-16赛季CBA联赛之佛山
日期:2017-02-27 20:41:19
2 [报告]
发表于 2008-11-11 08:51 |只看该作者
while {[gets $file line]>=0} {..............

论坛徽章:
0
3 [报告]
发表于 2008-11-11 09:01 |只看该作者
加个空格? 我开始就是加了空格的  

加了是 while {[gets $file line]>=0}"
没加是 while{28>=0}"
wrong # args: should be "while test command"
    while executing
"while {[gets $file line]>=0}"
    (file "./expect.sh" line 10)

TCL版本  tcl-8.4.7-2

expect版本  expect-5.42.1-1

内核 2.6.9-5.ELsmp

[ 本帖最后由 莫帝 于 2008-11-11 09:05 编辑 ]

论坛徽章:
8
摩羯座
日期:2014-11-26 18:59:452015亚冠之浦和红钻
日期:2015-06-23 19:10:532015亚冠之西悉尼流浪者
日期:2015-08-21 08:40:5815-16赛季CBA联赛之山东
日期:2016-01-31 18:25:0515-16赛季CBA联赛之四川
日期:2016-02-16 16:08:30程序设计版块每日发帖之星
日期:2016-06-29 06:20:002017金鸡报晓
日期:2017-01-10 15:19:5615-16赛季CBA联赛之佛山
日期:2017-02-27 20:41:19
4 [报告]
发表于 2008-11-11 09:56 |只看该作者

回复 #3 莫帝 的帖子

while {[gets $file line]>=0} {.........

论坛徽章:
0
5 [报告]
发表于 2008-11-11 10:09 |只看该作者
还是报错
extra characters after close-brace
    while executing
"while {[gets $file line]>=0}{
          send "scp -P 57891 root@[lindex $line 0]:./*\[0-9t\].tracert* /home/root/popresult\r"
          e..."
    (file "./expect.sh" line 10)

论坛徽章:
8
摩羯座
日期:2014-11-26 18:59:452015亚冠之浦和红钻
日期:2015-06-23 19:10:532015亚冠之西悉尼流浪者
日期:2015-08-21 08:40:5815-16赛季CBA联赛之山东
日期:2016-01-31 18:25:0515-16赛季CBA联赛之四川
日期:2016-02-16 16:08:30程序设计版块每日发帖之星
日期:2016-06-29 06:20:002017金鸡报晓
日期:2017-01-10 15:19:5615-16赛季CBA联赛之佛山
日期:2017-02-27 20:41:19
6 [报告]
发表于 2008-11-11 10:14 |只看该作者
}{
} {
眼镜度数不对吧?休息天再去配次光吧

论坛徽章:
0
7 [报告]
发表于 2008-11-11 10:28 |只看该作者
  多谢版主了 今天不搞好不睡觉了

登录第一台机器成功了,第二台时就报
send: spawn id exp5 not open
    while executing
"send "scp -P 57891 root@[lindex $line 0]:./$date*\[0-9t\].tracert* /home/root/popresult\r""
    ("while" body line 2)
    invoked from within
"while {[gets $file line]>=0} {
          send "scp -P 57891 root@[lindex $line 0]:./$date*\[0-9t\].tracert* /home/root/popresult\r"
          ..."
    (file "/root/expect.sh" line 10)

我猜是要把关闭文件放到循环里 改了之后还是只能登录第一台 ,到第二台就报
can not find channel named "file4"
    while executing
"gets $file line"
    (file "/root/expect.sh" line 1)

论坛徽章:
0
8 [报告]
发表于 2008-11-11 10:38 |只看该作者
版主帮忙解释一下 open 和 gets $file line

没找到资料  坛子里expect 的帖子看遍了
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP