3.4 中的预期输出不是预期的有一些 \b\n 值。虽然适用于 2.7
pexpect output in 3.4 not as intended has some \b\n values. Works well with 2.7 though
from pexpect import pxssh
import getpass
try:
s = pxssh.pxssh()
hostname = input('hostname: ')
username = input('username: ')
password = getpass.getpass('password: ')
s.login(hostname, username, password)
s.sendline('uptime') # run a command
s.prompt() # match the prompt
print(s.before) # print everything before the prompt.
s.sendline('ls -l')
s.prompt()
print(s.before)
s.sendline('df')
s.prompt()
print(s.before)
s.logout()
except pxssh.ExceptionPxssh as e:
print("pxssh failed on login.")
print(e)
下面是 o/p:
b'uptime\r\n 22:27:16 up 45 min, 4 users, load average: 0.02, 0.05, 0.00\r\n'
b'ls -l\r\ntotal 60\r\ndrwxr-xr-x 2 pi pi 4096 Mar 3 16:25 \x1b[0m\x1b[01;34mDesktop\x1b[0m\r\ndrwxr-xr-x 6 pi pi 4096 Apr 5 13:13 \x1b[01;34mDocuments\x1b[0m\r\ndrwxr-xr-x 3 pi pi 4096 Apr 13 05:51 \x1b[01;34mDownloads\x1b[0m\r\ndrwxr-xr-x 2 pi pi 4096 Mar 3 16:25 \x1b[01;34mMusic\x1b[0m\r\ndrwxr-xr-x 3 pi pi 4096 Jun 14 15:00 \x1b[01;34moldconffiles\x1b[0m\r\n-rw-r--r-- 1 pi pi 592 Jun 23 22:26 pexpected_ssh.py\r\ndrwxr-xr-x 4 pi pi 4096 Apr 13 06:55 \x1b[01;34mphantomjs-on-raspberry\x1b[0m\r\ndrwxr-xr-x 2 pi pi 4096 Mar 3 16:25 \x1b[01;34mPictures\x1b[0m\r\ndrwxr-xr-x 2 pi pi 4096 Mar 3 16:25 \x1b[01;34mPublic\x1b[0m\r\ndrwxr-xr-x 6 pi pi 4096 Apr 17 20:30 \x1b[01;34mPytest1\x1b[0m\r\ndrwxr-xr-x 2 pi pi 4096 Apr 11 21:56 \x1b[01;34mpython1\x1b[0m\r\ndrwxr-xr-x 2 pi pi 4096 Mar 3 15:55 \x1b[01;34mpython_games\x1b[0m\r\ndrwxr-xr-x 2 pi pi 4096 Mar 3 16:25 \x1b[01;34mTemplates\x1b[0m\r\ndrwxr-xr-x 2 pi pi 4096 Apr 11 14:57 \x1b[01;34mtest\x1b[0m\r\ndrwxr-xr-x 2 pi pi 4096 Mar 3 16:25 \x1b[01;34mVideos\x1b[0m\r\n'
b'df\r\nFilesystem 1K-blocks Used Available Use% Mounted on\r\n/dev/root 29710268 6264956 22178580 23% /\r\ndevtmpfs 468148 0 468148 0% /dev\r\ntmpfs 472756 0 472756 0% /dev/shm\r\ntmpfs 472756 6504 466252 2% /run\r\ntmpfs 5120 4 5116 1% /run/lock\r\ntmpfs 472756 0 472756 0% /sys/fs/cgroup\r\n/dev/mmcblk0p1 63503 21390 42113 34% /boot\r\ntmpfs 94552 0 94552 0% /run/user/1000\r\n/dev/sda1 15289328 2940992 12348336 20% /media/pi/SHUBHAYAN1\r\n'
pi@raspberrypi:~ $
Python 3.x s.before
是 bytes
而不是 str
。您需要将 bytes
转换为 str
:
v = s.before.decode()
print(v)
decode()
的默认 编码 是 utf-8
。如果不在 utf-8
中,您需要指定正确的 encoding。例如:
v = s.before.decode(encoding='latin1')
有关详细信息,请参阅 Python3 的 Unicode HOWTO。
from pexpect import pxssh
import getpass
try:
s = pxssh.pxssh()
hostname = input('hostname: ')
username = input('username: ')
password = getpass.getpass('password: ')
s.login(hostname, username, password)
s.sendline('uptime') # run a command
s.prompt() # match the prompt
print(s.before) # print everything before the prompt.
s.sendline('ls -l')
s.prompt()
print(s.before)
s.sendline('df')
s.prompt()
print(s.before)
s.logout()
except pxssh.ExceptionPxssh as e:
print("pxssh failed on login.")
print(e)
下面是 o/p:
b'uptime\r\n 22:27:16 up 45 min, 4 users, load average: 0.02, 0.05, 0.00\r\n'
b'ls -l\r\ntotal 60\r\ndrwxr-xr-x 2 pi pi 4096 Mar 3 16:25 \x1b[0m\x1b[01;34mDesktop\x1b[0m\r\ndrwxr-xr-x 6 pi pi 4096 Apr 5 13:13 \x1b[01;34mDocuments\x1b[0m\r\ndrwxr-xr-x 3 pi pi 4096 Apr 13 05:51 \x1b[01;34mDownloads\x1b[0m\r\ndrwxr-xr-x 2 pi pi 4096 Mar 3 16:25 \x1b[01;34mMusic\x1b[0m\r\ndrwxr-xr-x 3 pi pi 4096 Jun 14 15:00 \x1b[01;34moldconffiles\x1b[0m\r\n-rw-r--r-- 1 pi pi 592 Jun 23 22:26 pexpected_ssh.py\r\ndrwxr-xr-x 4 pi pi 4096 Apr 13 06:55 \x1b[01;34mphantomjs-on-raspberry\x1b[0m\r\ndrwxr-xr-x 2 pi pi 4096 Mar 3 16:25 \x1b[01;34mPictures\x1b[0m\r\ndrwxr-xr-x 2 pi pi 4096 Mar 3 16:25 \x1b[01;34mPublic\x1b[0m\r\ndrwxr-xr-x 6 pi pi 4096 Apr 17 20:30 \x1b[01;34mPytest1\x1b[0m\r\ndrwxr-xr-x 2 pi pi 4096 Apr 11 21:56 \x1b[01;34mpython1\x1b[0m\r\ndrwxr-xr-x 2 pi pi 4096 Mar 3 15:55 \x1b[01;34mpython_games\x1b[0m\r\ndrwxr-xr-x 2 pi pi 4096 Mar 3 16:25 \x1b[01;34mTemplates\x1b[0m\r\ndrwxr-xr-x 2 pi pi 4096 Apr 11 14:57 \x1b[01;34mtest\x1b[0m\r\ndrwxr-xr-x 2 pi pi 4096 Mar 3 16:25 \x1b[01;34mVideos\x1b[0m\r\n'
b'df\r\nFilesystem 1K-blocks Used Available Use% Mounted on\r\n/dev/root 29710268 6264956 22178580 23% /\r\ndevtmpfs 468148 0 468148 0% /dev\r\ntmpfs 472756 0 472756 0% /dev/shm\r\ntmpfs 472756 6504 466252 2% /run\r\ntmpfs 5120 4 5116 1% /run/lock\r\ntmpfs 472756 0 472756 0% /sys/fs/cgroup\r\n/dev/mmcblk0p1 63503 21390 42113 34% /boot\r\ntmpfs 94552 0 94552 0% /run/user/1000\r\n/dev/sda1 15289328 2940992 12348336 20% /media/pi/SHUBHAYAN1\r\n'
pi@raspberrypi:~ $
Python 3.x s.before
是 bytes
而不是 str
。您需要将 bytes
转换为 str
:
v = s.before.decode()
print(v)
decode()
的默认 编码 是 utf-8
。如果不在 utf-8
中,您需要指定正确的 encoding。例如:
v = s.before.decode(encoding='latin1')
有关详细信息,请参阅 Python3 的 Unicode HOWTO。