Automatic login: script to detect a text string and respond

Version 3

    Verified Product Versions

    CETerm 5.7

    NOTE: This auto-login script is ONLY for terminal emulation sessions. It is not recommended to be used for Browser sessions. Comments have been added in the script so that you can easily modify it to suit you environment.


    Here is an overview of how it works. When the session connects, the script OnSessionConnect(session) is automatically called. The expect method is called to start looking for text on the screen. The expect function is implemented in the script file expect.js (not shown here but attached in zip file). It keeps checking for up to 8 seconds. When it finds the text "login", it submits the username and an "Enter" as the response. It then starts looking for the next text and will submit the password when text "password" is found. You can edit the search strings which the script should look for on the screen to suite your application requirements. You can also change the username and password.





    function OnSessionConnect( session )


        // Start expect script

        // TODO: Enter correct username and password

        var myusername1 = "joeuser";

        var mypassword1 = "secret";

        //var myusername2 = "user2";

        //var mypassword2 = "secret2";

        var waittime = 8000;   // Milliseconds waiting for each text



       // TODO: Set the session numbers if different from 1 and 2

        if (session == 1)


            // TODO: Enter correct text to match on host screens.

            expect( session, waittime, "login", myusername1 + "\r",

                                       "password", mypassword1 + "\r" );


        //else if (session == 2)


        //    // TODO: Enter correct text to match on host screens.

        //    expect( session, waittime, "login", myusername2 + "\r",

        //                               "password", mypassword2 + "\r" );






        // TODO: You can also add more "match" - response pairs if wanted

        //expect( session, waittime, "login", myusername + "\r",

        //                           "password", mypassword + "\r",

        //                           "Menu", "1\r",     // select menu item 1

        //                           "Picklist", "2\r", // select Picklist option 2

        //                           "User", "Joe\r" );  // identify user to application



    Steps to install and run


    • On your desktop PC, edit "OnConnect.js" to specify the appropriate text that your host screens show at the login and password prompts. Currently these are login and password. A regular-expression can be used here if needed.
    • Edit the username and password values. These can be stored in other locations on the device if wanted. For example, they can be read from a file. You can insert any additional logic to select different usernames as needed.


      • Look for "TODO" notes in the script for instructions.
      • The sample script has a second session login commented out. You can uncomment this to experiment with multiple sessions.
      • Scripts can also be edited directly on the device, but it may be easier to make initial changes on your desktop PC
      • You should not need to edit "expect.js"


    • Copy expect.js and OnConnect.js to your device to the \My Documents directory.
    • Load and configure the scripts.
      • Select Session->Configure->Options->Configure Scripting
      • On the "General" tab
        • Check "Enable"
        • Check "Show Script Errors"
        • Tap the "Reinitialize" button if scripting was already enabled.
      • On the "Scripts" tab
        • Select script "1" or other available (empty) script and tap Edit.
        • Check "Load at Startup"
        • Tap "Import" and select file "OnConnect.js"
        • Select "OK"
      • Select script "2" or other available (empty) script and tap Edit.
      • Check "Load at Startup"
      • Tap "Import" and select file "expect.js"
      • Select "OK"
    • Select OK to exit all dialogs.
    • Disable any current "Macro on connect" enabled for the session. This setting is under Connection -> Advanced -> General tab
    • Set the remainder of your configuration and test your connection.




    • Tap "Re-Initialize" on the scripting "General" tab after you edit scripts
    • Be sure to check the "Load at Startup" boxes for these scripts
    • If your host takes more than 8 seconds to show the prompts you may need to increase the "8000" value in OnConnect.js (or edit in the loaded script)
    • All imported scripts are stored in the registry, so no files are needed after loading and preserving the registry values.
    • See the "OnConnect.js" script for information on adding more steps after the user and password are submitted.
    • The text match is literal and case dependent. If you want more flexibility, you can specify a Javascript regular expression.
    • The "expect" method is fairly powerful and can be used for other automation task.
    • Edit an empty script and tap on "Template" to see some of the event handlers that can be configured. These templates also show some of the automation object features.