2017-04-15 101 views
1

我使用Python 2.7和2.1.2的paramiko检查,如果在一个服务器上运行的OpenSSH存在Windows目录。 Windows目录存在于服务器上,但我的代码没有找到它。从我的测试中,似乎我错误地指定了该目录,但我无法弄清楚为什么这不起作用。虽然我没有显示它,但如果我使用“/”而不是“\”,那么我不会成功。有没有人有任何想法如何解决这个问题,以及可能发生了什么?使用exec_command检查如果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 
+0

您是否尝试过运行cmd/c目录d:\而不是? (请编辑从命令的输出到你的问题。) –

+0

感谢哈利。我刚更新了这个问题。 “D:\”起作用。但是,“D:\ Log \”似乎不能正常工作。 – Burzin

+0

你从第四次试验获得该错误是非常可疑的,它意味着'sh'是越来越莫名其妙地参与和反斜线和引号多层正在处理中。尝试在同一组测试,但每个'/ C'后加入'echo',如'CMD/C回声目录“d:\登录”',希望你能看到什么命令看起来像一次它已经通过了所有的过滤。 –

回答

-2

您正在寻找os.path.isdir,或os.path.exists 的目录。

if os.path.isdir("/test/to/dir") == True: 

    print('dir exists ') 

    # you next actions 
+0

不要忘记导入操作系统 –

+1

OP代码正在测试远程计算机上目录的存在(通过SSH),但此答案仅用于测试本地计算机上是否存在目录。 –

+0

@ Mahdi_Bahri除非我错过了一些东西,我不认为就是这样。我试图检查远程Windows系统上是否存在目录。远程系统上没有Python。我在哪里运行脚本的本地系统有Python,但这对我没有帮助。 – Burzin