Search code examples
pythonwebsphereibm-midrange

How can we automate login to IBM System using Python?


I need to login to IBM i System using Python without entering the username and password manually.

I used py3270 library but it is not able to detect the Emulator wc3270. The emulator I use has .hod extension and opens with IBM i Launcher. Can anyone help me with this? what could be the possible solution for this?


Solution

  • os.system() is a blocking statement. That is, it blocks, or stops further Python code from being executed until whatever os.system() is doing has completed. This problem needs us to spawn a separate thread, so that the Windows process executing the ACS software runs at the same time the rest of the Python code runs. subprocess is one Python library that can handle this.

    Here is some code that opens an ACS 5250 terminal window and pushes the user and password onto that window. There's no error checking, and there are some setup details that my system assumes about ACS which your system may not.

    # the various print() statements are for looking behind the scenes 
    
    import sys
    import time
    import subprocess
    from pywinauto.application import Application
    import pywinauto.keyboard as keyboard
    
    userid = sys.argv[1]
    password = sys.argv[2]
    
    print("Starting ACS")
    cmd = r"C:\Users\Public\IBM\ClientSolutions\Start_Programs\Windows_x86-64\acslaunch_win-64.exe"
    system = r'/system="your system name or IP goes here"'
    # Popen requires the command to be separate from each of the parameters, so an array
    result = subprocess.Popen([cmd, r"/plugin=5250",system], shell=True)
    print(result)
    
    # wait at least long enough for Windows to get past the splash screen
    print("ACS starting - pausing")
    time.sleep(5)
    
    print("connecting to Windows process")
    ACS = Application().connect(path=cmd)
    print(ACS)
    
    # debugging
    windows = ACS.windows()
    print(windows)
    
    dialog = ACS['Signon to IBM i']
    print(dialog)
    
    print("sending keystrokes")
    keyboard.send_keys(userid)
    keyboard.send_keys("{TAB}") 
    keyboard.send_keys(password)
    keyboard.send_keys("{ENTER}")
     
    print('Done.')