Search code examples
python-3.6centos7csh

Python3.6 and CSHELL Script on Centos 7 - need to step through prompts


I am currently dealing with a cshell script that I need to run on my servers, I cannot modify the cshell script. I wanted to automate running the script, which I have succeeded in launching the script, but the script has questions to be answered - examples:

Choose 1 for Linux, Choose 2 for Windows, or 3 to exit:

What is the system hostname:

etc.....

I want to be able to answer these questions through the python script. I have searched the standard Google searches, but I am unfamiliar with the results nothing seems to quite be the answer - does anyone know of any modules that can perform this function, with some good examples that I can see to help with the comprehension of putting this together, any recommendations or suggestions are appreciated? Thanks for your consideration!


Solution

  • The canonical tool to drive an interactive tool is expect. See https://core.tcl.tk/expect/index for more information.

    Your expect script, given your example, would be something like:

    expect -re "Choose 1.*exit:"
    send -- "1\r"
    expect "hostname:"
    send -- "$hostname\r"
    

    Once you've got the expect script working you can call this simply from Python.