How to enter a password into the terminal password prompt in PythonHow can I represent an 'Enum' in Python?Calling an external command in PythonHow do I prompt for Yes/No/Cancel input in a Linux shell script?How can I pretty-print JSON in a shell script?How do I split a string on a delimiter in Bash?How to concatenate string variables in BashHow do I set a variable to the output of a command in Bash?How to copy a folder from remote to local using scp?How to import an SQL file using the command line in MySQL?Why is “1000000000000000 in range(1000000000000001)” so fast in Python 3?
How would modern naval warfare have to have developed differently for battleships to still be relevant in the 21st century?
Unusual mail headers, evidence of an attempted attack. Have I been pwned?
Require advice on power conservation for backpacking trip
What does the hyphen "-" mean in "tar xzf -"?
Find the diameter of a word graph
How do I set an alias to a terminal line?
Should developer taking test phones home or put in office?
Does this Wild Magic result affect the sorcerer or just other creatures?
STM Microcontroller burns every time
(Newbie question)Why is voltage measurement of circuit different when switch is on?
Tantum religio potuit suadere malorum – Lucretius
Given a cell in a Voronoi diagram, which point did it come from?
3D Crossword, Cryptic, Statue View & Maze
How was Hillel permitted to go to the skylight to hear the shiur
Links to webpages in books
How dangerous are set-size assumptions?
What does "play with your toy’s toys" mean?
What is the better way to use Optional in conditions?
How to get cool night-vision without lame drawbacks?
Can humans ever directly see a few photons at a time? Can a human see a single photon?
Swapping rooks in a 4x4 board
How can I politely work my way around not liking coffee or beer when it comes to professional networking?
Except duplicates from duplicates based on columns
Is it damaging to turn off a small fridge for two days every week?
How to enter a password into the terminal password prompt in Python
How can I represent an 'Enum' in Python?Calling an external command in PythonHow do I prompt for Yes/No/Cancel input in a Linux shell script?How can I pretty-print JSON in a shell script?How do I split a string on a delimiter in Bash?How to concatenate string variables in BashHow do I set a variable to the output of a command in Bash?How to copy a folder from remote to local using scp?How to import an SQL file using the command line in MySQL?Why is “1000000000000000 in range(1000000000000001)” so fast in Python 3?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I am trying to make a simple Python script that enters a given password in the command line after using the 'su' command (or any other command which requires administrator privileges or simply requires a password in order to be executed).
I tried to use the Subprocess module for this as well as pynput, but haven't been able to figure it out.
import subprocess
import os
# os.system('su') # Tried using this too
process = subprocess.Popen('su', stdin=subprocess.PIPE, stdout=subprocess.PIPE)
process.stdin.write(b"password_to_enter")
print(process.communicate()[0])
process.stdin.close()
I was expecting this to enter 'password_to_enter' in the given password prompt after typing the 'su' command, but it didn't. I tried giving it the correct password as well but still did not work.
What am I doing wrong?
PS: I am on Mac
python-3.x shell command-line subprocess output
add a comment |
I am trying to make a simple Python script that enters a given password in the command line after using the 'su' command (or any other command which requires administrator privileges or simply requires a password in order to be executed).
I tried to use the Subprocess module for this as well as pynput, but haven't been able to figure it out.
import subprocess
import os
# os.system('su') # Tried using this too
process = subprocess.Popen('su', stdin=subprocess.PIPE, stdout=subprocess.PIPE)
process.stdin.write(b"password_to_enter")
print(process.communicate()[0])
process.stdin.close()
I was expecting this to enter 'password_to_enter' in the given password prompt after typing the 'su' command, but it didn't. I tried giving it the correct password as well but still did not work.
What am I doing wrong?
PS: I am on Mac
python-3.x shell command-line subprocess output
add a comment |
I am trying to make a simple Python script that enters a given password in the command line after using the 'su' command (or any other command which requires administrator privileges or simply requires a password in order to be executed).
I tried to use the Subprocess module for this as well as pynput, but haven't been able to figure it out.
import subprocess
import os
# os.system('su') # Tried using this too
process = subprocess.Popen('su', stdin=subprocess.PIPE, stdout=subprocess.PIPE)
process.stdin.write(b"password_to_enter")
print(process.communicate()[0])
process.stdin.close()
I was expecting this to enter 'password_to_enter' in the given password prompt after typing the 'su' command, but it didn't. I tried giving it the correct password as well but still did not work.
What am I doing wrong?
PS: I am on Mac
python-3.x shell command-line subprocess output
I am trying to make a simple Python script that enters a given password in the command line after using the 'su' command (or any other command which requires administrator privileges or simply requires a password in order to be executed).
I tried to use the Subprocess module for this as well as pynput, but haven't been able to figure it out.
import subprocess
import os
# os.system('su') # Tried using this too
process = subprocess.Popen('su', stdin=subprocess.PIPE, stdout=subprocess.PIPE)
process.stdin.write(b"password_to_enter")
print(process.communicate()[0])
process.stdin.close()
I was expecting this to enter 'password_to_enter' in the given password prompt after typing the 'su' command, but it didn't. I tried giving it the correct password as well but still did not work.
What am I doing wrong?
PS: I am on Mac
python-3.x shell command-line subprocess output
python-3.x shell command-line subprocess output
asked Mar 25 at 9:00
FedeCuciFedeCuci
183 bronze badges
183 bronze badges
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
The su command expects to be reading from a terminal. Running your example above on my Linux machine returns the following error:
su: must be run from a terminal
This is because su tries to make sure it's being run from a terminal. You can bypass this by allocating a pty and managing input and output yourself, but getting this right can be pretty tricky because you can't enter the password until after su prompts for it. For example:
import subprocess
import os
import pty
import time
# Allocate the pty to talk to su with.
master, slave = pty.openpty()
# Open the process, pass in the slave pty as stdin.
process = subprocess.Popen('su', stdin=slave, stdout=subprocess.PIPE, shell=True)
# Make sure we wait for the "Password:" prompt.
# The correct way to do this is to read from stdout and wait until the message is printed.
time.sleep(2)
# Open a write handle to the master end of the pty to write to.
pin = os.fdopen(master, "w")
pin.write("password_to_entern")
pin.flush()
# Clean up
print(process.communicate()[0])
pin.close()
os.close(slave)
There's a library called pexpect that makes interacting with interactive applications pretty simple:
import pexpect
import sys
child = pexpect.spawn("su")
child.logfile_read = sys.stdout
child.expect("Password:")
child.sendline("your-password-here")
child.expect("#")
child.sendline("whoami")
child.expect("#")
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55334273%2fhow-to-enter-a-password-into-the-terminal-password-prompt-in-python%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
The su command expects to be reading from a terminal. Running your example above on my Linux machine returns the following error:
su: must be run from a terminal
This is because su tries to make sure it's being run from a terminal. You can bypass this by allocating a pty and managing input and output yourself, but getting this right can be pretty tricky because you can't enter the password until after su prompts for it. For example:
import subprocess
import os
import pty
import time
# Allocate the pty to talk to su with.
master, slave = pty.openpty()
# Open the process, pass in the slave pty as stdin.
process = subprocess.Popen('su', stdin=slave, stdout=subprocess.PIPE, shell=True)
# Make sure we wait for the "Password:" prompt.
# The correct way to do this is to read from stdout and wait until the message is printed.
time.sleep(2)
# Open a write handle to the master end of the pty to write to.
pin = os.fdopen(master, "w")
pin.write("password_to_entern")
pin.flush()
# Clean up
print(process.communicate()[0])
pin.close()
os.close(slave)
There's a library called pexpect that makes interacting with interactive applications pretty simple:
import pexpect
import sys
child = pexpect.spawn("su")
child.logfile_read = sys.stdout
child.expect("Password:")
child.sendline("your-password-here")
child.expect("#")
child.sendline("whoami")
child.expect("#")
add a comment |
The su command expects to be reading from a terminal. Running your example above on my Linux machine returns the following error:
su: must be run from a terminal
This is because su tries to make sure it's being run from a terminal. You can bypass this by allocating a pty and managing input and output yourself, but getting this right can be pretty tricky because you can't enter the password until after su prompts for it. For example:
import subprocess
import os
import pty
import time
# Allocate the pty to talk to su with.
master, slave = pty.openpty()
# Open the process, pass in the slave pty as stdin.
process = subprocess.Popen('su', stdin=slave, stdout=subprocess.PIPE, shell=True)
# Make sure we wait for the "Password:" prompt.
# The correct way to do this is to read from stdout and wait until the message is printed.
time.sleep(2)
# Open a write handle to the master end of the pty to write to.
pin = os.fdopen(master, "w")
pin.write("password_to_entern")
pin.flush()
# Clean up
print(process.communicate()[0])
pin.close()
os.close(slave)
There's a library called pexpect that makes interacting with interactive applications pretty simple:
import pexpect
import sys
child = pexpect.spawn("su")
child.logfile_read = sys.stdout
child.expect("Password:")
child.sendline("your-password-here")
child.expect("#")
child.sendline("whoami")
child.expect("#")
add a comment |
The su command expects to be reading from a terminal. Running your example above on my Linux machine returns the following error:
su: must be run from a terminal
This is because su tries to make sure it's being run from a terminal. You can bypass this by allocating a pty and managing input and output yourself, but getting this right can be pretty tricky because you can't enter the password until after su prompts for it. For example:
import subprocess
import os
import pty
import time
# Allocate the pty to talk to su with.
master, slave = pty.openpty()
# Open the process, pass in the slave pty as stdin.
process = subprocess.Popen('su', stdin=slave, stdout=subprocess.PIPE, shell=True)
# Make sure we wait for the "Password:" prompt.
# The correct way to do this is to read from stdout and wait until the message is printed.
time.sleep(2)
# Open a write handle to the master end of the pty to write to.
pin = os.fdopen(master, "w")
pin.write("password_to_entern")
pin.flush()
# Clean up
print(process.communicate()[0])
pin.close()
os.close(slave)
There's a library called pexpect that makes interacting with interactive applications pretty simple:
import pexpect
import sys
child = pexpect.spawn("su")
child.logfile_read = sys.stdout
child.expect("Password:")
child.sendline("your-password-here")
child.expect("#")
child.sendline("whoami")
child.expect("#")
The su command expects to be reading from a terminal. Running your example above on my Linux machine returns the following error:
su: must be run from a terminal
This is because su tries to make sure it's being run from a terminal. You can bypass this by allocating a pty and managing input and output yourself, but getting this right can be pretty tricky because you can't enter the password until after su prompts for it. For example:
import subprocess
import os
import pty
import time
# Allocate the pty to talk to su with.
master, slave = pty.openpty()
# Open the process, pass in the slave pty as stdin.
process = subprocess.Popen('su', stdin=slave, stdout=subprocess.PIPE, shell=True)
# Make sure we wait for the "Password:" prompt.
# The correct way to do this is to read from stdout and wait until the message is printed.
time.sleep(2)
# Open a write handle to the master end of the pty to write to.
pin = os.fdopen(master, "w")
pin.write("password_to_entern")
pin.flush()
# Clean up
print(process.communicate()[0])
pin.close()
os.close(slave)
There's a library called pexpect that makes interacting with interactive applications pretty simple:
import pexpect
import sys
child = pexpect.spawn("su")
child.logfile_read = sys.stdout
child.expect("Password:")
child.sendline("your-password-here")
child.expect("#")
child.sendline("whoami")
child.expect("#")
answered Mar 25 at 19:59
Michael PowersMichael Powers
1,4651 gold badge2 silver badges10 bronze badges
1,4651 gold badge2 silver badges10 bronze badges
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55334273%2fhow-to-enter-a-password-into-the-terminal-password-prompt-in-python%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown