Python Forum

Full Version: write the password in the Terminal
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Dear sirs ,

I'm new with python , but i have some backgroud in programming ,
i want to automate some task at work ,i'm using python 3 with linux opensuse
i'm using the "os" library , but i cant do something :
Q : how to write the necessary password when the terminal (linux terminal) ask you to type it ?

Regards
Perhaps you can use subprocess.
Here is an example from here
I'm on my windows box but can be edited for linux

import subprocess

p = subprocess.Popen(['dir'], shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
p.stdin.write(b'mypassword\n')
result = p.stdout.read()
print(result)
Thank You
(Mar-16-2023, 08:50 AM)mohamed_touil Wrote: [ -> ]Q : how to write the necessary password when the terminal (linux terminal) ask you to type it ?

Sounds you want to use tools, which do not accept passwords from arguments. SSH is one of the examples.
Some people use expect. I don't like this technique because it relies on external tools and the language settings of your OS.

https://pexpect.readthedocs.io/en/stable/

For example, I prefer paramiko for ssh.
Other protocols like ftp, http are also supported by Python directly.

If it's a complete different tool, you want to run, you may find a Python implementation of this tool on pypi.