TCL期望在登录提示下发送$ expect_out(缓冲区)

时间:2013-06-25 06:18:16

标签: tcl expect

Expect在提示输入用户凭据而不是指定的变量($ user)时发送$ expect_out(缓冲区)。然后它超时了。以下是代码:

...
    }
    "yes" {
        send_user "\nEnter your username for the WLC supporting the new APs:\n"
        sleep 6
    }
}
expect {
    -re "(.*)\n" {
        set user $expect_out(1,string)
    }
}
send_user "\nEnter your password for the WLC supporting the new APs:\n"
sleep 15
expect {
    -re "(.*)\n" {
        set pass $expect_out(1,string)
    }
}
sleep 1
send_user "\nSshing to the IP of the WLC supporting the new APs ($wlc_temp)\n"
spawn ssh $wlc_temp

expect {
    "User:" {
        send $user\n
        sleep 1
    }
}
expect {
    "assword:" {
        send $pass\n
        sleep 1
    }
}

结果如下:

  

(设备主机名)用户:(设备主机名)   用户:

以下是调试:

  

支持新AP的WLC的IP()   产卵ssh   parent:等待同步字节   父母:告诉孩子继续前进   parent:现在与子spawn不同步:return {7153}

     

期望:“”(spawn_id exp6)匹配glob模式“User:”吗?无

     

期望:“\ r \ n”(spawn_id exp6)匹配glob模式“User:”吗?没有   (设备主机名)用户:

     

期望:“\ r \ n(设备主机名)用户:”   (spawn_id exp6)匹配glob模式“User:”?是   expect:set expect_out(0,string)“User:”expect:set expect_out(spawn_id)“exp6”   expect:set expect_out(buffer)“\ r \ n(设备主机名)用户:”   发送:将“\ n”发送到{exp6}

     

期望:“”(spawn_id exp6)匹配glob模式“assword:”吗?无

     

(设备主机名)用户:

     

期望:“\ r \ n(设备主机名)用户:”(spawn_id exp6)匹配glob   模式“assword:”?没有   期待:超时

更新 将请求用户输入的语句重新定位到脚本顶部作为解决方法。

1 个答案:

答案 0 :(得分:1)

如果您想阅读用户的输入,则应使用expect_user代替expect

        send_user "\nEnter your username for the WLC supporting the new APs:\n"
        sleep 6
    }
}
expect_user {
    -re "(.*)\n" {
        set user $expect_out(1,string)
    }
}
send_user "\nEnter your password for the WLC supporting the new APs:\n"
sleep 15
expect_user {
    -re "(.*)\n" {
        set pass $expect_out(1,string)
    }
}
sleep 1
send_user "\nSshing to the IP of the WLC supporting the new APs ($wlc_temp)\n"

# ...

(我还认为你可以在那里使用较少的sleep次呼叫,而只是调整超时,但这不太可能对正确的脚本操作产生影响。)