0

I am trying to convert the below ssh command to python code,I was able to convert to python but the python code doesnt give any output,so i think i did not convert it properly especially the boolean "AND" logic..can anyone point what is wrong here?

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'

Python code:-

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
user1934146
  • 2,905
  • 10
  • 25
  • 25

2 Answers2

1

>=1 might be interpreted as a redirect to =1 file that is why there is no output in the ids.txt file.

The string that you use in check_call() doesn't quote the gerrit's argument. Compare it with the first command that does quote the argument.

You could use pipes.quote() to escape a string as a single command-line argument for shell.

jfs
  • 399,953
  • 195
  • 994
  • 1,670
1

Use Paramiko to connect via ssh

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

Then you can use the subprocess python module to execute system calls.

Calling an external command in Python

Community
  • 1
  • 1
Pratik Mandrekar
  • 9,362
  • 4
  • 45
  • 65