[注意:我已经在物理机上运行了这段代码,它可以工作(OS=Fedora 14)。但是当我在虚拟机上安装的 Ubuntu 中使用相同的脚本时,它不起作用。我正在尝试捕获 ifconfig 输出并保存/打印它。]
我已经在 VMPlayer 上安装了 Ubuntu(它是虚拟机)。我正在使用 python 脚本语言并尝试使用 Pexpect 自动化 SSH 连接。我在同一个 Ubuntu 虚拟机上运行脚本。下面给出的是我正在使用这个脚本做的事情:
- 使用 Pexpect,通过 SSH 连接到 localhost
- 执行“ifconfig”cli 命令并捕获输出。
- 打印此 cli 输出。
代码片段:
class Linux:
HOSTIP = ""
USER = ""
PASSWD = ""
HOST = ""
PROMPTTIMEOUT = 120
PROMPT = ""
ERRORFLAG = False
ERRORMSG = ""
CLIOUTPUT = ""
def __init__(self,host, user, passwd, prompt):
self.HOSTIP = host
self.USER = user
self.PASSWD = passwd
self.PROMPT = prompt
self.login()
def login(self):
try:
connection = "ssh " + self.USER + "@" + self.HOSTIP
self.HOST = pexpect.spawn(connection)
while 1:
returnNum = self.HOST.expect(["yes/no","password:"],120)
if returnNum == 0:
self.HOST.send("yes\r\n")
elif returnNum == 1:
self.HOST.send(self.PASSWD + "\r\n")
break
self.HOST.expect(self.PROMPT,120)
self.CLIOUTPUT = self.HOST.before
self.HOST.send("\r\n")
self.HOST.expect(self.PROMPT,120)
self.CLIOUTPUT = self.CLIOUTPUT + self.HOST.before
except Exception:
return False
return True
def executeCLI(self, cmd):
self.resetErrors()
try:
self.HOST.send("\r\n")
self.HOST.expect(self.PROMPT,120)
self.HOST.send(cmd + "\r\n")
self.HOST.expect(self.PROMPT,120)
self.CLIOUTPUT = self.HOST.before
self.HOST.send("\r\n")
self.HOST.expect(self.PROMPT,120)
self.CLIOUTPUT = self.CLIOUTPUT + self.HOST.before
except Exception:
return False
return True
def getCLIOutput(self):
return self.CLIOUTPUT
# Testing Class Linux.py
host = Linux("1.1.1.1", "admin", "admin123", "@ubuntu")
host.executeCLI("ifconfig")
print("Output : " + str(host.getCLIOutput()))
结果输出:
admin@ubuntu12:/home/Host# python Linux.py
Output : 12: ~admin12:~$
admin@ubuntu12:/home/Host#
预期输出:
admin@ubuntu12:/home/Host# ifconfig
eth0 Link encap:Ethernet HWaddr 00:0c:29:60:6b:be
inet addr:192.168.2.5 Bcast:192.168.2.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fe60:6bbe/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:1733 errors:0 dropped:0 overruns:0 frame:0
TX packets:1539 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:205502 (205.5 KB) TX bytes:252126 (252.1 KB)
Interrupt:19 Base address:0x2024
eth1 Link encap:Ethernet HWaddr 00:0c:29:60:6b:c8
inet addr:20.20.20.2 Bcast:20.20.20.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fe60:6bc8/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:49 errors:0 dropped:0 overruns:0 frame:0
TX packets:82 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:5132 (5.1 KB) TX bytes:12322 (12.3 KB)
Interrupt:16 Base address:0x20a4
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:170 errors:0 dropped:0 overruns:0 frame:0
TX packets:170 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:27353 (27.3 KB) TX bytes:27353 (27.3 KB)
admin@ubuntu12:/home/Host#
如果我手动执行 SSH 登录到 LOCALHOST 并在 CLI 上执行 IFCONFIG 命令,那么我会在屏幕上看到以下输出:
admin@ubuntu12:/home/Host# ssh admin@192.168.2.5
admin@192.168.2.5's password:
Welcome to Ubuntu 12.04.3 LTS (GNU/Linux 3.2.0-29-generic-pae i686)
* Documentation: https://help.ubuntu.com/
67 packages can be updated.
35 updates are security updates.
Last login: Sat Sep 21 22:55:36 2013 from 192.168.2.5
admin@ubuntu12:~$ ifconfig
eth0 Link encap:Ethernet HWaddr 00:0c:29:60:6b:be
inet addr:192.168.2.5 Bcast:192.168.2.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fe60:6bbe/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:4270 errors:0 dropped:0 overruns:0 frame:0
TX packets:3664 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:463331 (463.3 KB) TX bytes:625129 (625.1 KB)
Interrupt:19 Base address:0x2024
eth1 Link encap:Ethernet HWaddr 00:0c:29:60:6b:c8
inet addr:20.20.20.2 Bcast:20.20.20.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fe60:6bc8/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:153 errors:0 dropped:0 overruns:0 frame:0
TX packets:105 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:15340 (15.3 KB) TX bytes:14388 (14.3 KB)
Interrupt:16 Base address:0x20a4
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:1456 errors:0 dropped:0 overruns:0 frame:0
TX packets:1456 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:192908 (192.9 KB) TX bytes:192908 (192.9 KB)
admin@ubuntu12:~$