I am writing a Python script which SSHs into every server within a given subnet, collects the uptime data, then prints the data to the screen. Most of the information necessary to run is collected from the user-supplied arguments when the script is executed, ie:
getUptime.py 192.168.150.1 /27 <options>
The two relevant options for this question are:
-l
: Write results to a logfile.
-s
: Silence the shell output.
Once the script is executed, it immediately prompts the user for a password using getpass
, and it will use that password to access every server.
So here's my problem: I want the user to be able to supply the -l
and -s
flags and only write to the log, while freeing up their terminal for whatever else they need to do. I know that this can be done with ctrl+z followed by bg
, but I want to do it within the script, rather than depending on the end user.
In previous scripts, I'd just include in the command documentation that the silencing flag is -s&
, so bash will run it in the background from the start, but that skips the password prompt, and leaves the script hanging. I need to move it to the background after the user has provided the password, and providing the password within the execution arguments is out of the question, due to IA requirements (it would be echoed onto the screen, and recorded in the bash command history).
Most of the solutions I've found through Google involve just appending &
to the command string; is there any other way to move the script to the background? Would it be possible for me to have the script send the ctrl+z and bg
commands itself?
Thank you in advance for any advice you can offer.