使用Expect和ssh发送长命令

时间:2014-01-28 08:44:05

标签: ssh expect

我的 Tcl 8.5 框架中有一个 Expect 脚本。 它被用于从 Ubuntu 11.10 中生成ssh到其他 Fedora版本15(Lovelock) PC。

我设法通过 Expect 成功登录。
此外,我的脚本正在成功识别我发出的短命令,例如ifconfig

但是当发出exp_send长命令时,
它们被expect命令读取,每个X字符之间注入“\ r”(空格CR)。

例如,exp_send的参数是(使用exp_internal 1验证):

sudo tshark -c 1 -i ethernet0 -f 'broadcast and ether host 00:00:00:00:00:00' -T fields -E header=y -E separator=, -E quote=d -e frame.time_epoch -e wlan.sa -e wlan_mgt.supported_rates -e wlan_mgt.extented_supported_rates\r

发送后,expect命令将其读取为:

sudo tshark -c 1 -i ethernet0 -f 'broadcast and ether host 
 00:00:00:00:00:00' -T fields -E header=y -E separator=, -E quote=d -e frame.tim 
e_epoch -e wlan.sa -e wlan_mgt.supported_rates -e wlan_mgt.extented_supported_ra 
tes

(注意前3行末尾的额外单个空格)
如何在没有注入“\ r”的情况下发送命令?


编辑#1(2014年1月29日,08:30 UTC):
另一个命令也会出现此问题,例如echo 例如,我发送了exp_send字符串:

Hello World0!Hello World1!Hello World2!Hello World3!Hello World4!Hello World5!Hello World6!Hello World7!Hello World8!Hello World9!Hello World10!Hello World11!Hello World12!Hello World13!Hello World14!Hello World15!Hello World16!Hello World17!Hello World18!Hello World19!'\r

expect读取它之间注入'\ r',例如:

echo 'Hello World0!Hello World1!Hello World2!Hello World3! \rHello World4!Hello World5!Hello W

您可以看到以下使用exp_internal 1收到的实时示例:

expect: does "\u001b]0;root@MyFedora15:~\u0007[root@MyFedora15 ~]# " (spawn_id exp9) match full buffer full_buffer? no
".+"? (No Gate, RE only) gate=yes re=yes
expect: set expect_out(0,string) "\u001b]0;root@MyFedora15:~\u0007[root@MyFedora15 ~]# "
expect: set expect_out(spawn_id) "exp9"
expect: set expect_out(buffer) "\u001b]0;root@MyFedora15:~\u0007[root@MyFedora15 ~]# "
send: sending "echo 'Hello World0!Hello World1!Hello World2!Hello World3!Hello World4!Hello World5!Hello World6!Hello World7!Hello World8!Hello World9!Hello World10!Hello World11!Hello World12!Hello World13!Hello World14!Hello World15!Hello World16!Hello World17!Hello World18!Hello World19!'\r" to { exp9 }
Gate keeper glob pattern for '.+' is ''. Not usable, disabling the performance booster.
expect: does "" (spawn_id exp9) match full buffer full_buffer? no
".+"? (No Gate, RE only) gate=yes re=no
expect: does "echo 'Hello World0!Hello World1!Hello World2!Hello World3! \rHello World4!Hello World5!Hello W" (spawn_id exp9) match full buffer full_buffer? no
".+"? (No Gate, RE only) gate=yes re=yes
expect: set expect_out(0,string) "echo 'Hello World0!Hello World1!Hello World2!Hello World3! \rHello World4!Hello World5!Hello W"
expect: set expect_out(spawn_id) "exp9"
expect: set expect_out(buffer) "echo 'Hello World0!Hello World1!Hello World2!Hello World3! \rHello World4!Hello World5!Hello W"
Gate keeper glob pattern for '.+' is ''. Not usable, disabling the performance booster.

This is basically my Tcl8.5 code(为便于阅读而修改)。

1 个答案:

答案 0 :(得分:0)

这应该有所帮助:

send "tshark -c 1 -i ethernet0\\\r"
send " -f 'broadcast and ether\\\r"
send " host 00:00:00:00:00:00'\\\r"
send " -T fields -E header=y -\\\r"
send "E separator=, -E quote=d\\\r"
send " -e frame.time_epoch -e \\\r"
send "wlan.sa -e wlan_mgt.supp\\\r"
send "orted_rates -e wlan_mgt.\\\r"
send "extented_supported_rates\r"