2017-04-15 96 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