Answer the question
In order to leave comments, you need to log in
How to pass string from script to terminal?
I use Python's subprocess library to call system commands from a script. When calling the sudo apt upgrade command on Linux, you must press the "y" button to confirm consent. How can I directly pass the button press "y" from the script?
def package_upgrade():
print("[+] Upgrading packages...")
subprocess.call(["sudo", "apt", "upgrade"])
Answer the question
In order to leave comments, you need to log in
I recommend reading the documentation. In particular, apt upgrade has flags that allow you not to ask for confirmation.
subprocess.call(["sudo", "apt", "upgrade", "--yes"])
I will add to the collection, to other quite correct options, one more, suitable not only for apt: yes | some_command_with_confirmations
.
Write to the stdin of the created process the character 'y' and, if necessary, '\n'. See the subprocess doc for how to get a link to the stdin of a process.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question