3

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)
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
mn0o7
  • 65
  • 8

1 Answers1

2

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.)

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
  • Yeah that could actually work if i ssh to the device using parmiko get the key and pass it onto the putty execution. Im still really interested in knowing why pywinauto doesnt have any effect on the popup window – mn0o7 Dec 11 '19 at 17:25
  • Nope actually it wont work since i need to login with putty gui and enable ssh login through the interactive perl menu :/ – mn0o7 Dec 11 '19 at 17:29
  • No you have to pass the a hardcoded/configured host key explicitly. Retrieving it somehow automatically (by Paramiko or any other way) negates the purpose of the verification. It's there as a protection against [MITM attacks](https://en.wikipedia.org/wiki/Man-in-the-middle_attack). – Martin Prikryl Dec 11 '19 at 17:52
  • Thank you for the direction i managed to make it work heres the code to retrieve the remote host key aka fingerprint incase some googles this: `def get_fingerprint(ip_addr): transport = paramiko.Transport(ip_addr) transport.connect() key = transport.get_remote_server_key() transport.close() return(key.get_base64())` – mn0o7 Dec 11 '19 at 20:56
  • That's what I advised you NOT TO DO. – Martin Prikryl Dec 11 '19 at 21:11
  • This is a autodeploy for a closed QA environment. – mn0o7 Dec 12 '19 at 06:54
  • But if you use Python/Paramiko anyway, I do not see why you execute PuTTY. – Martin Prikryl Dec 13 '19 at 10:55
  • Im using paramiko only to grab the host key then using it to connect with putty. Why im not connecting with ssh/paramiko and running everything on the linux os level? Because certain things can only be changed from the perl menu which is an interactive cli menu(requires gui as far as i can tell) – mn0o7 Dec 13 '19 at 14:06
  • If you can do it in PuTTY, you can do it in Paramiko. But it can be tricky to implement indeed. – Martin Prikryl Dec 13 '19 at 14:09