2017-11-01 4 views
0

おはようございます。だから私は仕事のためにこの問題に固執しています。 Paramikoライブラリを使用して、DB2インスタンスの多くの人が仕事で行う簡単な作業を自動化しようとしています。私はすでに1つの仕事をパスワードの束をリセットするように設定していたので、私は、サーバーへの接続の基本が正しいことを知っているので、これらのコマンドの問題は私が欲しいことをしていない。私がしようとしているのは、 "bjobs"の2番目のコマンドの後に出力を表示できるようにしたいということです。私はstdout.read()とこれまでのところ、私に何も与えていないb "'を使ってみました。どんな助けも非常に必要です。パラミコでSASの求人一覧(PYTHON)

from paramiko import client 
from os import getlogin 

class ssh: 
client = None 

def __init__(self, address, username, password): 
    print("Connecting to server") 
    self.client = client.SSHClient() 
    self.client.set_missing_host_key_policy(client.AutoAddPolicy()) 
    self.client.connect(address, username=username, password=password) 
    print("Connected to " + address) 

def sendCommand(self, command): 
    if(self.client): 
     stdin, stdout, stderr = self.client.exec_command(command) 
     x= stdout.read() 
     print(x) 
     while not stdout.channel.exit_status_ready(): 
      if stdout.channel.recv_ready(): 
       alldata = stdout.channel.recv(1024) 
       while stdout.channel.recv_ready(): 
        alldata+=stdout.channel.recv(1024) 
       print(str(alldata, 'utf8')) 
    else: 
     print("connection not opened") 


serverCon = "My Server" 
plist = [] 
currPass = 'MyPassword!' 

#get user information 
userName = getlogin() 

#Connect to server, insert and chnage passwords 

connection = ssh(serverCon, userName, currPass) 
connection.sendCommand(r'. /opt/sas/lsf/conf/profile.lsf') 

connection.sendCommand('bjobs') 

答えて

1

すべてexec_command()ので、あなたの最初の. /opt/sas/lsf/conf/profile.lsfは、次のbjobsに影響を与えないシェルの新しいインスタンス(SSHサーバー上のユーザーのログインシェル)でコマンドを実行します。

クラスparamiko.client.SSHClient

:あなたは、基本的には、マニュアルによると

ssh [email protected] '. /opt/sas/lsf/conf/profile.lsf; bjobs' 

と同じである

exec_command('. /opt/sas/lsf/conf/profile.lsf; bjobs') 

を書く必要があります

exec_command(command, bufsize=-1, timeout=None, get_pty=False)

SSHサーバーでコマンドを実行します。 新しいチャネルが開き、が要求されたコマンドが実行されます。コマンドの入力ストリームと出力ストリームは、 stdin、stdout、およびstderrを表すPythonのファイルライクオブジェクトとして返されます。

クラスparamiko.channel.Channel

exec_command(*args, **kwds)

サーバ上でコマンドを実行します。サーバーが許可している場合、チャネルは実行中のコマンドのstdin、stdout、stderrの に直接接続されます。 コマンドの実行が終了すると、チャネルは閉じられ、再利用できなくなります。別のコマンドを実行するには、新しいチャネルを開く必要があります。

+0

あなたは今あなたをどれだけ愛しているかわかりません。ちょうどSSHを勉強しようとしていますので、私のための少し新しいコンセプトです。あなたが決して少なくなることは決してありません。 – thwalker6

+0

何か問題が見つかったときに、 'stderr.read()'と 'recv_exit_status()'に何が入っているかを見てみることができます。 – pynexj

+0

私はstderr.read()を見てみましたが、私の混乱を招くような値は得られませんでした。私はこれを理解しようとするより多くの時間を私が認めたいと思った以上に過ごしました。 – thwalker6