2013-12-17 120 views
8

運行下面的腳本時,我收到以下錯誤,可以anyhelp以確定是什麼問題,以及如何克服它類型錯誤:execv()ARG 2必須只包含字符串

import subprocess 
import sys 
import os 

def main(): 
    to = '' 
    ssh_command = ["ssh", "-p", "29418", "review-android.quicinc.com", "gerrit", 
        "query", "--format=JSON", "--current-patch-set", 
        "--commit-message", "--files", ] 

    with open('gerrit_output.txt', 'a') as fp: 
     with open('caf_gerrits.txt','r') as f : 
      for gerrit in f : 
       print gerrit 
       result = subprocess.check_output(ssh_command + [gerrit, ]) 
       print result 
       fp.write(result) 

if __name__ == '__main__': 
main() 

錯誤: -

Traceback (most recent call last): 
    File "test.py", line 20, in <module> 

    File "test.py", line 15, in main 

    File "/usr/lib/python2.7/subprocess.py", line 537, in check_output 
    process = Popen(stdout=PIPE, *popenargs, **kwargs) 
    File "/usr/lib/python2.7/subprocess.py", line 679, in __init__ 
    errread, errwrite) 
    File "/usr/lib/python2.7/subprocess.py", line 1249, in _execute_child 
    raise child_exception 
TypeError: execv() arg 2 must contain only strings 

回答

16

ssh_command第三個元素是一個整數。它需要是一個字符串。

e.g:

ssh_command = ["ssh", "-p", 29418, ... 
#       ^problem 

而且解決方法很簡單,只需要添加一些報價:

ssh_command = ["ssh", "-p", "29418", ... 
#       ^Now it's a string. 
+0

感謝您的信息..如何解決它? – user2955256

+1

that does not fix the issue .. – user2955256

+1

@ user2955256 - 相同的錯誤?如果出現同樣的錯誤,您應該準確發佈我們需要*重現問題的內容。例如張貼'gerrit'的確切內容。如果我們不能重現你的問題,那麼我們只是在猜測發生了什麼問題。 – mgilson

1

首先,你需要爲mgilson提到的周圍添加29418引號。其次,讓我們打破你要運行什麼:

ssh_command = ["ssh", "-p", 29418, "review-android.company.com", "gerrit", 
       "query", "--format", "JSON", "--current-patch-set", 
       "--commit-message", "--files", ] 

,等於

,在我彈出
ssh -p 29418 review-android.company.com gerrit query --format JSON --current-patch-set --commit-message --files 
(then I'm assuming you have filenames in your caf_gerrits.txt file which get appended at the end) 

的一件事是,你可能要說在--format=JSON這種情況下,在你的元素ssh_command數組應合併爲[..., "--format=JSON", ...]。您可能想要做的另一件事是您的result=行後面的print result以幫助進行調試。

+0

我更新了最新的代碼和錯誤 – user2955256

+0

什麼是你想要完成的確切的ssh命令? – misakm

相關問題