歡迎您光臨本站 註冊首頁

expect使用問題,跪求指導!!!!

←手機掃碼閱讀     火星人 @ 2014-03-03 , reply:0

expect使用問題,跪求指導!!!!


以下是我寫的一個測試用的腳本:
#!/usr/bin/expect -f
set passwd
set ipaddress
set timeout 10

spawn ssh -q alne@$ipaddress
expect {
    "yes/no" { send "yes\r";exp_continue }
    "password:" { send "$passwd\r" }
}
expect "\$"
send "uptime \r"
send "exit \r"
expect eof
exit

腳本執行到登陸遠程機器以後就會停住,

和set timeout 設置的時間有關係,我要是設置10秒,他就會等10秒才會執行 send后的命令,設置100秒他就等100秒才執行send命令。

沒用過啊,痛苦啊,這是怎麼回事,跪求回復啊
《解決方案》

#!/usr/bin/env python

"""
This runs a command on a remote host using SSH. At the prompts enter hostname,
user, password and the command.
"""

import pexpect
import getpass, os

#user: ssh 主機的用戶名
#host:ssh 主機的域名
#password:ssh 主機的密碼
#command:即將在遠端 ssh 主機上運行的命令
def ssh_command (user, host, password, command):
    """
    This runs a command on the remote host. This could also be done with the
    pxssh class, but this demonstrates what that class does at a simpler level.
    This returns a pexpect.spawn object. This handles the case when you try to
    connect to a new host and ssh asks you if you want to accept the public key
    fingerprint and continue connecting.
    """
    ssh_newkey = 'Are you sure you want to continue connecting'
    # 為 ssh 命令生成一個 spawn 類的子程序對象.
    child = pexpect.spawn('ssh -l %s %s %s'%(user, host, command))
    i = child.expect()
    # 如果登錄超時,列印出錯信息,並退出.
    if i == 0: # Timeout
        print 'ERROR!'
        print 'SSH could not login. Here is what SSH said:'
        print child.before, child.after
        return None
    # 如果 ssh 沒有 public key,接受它.
    if i == 1: # SSH does not have the public key. Just accept it.
        child.sendline ('yes')
        child.expect ('password: ')
        i = child.expect()
        if i == 0: # Timeout
        print 'ERROR!'
        print 'SSH could not login. Here is what SSH said:'
        print child.before, child.after
        return None
    # 輸入密碼.
    child.sendline(password)
    return child

def main ():
    # 獲得用戶指定 ssh 主機域名.
    host = raw_input('Hostname: ')
    # 獲得用戶指定 ssh 主機用戶名.
    user = raw_input('User: ')
    # 獲得用戶指定 ssh 主機密碼.
    password = getpass.getpass()
    # 獲得用戶指定 ssh 主機上即將運行的命令.
    command = raw_input('Enter the command: ')
    child = ssh_command (user, host, password, command)
    # 匹配 pexpect.EOF
    child.expect(pexpect.EOF)
    # 輸出命令結果.
    print child.before

if __name__ == '__main__':
    try:
        main()
    except Exception, e:
        print str(e)
        traceback.print_exc()
        os._exit(1)


看下pexpect也能實現。
參考URL http://www.ibm.com/developerworks/cn/linux/l-cn-pexpect2/
《解決方案》

#!/usr/bin/expect -f
set passwd
set ipaddress
set timeout 10

spawn ssh -q alne@$ipaddress
expect {
    "yes/no" { send "yes\r";exp_continue }
    "password:" { send "$passwd\r" }
}

set timeout 1 # 這裡重新定義timeout 時間就可以了,笨辦法
expect "\$"
send "uptime \r"
send "exit \r"
expect eof
exit
《解決方案》

同問坐等!http://www.mohuanshouji2shaniuguilai.com
《解決方案》

回復 2# steveneast


    可是下面在執行命令的時候等待時間是按上面的10秒還是下面的1秒呢?
《解決方案》

set  timeout  -1     只有在成功expect ,就會send 。
《解決方案》

回復 5# hongjie7456


    還是不行
《解決方案》

#!/usr/local/bin/expect -f
set passwd
set ipaddress
set timeout 10

spawn ssh -l bizcard $ipaddress
expect {
    "yes/no" { send "yes\r";exp_continue }
    "password:" { send "$passwd\r" }
}
expect "*\$"
send "uptime\r"
expect "*\$"
send "exit\r"
expect eof
exit
試試這個,應該是沒問題了。
《解決方案》

超時時間設置短一點不可以么?
《解決方案》

回復 9# xiaodylan


    那樣的話如果執行大點的腳本或是安裝軟體的話就會因為超時自動斷開

[火星人 ] expect使用問題,跪求指導!!!!已經有701次圍觀

http://coctec.com/docs/service/show-post-273.html