Read a file from server with SSH using Python

前端 未结 5 1131
北恋
北恋 2020-11-28 05:11

I am trying to read a file from a server using SSH from Python. I am using Paramiko to connect. I can connect to the server and run a command like cat filename

5条回答
  •  失恋的感觉
    2020-11-28 05:52

    #!/usr/bin/env python
    import paramiko
    import select
    client = paramiko.SSHClient()
    client.load_system_host_keys()
    client.connect('yourhost.com')
    transport = client.get_transport()
    channel = transport.open_session()
    channel.exec_command("cat /path/to/your/file")
    while True:
      rl, wl, xl = select.select([channel],[],[],0.0)
      if len(rl) > 0:
          # Must be stdout
          print channel.recv(1024)
    

提交回复
热议问题