Feeding input to an interactive command line application

前端 未结 2 1785
误落风尘
误落风尘 2020-12-10 03:06

I\'d like to feed inputs to a command line interface for Cisco AnyConnect vpncli.exe (v2.3) to automate its (re)connection. It does not take username nor passw

相关标签:
2条回答
  • 2020-12-10 03:44

    you need to create an usual text file like

    connect myvpnhost
    myloginname
    mypassword

    save it as myfile.dat (for example) and then call

    "%ProgramFiles%\Cisco\Cisco AnyConnect Secure Mobility Client\vpncli.exe" -s < myfile.dat

    0 讨论(0)
  • 2020-12-10 03:52

    There are at least two ways to read input in a Windows console application.

    • ReadConsole: reads input either from keyboard or redirection (documentation).
    • ReadConsoleInput: reads only raw keystrokes (documentation).

    The vpncli.exe application uses ReadConsoleInput in order to read the password, that's way redirecting the password does not work. You can, though, use WriteConsoleInput. I have a small Python script that does exactly that:

    import subprocess
    import win32console
    
    ANYCONNECT_BIN = 'c:\\Program Files\\Cisco\\Cisco AnyConnect Secure Mobility Client\\vpncli.exe'
    
    def write_console_input(text):
      stdin = win32console.GetStdHandle(win32console.STD_INPUT_HANDLE)
      ir = win32console.PyINPUT_RECORDType(win32console.KEY_EVENT)
      ir.KeyDown = True
      for ch in text:
        ir.Char = unicode(ch)
        stdin.WriteConsoleInput([ir])
    
    def main():
      proc = subprocess.Popen([ANYCONNECT_BIN,'connect','VPN'],stdin=subprocess.PIPE)
      proc.stdin.write('%s\n%s\n' % ('GROUP', 'USERNAME'))
      write_console_input('%s\n' % 'PASSWORD')
      ret = proc.wait()
      print ret
    
    if __name__ == '__main__':
      main()
    
    0 讨论(0)
提交回复
热议问题