While working with subrpocess module I try to run the .call()
method by passing two parameters
Eg.
subrpocess.call("sudo ifconfig" , shell=True);
But when I run the program its asking for password, could you please tell me how to pass it?

- 3,915
- 2
- 9
- 25

- 11
- 4
-
I don't have the root privileges so every time I ran any system command I need to put in password so is there any provision to pass password? – Shwetang Rangari Feb 22 '20 at 09:28
-
I improved my answer. It turns out, I, a long-time Linux user, am not an expert on sudo, and I am learning quite a bit from the linked reference. – hellork Feb 22 '20 at 11:17
1 Answers
Use the -S
option to pipe the password to sudo
.
echo "mypass" | sudo -S ifconfig...
or with Python subprocess, according to the duplicate answer:
call('echo {} | sudo -S {}'.format("mypass", "ifconfig..."), shell=True)
New users tend to overuse sudo. Sudo runs commands with the security privileges of another user. Sudo defaults to superuser or root privileges.
Administrators can configure which users are permitted to run which commands as another user, with audit trails and logs. The file, /etc/sudoers
configures who can use sudo without a password, among other things. More...
Another way to send a password to sudo
during invocation is to use the -A
or --askpass
option. Sudo uses the environment variable, SUDO_ASKPASS
to tell sudo
where to look for a program to supply a password on the standard output. One could use a graphical program like zenity to pop up a window asking for a password. Or simply create a script to echo the password. Although this works as a demo, it is probably a bad idea to have plain text passwords sitting around on the filesystem.
$ export SUDO_ASKPASS=$(pwd)/mypass.sh
$ echo "echo mypassword" > mypass.sh; chmod +x mypass.sh
$ sudo -A -u testuser bash
# whoami
testuser
Sudo has many command options that were unknown prior to answering this question. Type man sudo
to see what this author learned today.

- 324
- 2
- 8