我正在稳步研究如何通过运行命令来 ssh 和解析设备上的数据。在这一努力的过程中,我遇到了一些问题,并且我提出的问题得到了很多帮助。我现在正在使用 pexpect,但我在文档中没有看到太多与我正在做的事情相关的内容。基本上我需要 ssh 输入,正如我所说,然后运行一个命令来打印数据,然后将该数据打印到我的控制台。
这是我的代码:
import pexpect
import pxssh
import getpass
child = pexpect.spawn('ssh www.example.com')
password = getpass.getpass('password: ')
child.sendline ('foo bar')
data = (child.read_nonblocking(size=1000, timeout=100))
print data
输出:
password:
foo bar
在 foo bar
命令中,打印输出的第一行是 foo bar
所以我想知道这是否试图打印此数据但只打印第一行。我添加了 read_nonblocking(size=1000, timeout=100)
尝试将大小设置为更大,并设置超时以让数据打印。
使用 PXSSH 更新
我还尝试使用 pxssh 示例来执行此操作,并仅获取 foo
可以运行的命令列表。我需要从 foo bar 中打印出来,这是配置列表。 我的猜测是你不能使用带有空格的命令?这是我尝试过的代码:
import pxssh
import getpass
try:
s = pxssh.pxssh()
s.force_password = True
hostname = raw_input('hostname: ')
username = raw_input('username: ')
password = getpass.getpass('password: ')
s.login (hostname, username, password)
s.sendline ('foo bar') # run a command
s.prompt() # match the prompt
print s.before # print everything before the prompt.
s.logout()
except pxssh.ExceptionPxssh, e:
print "pxssh failed on login."
print str(e)
这让我在控制台中返回:
pxssh failed on login.
could not set shell prompt
:
Session idle time out is disabled
SSH> unset PROMPT_COMMAND
Error - Command [unset PROMPT_COMMAND] not found.
foo [ bar | bart | ran | up
| cmd | bee | hvac | monkey
| selective | list | help ]
check[v,nv,beep] [ list | help ]
delete [ all | bee | neewb | stuff
| up | cmd | fooconfig | root
| app | list | hvac | monkey
| selective | <filename> | confirmed | list | help ]
exit [ help ]
get [ vcf | nvcf | snmpcf | help ] [<filename>]
verbose [ help ]
help [ <command> | help ]
up arrow - brings up old command lines
down arrow - brings up newer command lines
right arrow - moves cursor to the right
left arrow - moves cursor to the left
insert - inserts a space at the cursor
delete - deletes character at the cursor
SSH> PS1='[PEXPECT]\$ '
Error - Command [PS1='[PEXPECT]\$ '] not found.
foo [ bar | bart | ran | up
| cmd | bee | hvac | monkey
| selective | list | help ]
check[v,nv,beep] [ list | help ]
delete [ all | bee | neewb | stuff
| up | cmd | fooconfig | root
| app | list | hvac | monkey
| selective | <filename> | confirmed | list | help ]
exit [ help ]
get [ vcf | nvcf | snmpcf | help ] [<filename>]
verbose [ help ]
help [ <command> | help ]
up arrow - brings up old command lines
down arrow - brings up newer command lines
right arrow - moves cursor to the right
left arrow - moves cursor to the left
insert - inserts a space at the cursor
delete - deletes character at the cursor
正如我提到的,我只是想让控制台打印出 foo bar
命令配置。这是我在发现我需要在较旧的 Python 2.4
中工作之前使用 python-exscript
的代码。
我需要在 EXSCRIPT 中执行的代码
account = read_login()
conn = SSH2()
conn.connect('example.com')
conn.login(account)
conn.execute('foo bar')
data = conn.response
conn.send('exit\r')
conn.close()
print data
非常感谢有关如何使此代码工作的任何帮助!谢谢!
最佳答案
解决了这个问题。我缺少 s.prompt()
try:
s = pxssh.pxssh(timeout=60, maxread=2000000)
s.force_password = True
hostname = raw_imput('hostname: ')
username = raw_input('password: ')
password = getpass.getpass('password: ')
s.PROMPT= 'SSH> '
s.login (hostname, username, password, auto_prompt_reset=False)
s.prompt()
s.sendline('foo bar')
s.prompt()
data = s.before
print data
s.logout()
except pxssh.ExceptionPxssh, e:
print "pxssh failed on login."
print str(e)
关于python - 似乎无法让 pexpect 从命令打印数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21268497/