我想創建一個expect腳本將發送基於不同的密碼字符串的「期望」做出決策期望返回
條件A:如果思科設備尚未安裝使用用戶名那麼第一個提示將簡單地爲「密碼:」 - 那麼它應該使用passwordA(沒有用戶名)
條件B:如果它已經用用戶名設置,則提示符將是「用戶名:」,後跟「密碼:「 - 那麼它應該使用用戶名和密碼B
#!/bin/bash
# Declare host variable as the input variable
host=$1
# Start the expect script
(expect -c "
set timeout 20
# Start the session with the input variable and the rest of the hostname
spawn telnet $host
set timeout 3
if {expect \"Password:\"} {
send \"PasswordA\"}
elseif { expect \"Username:\"}
send \"UsersName\r\"}
expect \"Password:\"
log_user 0
send \"PasswordB\r\"
log_user 1
expect \"*>\"
# send \"show version\r\"
# set results $expect_out(buffer)
#expect \"Password:\"
#send \"SomeEnablePassword\r\"
# Allow us to interact with the switch ourselves
# stop the expect script once the telnet session is closed
send \"quit\r\"
expect eof
")
問題不是很清楚 –
如果您使用單引號括住期望腳本本身,而不是雙引號,那麼您會讓代碼更加惱火。需要更少的反斜槓...... –