2016-06-24 14 views
0

SSH経由でリモートサーバーに接続するためにSpurを使用しようとしています。私は 'ps'と 'aux'引数を与えてもうまく動作しますが、うまくいきます。 私は 'PS'、 'AUX'、与える場合は '|' それは私にエラーPythonを使用してSSHに接続

spur.results.RunProcessError: return code: 1 
output: b'' 
stderr output: b'ERROR: Garbage option.\n********* simple selection ********* ********* selection by list *********\n-A all processes      -C by command name\n-N negate selection     -G by real group ID (supports names)\n-a all w/ tty except session leaders -U by real user ID (supports names)\n-d all except session leaders   -g by session OR by effective group name\n-e all processes      -p by process ID\nT all processes on this terminal  -s processes in the sessions given\na all w/ tty, including other users -t by tty\ng OBSOLETE -- DO NOT USE    -u by effective user ID (supports names)\nr only running processes    U processes for specified users\nx processes w/o controlling ttys  t by tty\n*********** output format ********** *********** long options ***********\n-o,o user-defined -f full   --Group --User --pid --cols --ppid\n-j,j job control s signal   --group --user --sid --rows --info\n-O,O preloaded -o v virtual memory --cumulative --format --deselect\n-l,l long   u user-oriented --sort --tty --forest --version\n-F extra full X registers  --heading --no-heading --context\n     ********* misc options *********\n-V,V show version  L list format codes f ASCII art forest\n-m,m,-L,-T,H threads S children in sum -y change -l format\n-M,Z security data  c true command name -c scheduling class\n-w,w wide output  n numeric WCHAN,UID -H process hierarchy\n' 

私のコードを与え、 'グレップ' と 'javaの' は以下のとおりです。

import spur 

shell = spur.SshShell(hostname='hostname', 
         username='uname', 
         password='password') 

with shell: 
    result = shell.run(['ps', 'aux', '| grep java']) 
print result.output 

を助けてください私の場合、結果には

import spur 

shell = spur.SshShell(hostname='host', 
         username='user', 
         password='password') 

with shell: 
    result = shell.run(['sh','-c','ps aux | grep java']) 
print result.output 

:私は誰もが

答えて

1

それは次のようにして動作するはずです同様の問題に直面している場合次のとおりです。

user 5090 0.0 0.0 9632 2472 ?  Ss 04:06 0:00 sh -c ps aux | grep java 
user 5093 0.0 0.0 9116 904 ?  S 04:06 0:00 grep java 
0

ありがとうございました。 Pexpectを使った後も同じ結果が得られました。ここに私のコード。他のユーザーに役立つことを願っています。

def connect(): 
    s = pxssh.pxssh() 
    hostname = 'host' 
    username = 'name' 
    password = 'password' 

    if not s.login(hostname, username, password): 
     print 'SSH login failed' 
     print str(s) 
    else: 
     print 'SSH ok' 
     s.sendline(' ps -eo pid,cmd,etime | grep java') 
     s.prompt() 
     arr = s.before.split('\n') 
     arr_1 = arr[1].split(' ') 
     print 'Uptime for the following process is ', arr_1[-1] 
     s.logout() 
+0

[Fabric](https://github.com/fabric/fabric)もご覧ください。 –

関連する問題