我正在尝试使用python telnetlib自动执行任务。这是我的代码:
import telnetlib
t = telnetlib.Telnet(self._ip_addr)
# t.connect()
if not t.expect(["User Name : "], timeout=10): # Expect username prompt
raise Exception('No login prompt is received')
t.write(self._username + '\r\n') # Send username
if not t.expect(["Password : "], timeout=10): # Expect password prompt
raise Exception('No password prompt is received')
t.write(self._password + '\r\n') # send password
t.set_debuglevel(1)
if not t.read_until("apc>", timeout=10): # Expect prompt
raise Exception('No command prompt is received')
import time
time.sleep(2)
t.write("vt100\n")
t.write("olOff 9\n")
# t.close()
此代码的输出如下:
Telnet(10.130.210.71,23): recv '*'
Telnet(10.130.210.71,23): recv '****'
Telnet(10.130.210.71,23): recv '*'
Telnet(10.130.210.71,23): recv '\r\n\r\nAmerican Power Conversion Networ'
Telnet(10.130.210.71,23): recv 'k Management Card AOS v5.1.4\r\n'
Telnet(10.130.210.71,23): recv '(c) Copyright 2009 All Rights Reserved RPDU 2g '
Telnet(10.130.210.71,23): recv ' v5.1.2\r\n-------------------'
Telnet(10.130.210.71,23): recv '--------------------------------------------------'
Telnet(10.130.210.71,23): recv '----------\r\nName : Unknown '
Telnet(10.130.210.71,23): recv ' Date : 01/10/2011\r\nContact : Unk'
Telnet(10.130.210.71,23): recv 'nown Time : 06:1'
Telnet(10.130.210.71,23): recv '5:31\n\r'
Telnet(10.130.210.71,23): recv 'Location : Unknown '
Telnet(10.130.210.71,23): recv ' User : Administrator\n\r'
Telnet(10.130.210.71,23): recv 'Up Time : 85 Days 7 Hours 30 Minutes '
Telnet(10.130.210.71,23): recv ' Stat : P+ N4+ N6+ A+\r\n\r\n'
Telnet(10.130.210.71,23): recv '\r\nType ? for command listing'
Telnet(10.130.210.71,23): recv '\r\nUse tcpip command for IP address(-i), subnet(-s)'
Telnet(10.130.210.71,23): recv ', and gateway(-g)\r\n\r\n'
Telnet(10.130.210.71,23): recv 'apc>'
Telnet(10.130.210.71,23): send 'vt100\n'
Telnet(10.130.210.71,23): send 'olOff 9\n'
我正在Windows上执行此代码。这里的问题是最后一个命令没有被执行。
任何人都可以建议我在哪里做错了吗?
任何人都可以建议我
答案 0 :(得分:0)
评论中的答案,结尾缺少:
t.read_until("apc>", timeout=10)