如何在向linux主机发送命令时转义asterisk / glob

时间:2013-05-23 15:32:17

标签: linux perl unix escaping expect

我编写了一个脚本,它获取了主机文件和cmdlist的内容,依次登录每个主机并在cmdfile中执行每个命令,然后返回输出。典型的会话可能如下所示:

-----------------------------------------
        rdo - run a cisco command
  on numerous devices and return output
-----------------------------------------

username: myuser
password: *********

devices to log into: hostname1 

you want to perform the following command(s) on all hosts:

  echo 'P..p..pick up a p..p..enguin!' | cowsay -f tux

is this correct? [Y/N]: y


Beginning expect...

---------- hostname1 (1 of 1) ------------

chosen ssh to connect.
logged in successfully.

Output for "echo 'P..p..pick up a p..p..enguin!' | cowsay -f tux":

    _______________________________
   < P..p..pick up a p..p..enguin! >
    -------------------------------
    \
     \
      .--.
     |o_o |
     |:_/ |
    //   \ \
   (|     | )
   /'\_   _/`\
   \___)=(___/

Closed connection to server.

这一切都很好。然而,当我在其中包含一个带星号的命令时,Expect可以理解为难以理解并使我的输出变得混乱,似乎是从早期的缓冲区中重复出现的东西。

-----------------------------------------
        rdo - run a cisco command
  on numerous devices and return output
-----------------------------------------

username: myuser
password: *********

devices to log into: hostname1 

you want to perform the following command(s) on all hosts:

  echo 'P..p..pick up a p..p..enguin!' | cowsay -f tux

is this correct? [Y/N]: y


Beginning expect...

---------- hostname1 (1 of 1) ------------

chosen ssh to connect.
logged in successfully.

Output for "echo 'P..p..pick up a p..p..enguin!' | cowsay -f tux":

    _______________________________
   < P..p..pick up a p..p..enguin! >
    -------------------------------
    \
     \
     .--.
    |o_o |
    |:_/ |
   //   \ \
  (|     | )
  /'\_   _/`\
  \___)=(___/

Output for "cat /etc/*release":

    Last login: Thu May 23 15:15:54 2013 from XXXX
 cowsay -f tuxemhostname1 $ echo 'P..p..pick up a p..p..enguin!' |
     _______________________________
    < P..p..pick up a p..p..enguin! >
     -------------------------------
      \
       \
       .--.
      |o_o |
      |:_/ |
     //   \ \
    (|     | )
    /'\_   _/`\
    \___)=(___/
   myuser@hostname1 $ cat /etc/*release
    CentOS release 6.3 (Final)
    CentOS release 6.3 (Final)
    CentOS release 6.3 (Final)

Closed connection to server.

我发送命令的方式非常基本。当我剥离所有循环时,它就像:

my $cmd = "cat /etc/*release";
$expect_session->send("$cmd\n");
my $outp = $expect_session->before();

我知道这是因为Expect正在解释我的星号并用它来做它不应该的东西。我的问题是,我可以对$ cmd做一些事情,以期望忽略glob但是将它直接发送给主机吗?我尝试用单反斜杠和双反斜杠逃避它无济于事。我觉得有些东西我不见了。

谢谢你的时间。

0 个答案:

没有答案