Search code examples
pythonpython-3.xsshputtypywinauto

Confirm PuTTY host key prompt with pywinauto


I'm trying to automate PuTTY interface with pywinauto everything works except the part where i need to press y to accept the host keys when the alert window pops up. I'm using the PuTTY interface because it connects to an interactive interface not just normal ssh.

Here's my code. I'm wondering why when I send y or Enter, it doesn't have effect on the popup window:

def config_dp(hostname, cm_temp_ip, new_ip):
    first_connect(cm_temp_ip)
    app = Application ().Start (cmd_line=u'putty.exe admin@'+cm_temp_ip+' -pw BLAHBLAHJ')
    putty = app.PuTTY
    putty.type_keys("y")
    putty.wait('ready')
    time.sleep(3)
    putty.type_keys("2")
    putty.type_keys("{ENTER}")
    putty.type_keys("2")
    putty.type_keys("{ENTER}")
    putty.type_keys(hostname)
    putty.type_keys("{ENTER}")
    putty.type_keys("{ENTER}")
    time.sleep(3)
    putty.type_keys("U")
    putty.type_keys("3")
    putty.type_keys("{ENTER}")
    putty.type_keys("{ENTER}")
    putty.type_keys("2")
    putty.type_keys("{ENTER}")
    putty.type_keys(new_ip+"/24")
    putty.type_keys("{ENTER}")
    time.sleep(5)
    putty.close()
    first_connect(new_ip)
    reboot(new_ip)
    time.sleep(60)
    enable_root(new_ip)

Solution

  • Do not automate host key verification. Instead use the -hostkey command line switch to provide the fingerprint of valid host key.

    Can I pass RSA hostkey of server as PuTTY command line option?


    (Putting aside why you want to automate PuTTY, instead of using native Python SSH implementation.)