【发布时间】:2017-09-11 21:17:55
【问题描述】:
我正在使用 Python 2.7 和 Paramiko 2.1.2 检查运行 OpenSSH 的服务器上是否存在 Windows 目录。 Windows 目录存在于服务器上,但我的代码没有找到它。从我的测试来看,我似乎错误地指定了目录,但我无法弄清楚为什么这不起作用。虽然我没有展示它,但如果我使用“/”而不是“\”,我不会成功。有没有人知道如何解决这个问题,可能会发生什么?
更新 1:这在 Windows 系统上可以正常执行,
stdin, stdout, stderr = ssh.exec_command('cmd /c dir "D:\"')
但是,我仍然无法检查特定目录是否存在
更新 2:这似乎有效。
stdin, stdout, stderr = ssh.exec_command('if [[ -d /cygdrive/d/log ]]; then echo Exists; fi')
但是,我仍然对任何关于为什么跟随不起作用,或者说不能始终如一地起作用的解释感兴趣。
if_exist_path = "cmd /c if exist \"" + log_path + "\" echo Exists"
stdin, stdout, stderr = ssh.exec_command(if_exist_path)
考虑下面的代码。
import paramiko
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect("10.9.8.7", username="FakeUser", password="FakePassword")
log_path = "D:\\Log"
if_exist_path = "cmd /c if exist \"" + log_path + "\" echo Exists"
print("Var 'if_path_exist' is: {}".format(if_exist_path))
stdin, stdout, stderr = ssh.exec_command(if_exist_path)
stdoutput = stdout.readlines()
stderror = stderr.readlines()
print("First Stdout is {}".format(stdoutput))
print("First Stderr is {}".format(stderror))
print("")
stdin, stdout, stderr = ssh.exec_command('cmd /c dir "D:\\Log"')
stdoutput = stdout.read()
stderror = stderr.read()
print("Second Stdout is {}".format(stdoutput))
print("Second Stderr is {}".format(stderror))
print("")
stdin, stdout, stderr = ssh.exec_command('cmd /c dir "D:\"')
stdoutput = stdout.read()
stderror = stderr.read()
print("Third Stdout is {}".format(stdoutput))
print("Third Stderr is {}".format(stderror))
print("")
stdin, stdout, stderr = ssh.exec_command('cmd /c dir "D:\\"')
stdoutput = stdout.read()
stderror = stderr.read()
print("Fourth Stdout is {}".format(stdoutput))
print("Fourth Stderr is {}".format(stderror))
print("")
stdin, stdout, stderr = ssh.exec_command('cmd /c dir "D:\Log"')
stdoutput = stdout.read()
stderror = stderr.read()
print("Fifth Stdout is {}".format(stdoutput))
print("Fifth Stderr is {}".format(stderror))
print("")
stdin, stdout, stderr = ssh.exec_command('cmd /c dir "D:\Log\"')
stdoutput = stdout.read()
stderror = stderr.read()
print("Sixth Stdout is {}".format(stdoutput))
print("Sixth Stderr is {}".format(stderror))
print("")
输出:
C:\Scripts>SO_Test.py
Var 'if_path_exist' is: cmd /c if exist "D:\Log" echo Exists
First Stdout is []
First Stderr is []
Second Stdout is Volume in drive D is Apps
Volume Serial Number is xxxx-xxxx
Directory of D:\
Second Stderr is File Not Found
Third Stdout is Volume in drive D is Apps
Volume Serial Number is xxxx-xxxx
Directory of D:\
01/26/2017 10:49 PM <DIR> Log
01/12/2017 11:34 AM 12,887 Blah.ps1
01/12/2017 01:16 PM 12,082 result.txt
12/12/2016 03:39 PM 23,340 Blahv201.zip
04/15/2017 10:37 PM <DIR> Company
01/27/2017 08:19 PM <DIR> CompanyBackup
01/12/2017 01:08 PM 10,060 Untitled1.ps1
4 File(s) 58,369 bytes
3 Dir(s) 175,276,904,448 bytes free
Third Stderr is
Fourth Stdout is
Fourth Stderr is sh: -c: line 0: unexpected EOF while looking for matching `"'
sh: -c: line 1: syntax error: unexpected end of file
Fifth Stdout is Volume in drive D is Apps
Volume Serial Number is xxxx-xxxx
Directory of D:\
Fifth Stderr is File Not Found
Sixth Stdout is Volume in drive D is Apps
Volume Serial Number is xxxx-xxxx
Directory of D:\
Sixth Stderr is File Not Found
【问题讨论】:
-
您是否尝试过运行 cmd /c dir d:\? (请将该命令的输出编辑到您的问题中。)
-
谢谢哈利。我刚刚更新了这个问题。 “D:\”有效。但是,“D:\Log\”似乎无法正常工作。
-
您从第四次测试中得到的错误非常可疑,这意味着
sh以某种方式参与其中,并且反斜杠和引号正在多层处理。尝试相同的测试集,但在每个/c之后添加echo,例如cmd /c echo dir "D:\Log",希望您能够看到命令通过所有过滤后的样子。 -
如果服务器支持,你应该使用 SFTP 而不是普通的 SSH。因为服务器是 Windows,您可能需要以交互方式 sftp 进入它以找出访问 D: 驱动器的正确路径。如果服务器是 Cygwin,我想你会使用“/cygdrive/d/dir”或类似的东西。
标签: python windows python-2.7 paramiko openssh