免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
楼主: whatislinux
打印 上一主题 下一主题

[文本处理] 看看 expect 执行,结果不能自动登录,提示invalid command name "~" [复制链接]

论坛徽章:
0
11 [报告]
发表于 2014-06-18 13:48 |只看该作者
回复 9# Shell_HAT


    当时生产秘钥是,是技术部给分配的。我不知道。所以只能在通过上面你说的这个方法了,对吗

论坛徽章:
2
IT运维版块每日发帖之星
日期:2016-02-10 06:20:01IT运维版块每日发帖之星
日期:2016-02-11 06:20:00
12 [报告]
发表于 2014-06-18 13:55 |只看该作者
回复 10# todayhero


   
记录下,回去测试下

论坛徽章:
0
13 [报告]
发表于 2014-06-18 13:58 |只看该作者
回复 10# todayhero
我没有“send "sh /data/tool/sync/web.sh\r"” 是不是需要再创建秘钥


   

论坛徽章:
0
14 [报告]
发表于 2014-06-18 14:02 |只看该作者
@whatislinux



这是我执行的命令,登录上去,总要操作点东西吧。你随便写个命令就OK。

论坛徽章:
0
15 [报告]
发表于 2014-06-18 14:09 |只看该作者
回复 14# todayhero

#!/usr/bin/expect -f
set ip l-hs01.wap.cn1
set pw "v^@*8#3k8^z-@;~"
spawn ssh  abel.li@$ip
        expect {
                "want"        {send -- "yes\r"; exp_continue}
                "password:"   {send -- $pw\r}
                "No route"    { exit }
                }

        set timeout 5
#        expect  "*#"
        expect "]#"
        send "pwd\r"
        expect "]#"
        send "exit\r"
然后在执行时,结果是这样,还是停留在本地机器上
    [abel@l-rtools1.ops.cn1 ~]$ ./ssh-hs01.sh
spawn ssh abel.li@l-hs01.wap.cn1
Enter passphrase for key '/home/q/home/abel.li/.ssh/id_rsa':
Enter passphrase for key '/home/q/home/abel.li/.ssh/id_rsa': [abel@l-rtools1.ops.cn1 ~]$

论坛徽章:
0
16 [报告]
发表于 2014-06-18 14:41 |只看该作者
@whatislinux

我对expect不熟悉,不对我提供的那个脚本,在我机子上运行正常。登录到远程机子,执行命令,然后再退回本地。

论坛徽章:
16
IT运维版块每日发帖之星
日期:2015-08-24 06:20:00综合交流区版块每日发帖之星
日期:2015-10-14 06:20:00IT运维版块每日发帖之星
日期:2015-10-25 06:20:00IT运维版块每日发帖之星
日期:2015-11-06 06:20:00IT运维版块每日发帖之星
日期:2015-12-10 06:20:00平安夜徽章
日期:2015-12-26 00:06:302016猴年福章徽章
日期:2016-02-18 15:30:34IT运维版块每日发帖之星
日期:2016-04-15 06:20:00IT运维版块每日发帖之星
日期:2016-05-21 06:20:00综合交流区版块每日发帖之星
日期:2016-08-16 06:20:002015七夕节徽章
日期:2015-08-21 11:06:17IT运维版块每日发帖之星
日期:2015-08-14 06:20:00
17 [报告]
发表于 2014-06-18 14:45 |只看该作者
-d debug看一下结果

expect -d uscript

论坛徽章:
0
18 [报告]
发表于 2014-06-18 14:55 |只看该作者
回复 17# expert1


    执行了,帮忙看看
[abel.li@l-rtools1.ops.cn1 ~]$ expect -d ssh-hs01.sh
expect version 5.43.0
argv[0] = expect  argv[1] = -d  argv[2] = ssh-hs01.sh  
set argc 0
set argv0 "ssh-hs01.sh"
set argv ""
executing commands from command file ssh-hs01.sh
spawn ssh abel.li@l-hs01.wap.cn1
parent: waiting for sync byte
parent: telling child to go ahead
parent: now unsynchronized from child
spawn: returns {9222}

