Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash 发送<;命令>;,条件测试的期望值?_Bash_Tcl_Expect - Fatal编程技术网

Bash 发送<;命令>;,条件测试的期望值?

Bash 发送<;命令>;,条件测试的期望值?,bash,tcl,expect,Bash,Tcl,Expect,我有一个包在bash脚本中的expect脚本,用于根据是否从Cisco路由器提取IP地址执行特定命令。我试图编写的算法如下所示: send -h "ip int br" expect -re { if "192\.[0-9]{1,3}\.{2}[0-9]{1,3}" { expect { "up/up { do these things here } "down/down" { do this in

我有一个包在bash脚本中的
expect
脚本,用于根据是否从Cisco路由器提取IP地址执行特定命令。我试图编写的算法如下所示:

send -h "ip int br"
expect -re {
    if "192\.[0-9]{1,3}\.{2}[0-9]{1,3}"
    {
        expect 
        {
            "up/up { do these things here }
            "down/down" { do this instead }
        }
    }
    else
    {
        send -h "<another command>"
        expect
        {
            "up/up" { do this }
            "down/down" { or this if down }
        }
    }
}
send-h“ip int br”
期望-再{
如果“192\[0-9]{1,3}.{2}[0-9]{1,3}”
{
期待
{
“向上{在这里做这些事情}”
“向下/向下”{改为这样做}
}
}
其他的
{
发送-h”),但还没有任何结果


有什么想法吗?

我会捕获返回的地址,然后对其执行操作。我看到重复的代码,所以请尽量减少:

send -h "ip int br"
expect {
    timeout {error "can't find an ip address after 'ip int br' command"}
    -re {\m\d{1,3}(\.\d{1,3}){3}\M}
}
if { ! [regexp {192\.[0-9]{1,3}\.{2}[0-9]{1,3}} $expect_out(0,string)]} {
    send -h "<another command>"
}
expect {
    "up/up" { do these things here }
    "down/down" { do this instead }
}
send-h“ip int br”
期待{
超时{错误“在'ip int br'命令之后找不到ip地址”}
-re{\m\d{1,3}(\.\d{1,3}){3}\m}
}
如果{![regexp{192\[0-9]{1,3}\.{2}[0-9]{1,3}}$expect_out(0,string)]}{
发送-h“”
}
期待{
“up/up”{在这里做这些事情}
“向下/向下”{改为这样做}
}
其他说明:

  • 注意不要将块的开始括号放在它自己的行上,而是用命令抱住它
  • 将正则表达式放在{brages}中,因为这是
    “192\.[0-9]{1,3}.{2}[0-9]{1,3}”
    将给您一个类似
    无效命令名“0-9”
    的错误--括号是Tcl特有的,除非它们在大括号中,否则需要转义

通常最好在远程命令完成后专门检测提示,而不是依赖超时。但除此之外,它看起来相当不错。问题:为什么在最后一个expect块中,我的命令运行,但没有显示在我的控制台窗口(终端)中?为了测试,我使用“向下/向下”重新启动,因此,
改为执行此操作
看起来像
{0:01重新启动;退出}
。尽管此命令已由路由器注册(在另一个终端中,我正在ping路由器,可以看到它在一分钟后按照命令退出),它没有像
sh ip int br
那样打印到我的终端。这是为什么?还有:当使用退出代码时,退出脚本的最佳方式是什么,即我是否仍然需要
expect\u eof
语句?在
发送重新启动后,尝试
expect eof
而不是
退出
expect