2014-01-21 16 views
1

我正在穩步地研究如何通過運行命令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 

OUTPUT:

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-exscript之前發現我需要在較舊的Python 2.4中工作的代碼。

CODE,我不得不遊走於EXSCRIPT,我需要Pexpect的TO DO

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 

如何獲得此代碼工作的任何幫助,不勝感激!謝謝!

+0

read_nonblocking的文檔字符串表示超時不是你做你覺得它在做什麼:「超時僅指的時間至少讀一個字符量這不是由影響。 'size'參數,所以如果你調用read_nonblocking(size = 100,timeout = 30)並且只有一個字符可用,那麼一個字符會立即返回,它將不會等待30秒, 「。 –

+0

使用pexpect的常用方法是'child.expect(x)',其中x是輸出的* end *處打印的內容(例如下一個提示符),然後從'child.before '。 –

+0

@ThomasK哦,好吧,那麼你怎麼用'pexpect'運行一個命令並從那裏得到輸出? – tjoenz

回答

3

找出問題所在。我缺少一個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) 
+0

你需要1個還是每個發送線? –