2013-01-20 4 views
0

아래의 ssh 명령을 파이썬 코드로 변환하려고하는데 파이썬으로 변환 할 수 있었지만 파이썬 코드가 출력을 제공하지 않아 제대로 변환하지 못했을 것입니다. 특히 부울 "AND"logic.. 여기 누군가가 잘못된 점을 지적 할 수 있습니까?ssh 명령을 파이썬으로 변환

ssh -p 29418 company.com gerrit query --current-patch-set --commit-message --files 'status:open project:platform/code branch:master label:Developer-Verified=1 AND label:Code-Review>=1' 

파이썬 코드 : -

with open(timedir + "/ids.txt", "wb") as file: 
    check_call("ssh -p 29418 company.com " 
     "gerrit query --commit-message --files --current-patch-set " 
     "status:open project:platform/code branch:master label:Developer-Verified=1 AND label:Code-Review>=1 |" 
     "grep refs |" 
     "cut -f4 -d'/'", 
      shell=True, # need shell due to the pipes 
      stdout=file) # redirect to a file 

답변

1

>=1은 ids.txt 파일에는 출력이없는 이유 =1 파일로 리디렉션으로 해석 될 수 있습니다.

check_call()에서 사용하는 문자열은 제릿의 인수를 인용하지 않습니다. 인수를 인용하지 않는 첫 번째 명령과 비교하십시오.

pipes.quote()을 사용하여 문자열을 쉘의 단일 명령 줄 인수로 이스케이프 할 수 있습니다.

1

사용 Paramiko 그런 다음 당신이 시스템 호출을 실행하기 위해 subprocess 파이썬 모듈을 사용할 수 있습니다 SSH

import paramiko 
ssh = paramiko.SSHClient() 
ssh.connect('127.0.0.1', username='xxx', 
    password='xxx') 

를 통해 연결합니다.

Calling an external command in Python