Search code examples
pythonpython-3.xsshpython-requestsparamiko

ssh to server and execute curl equivalent with Python


I have this command:

ssh 111.222.333.444 curl -s -k https://123.456.789.876:4567/mock.crt -o /root/mock.crt

I'd like to include it, or the equivalent of it, in a Python application I'm writing. I think I need to use a combination of requests and paramiko to achieve this. So far I'vew come up with this:

  def add_certs(self):
      ssh = paramiko.SSHClient()
      ssh.connect(111.222.333.444)
      url = 'https://123.456.789.876:4567/mock.crt'
      res = ssh.requests.get(url, verify=False)
      print("Add mock server certs: " + res.json())

I haven't had a chance to run this yet as the server is currently unavailable. I still haven't got the output writing to file, rather than stdout. How do I do this? Also, if there is anything wronw with the way I'm trying to do this I'd appreciate the feedback.


Solution

  • There's no API in SSH protocol itself to make the server download a file and store it locally (on the server).

    This is true in general, Python or not.

    All you can do is to execute some application or command (like curl or similar) on the SSH server, to do it for you. In Python/Paramiko, you can use SSHClient.exec_command.


    There might be a pure Python way to achieve, what you need, with some limitations. But we do not know your constraints, so I cannot elaborate.