私は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.py
はcommand_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マシンに入れて、まったく同じコマンドを実行することができます。
参照イメージ:
私はどのように進んでいますか?
はい@MartinPrikryl –
ありがとう@MartinPrikryl、私は変更を加えました。また、 'ssh_stderr'は空文字列を返しますので、エラーがないと仮定します。 –
コードスニペットに見られるように、' ssh_stderr.read() 'を実行してください。 どのsshサーバ - 'freeSSHd'。 sshを実行して実行すると機能しますか?はい。 –