Verify host key with pysftp

前端 未结 9 885
北海茫月
北海茫月 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 09:00

    Cook book to use different ways of pysftp.CnOpts() and hostkeys options.

    Source : https://pysftp.readthedocs.io/en/release_0.2.9/cookbook.html

    Host Key checking is enabled by default. It will use ~/.ssh/known_hosts by default. If you wish to disable host key checking (NOT ADVISED) you will need to modify the default CnOpts and set the .hostkeys to None.

    import pysftp
    cnopts = pysftp.CnOpts()
    cnopts.hostkeys = None
    with pysftp.Connection('host', username='me', password='pass', cnopts=cnopts):
        # do stuff here
    

    To use a completely different known_hosts file, you can override CnOpts looking for ~/.ssh/known_hosts by specifying the file when instantiating.

    import pysftp
    cnopts = pysftp.CnOpts(knownhosts='path/to/your/knownhostsfile')
    
    with pysftp.Connection('host', username='me', password='pass', cnopts=cnopts):
        # do stuff here
    

    If you wish to use ~/.ssh/known_hosts but add additional known host keys you can merge with update additional known_host format files by using .load method.

    import pysftp
    cnopts = pysftp.CnOpts()
    cnopts.hostkeys.load('path/to/your/extra_knownhosts')
    with pysftp.Connection('host', username='me', password='pass', cnopts=cnopts):
        # do stuff here
    

提交回复
热议问题