SSH Connection with Python 3.0

后端 未结 6 1396
余生分开走
余生分开走 2020-12-03 18:53

How can I make an SSH connection in Python 3.0? I want to save a file on a remote computer where I have password-less SSH set up.

6条回答
  •  一向
    一向 (楼主)
    2020-12-03 19:04

    First:

    Two steps to login via ssh without password

    in your terminal

    [macm@macm ~]$  ssh-keygen
    [macm@macm ~]$  ssh-copy-id -i $HOME/.ssh/id_rsa.pub root@192.168.1.XX <== change
    

    Now with python

    from subprocess import PIPE, Popen
    
    cmd = 'uname -a'
    stream = Popen(['ssh', 'root@192.168.1.XX', cmd],
                        stdin=PIPE, stdout=PIPE)
    
    rsp = stream.stdout.read().decode('utf-8')
    print(rsp)
    

提交回复
热议问题