2

I run the following command in git bash terminal (not the windows command line or mac's terminal).

ssh username@code.companyname.com -p 12356 gerrit query --format=JSON --current-patch-set --files change: 878545

This generates a JSON output when I run it in git bash. I am trying to do the same in Python file using the following:

import subprocess
result = subprocess.run(
    [
        "ssh",
        "-c",
        "aes128-cbc", #adds necessary ciphers
        "username@code.companyname.com",
        "-p",
        "12356",
        "gerrit",
        "query",
        "--format=JSON",
        "--current-patch-set",
        "--files",
        "change: ",
        "878545",
    ], stdout=subprocess.PIPE,
       stderr=subprocess.STDOUT,
       shell=True

)
result_process = result.stdout.decode("utf-8")
print(result_process)

But this produced an error: 'ssh' is not recognized as internal or external command, operable program or batch file.

The ssh works in command line.

Gravity Mass
  • 605
  • 7
  • 13
  • @jodag it is producing an error " 'ssh' is not recognized as an internal or external command, operable program or batch file." – Gravity Mass Jul 08 '19 at 23:45
  • 1
    Resinded my comment because of security issues with `shell=True`. But if you wanted to do it you need to rejoin the list into a string. E.g. if you had `subprocess.run(['echo', 'a'])` would become `subprocess.run('echo a', shell=True)`. In your case you should be able to augment the first argument by doing `' '.join([...])` to your first argument where `[...]` is your first argument. – jodag Jul 08 '19 at 23:49
  • @jodag I did subprocess.run("ssh username@code.companyname.com -p 12356 gerrit query --format=JSON --current-patch-set --files change: 878545", shell=True) ... it gave the same error – Gravity Mass Jul 08 '19 at 23:56
  • does it runs ok in command line ? – user8426627 Jul 09 '19 at 02:10
  • 2
    `AttributeError: 'module' object has no attribute 'run'`, so I tried `Popen` and `result.stdout.readlines()`. It gave the expected result, a list with 2 json bytes ending with `\n`. – ElpieKay Jul 09 '19 at 02:23
  • @ElpieKay I issued the ssh command in command line and it worked but when I tried to run it through Popen, it says "ssh is not recognized as internal or external command or operable program or batch file" – Gravity Mass Jul 09 '19 at 18:14
  • You have to give the path to `ssh`, probably `/usr/bin/ssh`. – wallyk Jul 09 '19 at 18:22
  • @wallyk it says "The system cannot find the path specified." – Gravity Mass Jul 09 '19 at 18:46
  • In a command shell, type `which ssh` and use the result as the full path. – wallyk Jul 09 '19 at 22:19
  • @wallyk I am on windows and I did {where ssh}. Added the whole path to the command. it shows "}username@code.companyname.com: Permission denied (publickey).}" But I was able to login to that domain code.companyname.com – Gravity Mass Jul 09 '19 at 23:43
  • Does this answer your question? [How to run bash commands using subprocess.run on windows](https://stackoverflow.com/questions/58402900/how-to-run-bash-commands-using-subprocess-run-on-windows) – user202729 Aug 16 '21 at 13:52

0 Answers0