如何从期望脚本中转义不寻常/uniq字符? [英] how to escape unusual/uniq characters from expect scripts?

查看:98
本文介绍了如何从期望脚本中转义不寻常/uniq字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在期望脚本中,我可以设置任何命令或字符以在远程计算机上运行它 但是可悲的是,期望不能发送与期望脚本中定义的字符相同的字符

in expect script I can set any command or character to run it on remote machine but the sad thing is that expect cant send the same character as they defined in the expect script

例如

我想从Expect脚本运行此行,以便将IP地址从 10.10.10.10 更改 到 1.1.1.1

I want to run this line from expect script in order to change IP address from 10.10.10.10 to 1.1.1.1

    expect #  {send "perl -i -pe 's/\Q10.10.10.10\E/1.1.1.1/' /etc/hosts\r"}

但是当我实际运行期望"屏幕时,我看到此行在控制台上运行:

but when I run the expect screen actually I see this line runing on the console:

   [root@localhost ~]# perl -i -pe 's/Q10.10.10.10E/1.1.1.1/' /etc/hosts

请注意,消失了Q之前和E之前的反斜杠

pay attention that the backslash before Q and before E was Disappeared

所以我想知道如何从期望脚本中转义那些字符?

so I wonder hoW to escape those characters from expect script?

所以期望它将在控制台上与以下相同的行

so expect will run the same line on the console as following

   [root@localhost ~]# perl -i -pe 's/\Q10.10.10.10\E/1.1.1.1/' /etc/hosts

  • REMARK在反斜杠没有帮助之前设置反斜杠"\" !!!
  • 我的剧本:

     #!/bin/ksh
    
     #
      expect=`cat << EOF
      set timeout -1
      spawn  ssh  192.9.200.10 
       expect {
                 ")?"   { send "yes\r"  ; exp_continue  }
    
                 word:  {send secret1\r}
              }
       expect #  {send "perl -i -pe 's/\\Q10.10.10.10\\E/1.1.1.1/' /etc/hosts\r"}
       expect #    {send exit\r}
       expect eof
       EOF`
    
    
        expect -c  "$expect" 
    

    结果(在我运行脚本后:)

    RESULTS ( after I run my script: )

      spawn ssh 192.9.200.10 
      root@'192.9.200.10 s password: 
      Last login: Sun Aug  4 22:46:53 2013 from 192.9.200.10 
      [root@localhost ~]# perl -i -pe 's/Q10.10.10.10E/1.1.1.1/' /etc/hosts
          [root@localhost ~]# exit
          logout
    
          Connection to 192.9.200.10  closed.
    

    推荐答案

    使用不同的Tcl引号将起作用

    Using different Tcl quotes will work

    expect #  {
        # send text verbatim here
        send {perl -i -pe 's/\Q10.10.10.10\E/1.1.1.1/' /etc/hosts}
        # interpret backslash sequence as carriage return here
        send "\r"
    }
    

    这篇关于如何从期望脚本中转义不寻常/uniq字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