expect: does "" (spawn_id exp6) match glob pattern "want"? no
"password:"? no
"No route"? no
Enter passphrase for key '/home/q/home/abel.li/.ssh/id_rsa':
expect: does "Enter passphrase for key '/home/q/home/abel.li/.ssh/id_rsa': " (spawn_id exp6) match glob pattern "want"? no
"password:"? no
"No route"? no
expect: timed out

expect: does "Enter passphrase for key '/home/q/home/abel.li/.ssh/id_rsa': " (spawn_id exp6) match glob pattern "]#"? no
expect: timed out
send: sending "pwd\r" to { exp6 }

expect: does "Enter passphrase for key '/home/q/home/abel.li/.ssh/id_rsa': " (spawn_id exp6) match glob pattern "]#"? no

Enter passphrase for key '/home/q/home/abel.li/.ssh/id_rsa':
expect: does "Enter passphrase for key '/home/q/home/abel.li/.ssh/id_rsa': \r\nEnter passphrase for key '/home/q/home/abel.li/.ssh/id_rsa': " (spawn_id exp6) match glob pattern "]#"? no
expect: timed out
send: sending "exit\r" to { exp6 }

论坛徽章:
16
IT运维版块每日发帖之星
日期:2015-08-24 06:20:00综合交流区版块每日发帖之星
日期:2015-10-14 06:20:00IT运维版块每日发帖之星
日期:2015-10-25 06:20:00IT运维版块每日发帖之星
日期:2015-11-06 06:20:00IT运维版块每日发帖之星
日期:2015-12-10 06:20:00平安夜徽章
日期:2015-12-26 00:06:302016猴年福章徽章
日期:2016-02-18 15:30:34IT运维版块每日发帖之星
日期:2016-04-15 06:20:00IT运维版块每日发帖之星
日期:2016-05-21 06:20:00综合交流区版块每日发帖之星
日期:2016-08-16 06:20:002015七夕节徽章
日期:2015-08-21 11:06:17IT运维版块每日发帖之星
日期:2015-08-14 06:20:00
19 [报告]
发表于 2014-06-18 15:35 |只看该作者
expect: does "" (spawn_id exp6) match glob pattern "want"? no
"password:"? no
"No route"? no

#这部分应该是你ssh过去后的,没人任何output.

Enter passphrase for key '/home/q/home/abel.li/.ssh/id_rsa':
这个是终端输出,

expect: does "Enter passphrase for key '/home/q/home/abel.li/.ssh/id_rsa': " (spawn_id exp6) match glob pattern "want"? no
"password:"? no
"No route"? no
expect: timed out
是否匹配你的设定want/password/No route,没有一个匹配上。

expect: does "Enter passphrase for key '/home/q/home/abel.li/.ssh/id_rsa': " (spawn_id exp6) match glob pattern "]#"? no
expect: timed out

也不匹配]#

输出是Enter passphrase for key '/home/q/home/abel.li/.ssh/id_rsa':

应该写成expect "Enter"
            send 你的密码

expect是一问一答形式,比如终端输出是Hello xxxx, 那你要捕获的是hello xxx的任意字符。然后send对应的答案。 你man expect看下关于expect的大概说明,然后结合这个debug看下就明白了。

论坛徽章:
16
IT运维版块每日发帖之星
日期:2015-08-24 06:20:00综合交流区版块每日发帖之星
日期:2015-10-14 06:20:00IT运维版块每日发帖之星
日期:2015-10-25 06:20:00IT运维版块每日发帖之星
日期:2015-11-06 06:20:00IT运维版块每日发帖之星
日期:2015-12-10 06:20:00平安夜徽章
日期:2015-12-26 00:06:302016猴年福章徽章
日期:2016-02-18 15:30:34IT运维版块每日发帖之星
日期:2016-04-15 06:20:00IT运维版块每日发帖之星
日期:2016-05-21 06:20:00综合交流区版块每日发帖之星
日期:2016-08-16 06:20:002015七夕节徽章
日期:2015-08-21 11:06:17IT运维版块每日发帖之星
日期:2015-08-14 06:20:00
20 [报告]
发表于 2014-06-18 15:37 |只看该作者
   expect {
                "want"        {send -- "yes\r"; exp_continue}
                "password:"   {send -- $pw\r}
                "No route"    { exit }
                }

这部分没任何作用了,因为你设了passphase,不可能有这个are you sure you want to continue或者passowrd 这种输出了,所以你expect捕获不到的。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP