Tuesday, June 28, 2016

automatically answer to command that require to answer

1- install answers
$ sudo pip install answers


- example when we run command apt-get autoremove  it will ask us:
Do you want to continue? [Y/n]

2- so we can automate it, without interactively type Y in terminal by using answers

- create script call auto-apt-get-autoremove.py with the following contents

from answers import *
ShellCommand = Answers("sudo apt-get autoremove", '{"answers": [ {"question":"Do you want to continue?", "answer":"Y"}] }')
ShellCommand.debugMode = True
ShellCommand.logMode = True
ShellCommand.timeoutInSeconds = 1
ShellCommand.Execute()


3- run it, and the aget-ger remove will get answer Y when ask the question without our intervention at all
$ sudo python auto-apt-get-autoremove.py

4- after the running is completed we can view its logs ("answers.log")
$ cat answers.log
Answers Loaded: {u'answers': [{u'answer': u'Y', u'question': u'Do you want to continue?'}]}

No comments:

Post a Comment