Verify host key with pysftp

前端 未结 9 889
北海茫月
北海茫月 2020-11-22 08:33

I am writing a program using pysftp, and it wants to verify the SSH host Key against C:\\Users\\JohnCalvin\\.ssh\\known_hosts.

Using PuTTY, the terminal

9条回答
  •  长发绾君心
    2020-11-22 08:57

    I've implemented auto_add_key in my pysftp github fork.

    auto_add_key will add the key to known_hosts if auto_add_key=True
    Once a key is present for a host in known_hosts this key will be checked.

    Please reffer Martin Prikryl -> answer about security concerns.

    Though for an absolute security, you should not retrieve the host key remotely, as you cannot be sure, if you are not being attacked already.

    import pysftp as sftp
    
    def push_file_to_server():
        s = sftp.Connection(host='138.99.99.129', username='root', password='pass', auto_add_key=True)
        local_path = "testme.txt"
        remote_path = "/home/testme.txt"
    
        s.put(local_path, remote_path)
        s.close()
    
    push_file_to_server()
    

    Note: Why using context manager

    import pysftp
    with pysftp.Connection(host, username="whatever", password="whatever", auto_add_key=True) as sftp:
        #do your stuff here
    #connection closed
    

提交回复
热议问题