2017-12-12 8 views
-1

私はparamikoを使用して、リモートWindowsサーバー上でコマンドを実行しています。私はdirのようなコマンドを実行して出力を抽出することができますが、pythonスクリプトの実行は失敗したようです。エラーメッセージはスローされません。ここでリモートウィンドウクライアントでPythonスクリプトを実行

は私のコードスニペットです:ここで

def ssh_connect(ip,user,pwd): 
     ssh = paramiko.SSHClient() 
     ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) 
     ssh.connect(ip, username=user, password=pwd) 
     return ssh 


def execute_command(device_details, command): 
     ip = device_details.get('ip') 
     username = device_details.get('username') 
     password = device_details.get('password') 
     ssh_ns_obj = ssh_connect(ip, username, password) 
     ssh_stdin, ssh_stdout, ssh_stderr = ssh_ns_obj.exec_command(command) 
     print ssh_stderr.read() 
     return ssh_stdout.read() 

device_details = dict() 
device_details['ip'] = 'a.b.c.d' 
device_details['username'] = 'Administrator' 
device_details['password'] = 'pass' 

command_1 = "cmd /c mkdir asdf" 
output = execute_command(device_details, command_1) 

command_2 = 'cmd /c "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\python.exe C:\pythonattempt\try.py"' 
output = execute_command(device_details, command_2) 

は私のtry.pycommand_2に記載されている:

import os 

if __name__ == '__main__': 
    print ("Hey, just starting") 

    os.system(r"mkdir C:\Users\Administrator\Desktop\x") 
    print ("hey, file is up") 

    with open(r"C:\Users\Administrator\Desktop\x.txt", "w") as f: 
     f.write("This is a line of a file") 

command_1は正常に実行され、私は私のWindowsマシン上で作成したディレクトリを参照してくださいすることができますよ。しかしcommand_2はエラーを投げず、実行もしません。

x.txtまたはフォルダxが作成されないためです。

私はfreeSSHdサービスをWindowsにインストールしました。私はsshをWindowsマシンに入れて、まったく同じコマンドを実行することができます。

以下

参照イメージ:

enter image description here

私はどのように進んでいますか?

+0

はい@MartinPrikryl –

+0

ありがとう@MartinPrikryl、私は変更を加えました。また、 'ssh_stderr'は空文字列を返しますので、エラーがないと仮定します。 –

+0

コードスニペットに見られるように、' ssh_stderr.read() 'を実行してください。 どのsshサーバ - 'freeSSHd'。 sshを実行して実行すると機能しますか?はい。 –

答えて

0

エラーが\tが特殊文字であることであると私はC:\pythonattempt\try.pyを実行しようとしていたとき、\tは特殊文字としました。

viの代わりにsublimeに同じスクリプトを開いたときに気付きましたが、色に違いがありました。

関連する問題