subprocess.Popen keeps on asking password for ssh-copy-id command
I have gone through multiple threads which has similar questions (Use subprocess to send a password) and have tried number of things but still i am not able to get it to work. Basically i want to push my ssh-keys to a bunch of machines and i am trying to do that with subprocess. But somehow subprocess.Popen fails to get the password and hence it gets stuck.
Below are some of things I have tried.
from subprocess import Popen, PIPE
p = Popen(['ssh-copy-id', 'testbox1'], stdin=PIPE, stdout=PIPE).communicate(input=b'mypassword')
I have also tried supplying the password by writing to process's stdin channel like below
p.stdin.write(b'mypassword')
p.stdin.flush()
I have tried this in both python 2.7 and python and it didn't work. I have also tried providing a linefeed as well after the password but even that didn't work. I am not sure what i am missing here.
I know people have suggested to use Pexpect for this but then again i am more keen in knowing why subprocess can't handle this.
I know there are multiple libraries like Paramiko and also fabric which handles remote connections with much ease, but i don't think that can be used in this case as i am not directly sshing to a machine and rather using ssh-copy-id command from my local machine
python ssh subprocess
|
show 8 more comments
I have gone through multiple threads which has similar questions (Use subprocess to send a password) and have tried number of things but still i am not able to get it to work. Basically i want to push my ssh-keys to a bunch of machines and i am trying to do that with subprocess. But somehow subprocess.Popen fails to get the password and hence it gets stuck.
Below are some of things I have tried.
from subprocess import Popen, PIPE
p = Popen(['ssh-copy-id', 'testbox1'], stdin=PIPE, stdout=PIPE).communicate(input=b'mypassword')
I have also tried supplying the password by writing to process's stdin channel like below
p.stdin.write(b'mypassword')
p.stdin.flush()
I have tried this in both python 2.7 and python and it didn't work. I have also tried providing a linefeed as well after the password but even that didn't work. I am not sure what i am missing here.
I know people have suggested to use Pexpect for this but then again i am more keen in knowing why subprocess can't handle this.
I know there are multiple libraries like Paramiko and also fabric which handles remote connections with much ease, but i don't think that can be used in this case as i am not directly sshing to a machine and rather using ssh-copy-id command from my local machine
python ssh subprocess
I would do some debugging first. See unix.stackexchange.com/questions/36540/… for a decent starting place.
– Chris Beard
Nov 28 '18 at 14:08
1
you could tryp.stdin.write(b'mypasswordn')(with a linefeed). But those silent entry methods are sometimes more low-level, and it doesn't work. Check if the command doesn't take a password as parameter (plinkfrom Putty does)
– Jean-François Fabre♦
Nov 28 '18 at 14:09
@Jean-FrançoisFabre i tried with linefeed as well and it didn't work.
– Rohit
Nov 28 '18 at 14:16
@Rohit Give this a try? serverfault.com/a/306675
– Chris Beard
Nov 28 '18 at 14:17
1
ssh-copy-idisn't reading from standard input; it's trying to read directly from the terminal.
– chepner
Nov 28 '18 at 14:22
|
show 8 more comments
I have gone through multiple threads which has similar questions (Use subprocess to send a password) and have tried number of things but still i am not able to get it to work. Basically i want to push my ssh-keys to a bunch of machines and i am trying to do that with subprocess. But somehow subprocess.Popen fails to get the password and hence it gets stuck.
Below are some of things I have tried.
from subprocess import Popen, PIPE
p = Popen(['ssh-copy-id', 'testbox1'], stdin=PIPE, stdout=PIPE).communicate(input=b'mypassword')
I have also tried supplying the password by writing to process's stdin channel like below
p.stdin.write(b'mypassword')
p.stdin.flush()
I have tried this in both python 2.7 and python and it didn't work. I have also tried providing a linefeed as well after the password but even that didn't work. I am not sure what i am missing here.
I know people have suggested to use Pexpect for this but then again i am more keen in knowing why subprocess can't handle this.
I know there are multiple libraries like Paramiko and also fabric which handles remote connections with much ease, but i don't think that can be used in this case as i am not directly sshing to a machine and rather using ssh-copy-id command from my local machine
python ssh subprocess
I have gone through multiple threads which has similar questions (Use subprocess to send a password) and have tried number of things but still i am not able to get it to work. Basically i want to push my ssh-keys to a bunch of machines and i am trying to do that with subprocess. But somehow subprocess.Popen fails to get the password and hence it gets stuck.
Below are some of things I have tried.
from subprocess import Popen, PIPE
p = Popen(['ssh-copy-id', 'testbox1'], stdin=PIPE, stdout=PIPE).communicate(input=b'mypassword')
I have also tried supplying the password by writing to process's stdin channel like below
p.stdin.write(b'mypassword')
p.stdin.flush()
I have tried this in both python 2.7 and python and it didn't work. I have also tried providing a linefeed as well after the password but even that didn't work. I am not sure what i am missing here.
I know people have suggested to use Pexpect for this but then again i am more keen in knowing why subprocess can't handle this.
I know there are multiple libraries like Paramiko and also fabric which handles remote connections with much ease, but i don't think that can be used in this case as i am not directly sshing to a machine and rather using ssh-copy-id command from my local machine
python ssh subprocess
python ssh subprocess
edited Nov 28 '18 at 15:32
Jean-François Fabre♦
106k1057115
106k1057115
asked Nov 28 '18 at 14:01
RohitRohit
895216
895216
I would do some debugging first. See unix.stackexchange.com/questions/36540/… for a decent starting place.
– Chris Beard
Nov 28 '18 at 14:08
1
you could tryp.stdin.write(b'mypasswordn')(with a linefeed). But those silent entry methods are sometimes more low-level, and it doesn't work. Check if the command doesn't take a password as parameter (plinkfrom Putty does)
– Jean-François Fabre♦
Nov 28 '18 at 14:09
@Jean-FrançoisFabre i tried with linefeed as well and it didn't work.
– Rohit
Nov 28 '18 at 14:16
@Rohit Give this a try? serverfault.com/a/306675
– Chris Beard
Nov 28 '18 at 14:17
1
ssh-copy-idisn't reading from standard input; it's trying to read directly from the terminal.
– chepner
Nov 28 '18 at 14:22
|
show 8 more comments
I would do some debugging first. See unix.stackexchange.com/questions/36540/… for a decent starting place.
– Chris Beard
Nov 28 '18 at 14:08
1
you could tryp.stdin.write(b'mypasswordn')(with a linefeed). But those silent entry methods are sometimes more low-level, and it doesn't work. Check if the command doesn't take a password as parameter (plinkfrom Putty does)
– Jean-François Fabre♦
Nov 28 '18 at 14:09
@Jean-FrançoisFabre i tried with linefeed as well and it didn't work.
– Rohit
Nov 28 '18 at 14:16
@Rohit Give this a try? serverfault.com/a/306675
– Chris Beard
Nov 28 '18 at 14:17
1
ssh-copy-idisn't reading from standard input; it's trying to read directly from the terminal.
– chepner
Nov 28 '18 at 14:22
I would do some debugging first. See unix.stackexchange.com/questions/36540/… for a decent starting place.
– Chris Beard
Nov 28 '18 at 14:08
I would do some debugging first. See unix.stackexchange.com/questions/36540/… for a decent starting place.
– Chris Beard
Nov 28 '18 at 14:08
1
1
you could try
p.stdin.write(b'mypasswordn') (with a linefeed). But those silent entry methods are sometimes more low-level, and it doesn't work. Check if the command doesn't take a password as parameter (plink from Putty does)– Jean-François Fabre♦
Nov 28 '18 at 14:09
you could try
p.stdin.write(b'mypasswordn') (with a linefeed). But those silent entry methods are sometimes more low-level, and it doesn't work. Check if the command doesn't take a password as parameter (plink from Putty does)– Jean-François Fabre♦
Nov 28 '18 at 14:09
@Jean-FrançoisFabre i tried with linefeed as well and it didn't work.
– Rohit
Nov 28 '18 at 14:16
@Jean-FrançoisFabre i tried with linefeed as well and it didn't work.
– Rohit
Nov 28 '18 at 14:16
@Rohit Give this a try? serverfault.com/a/306675
– Chris Beard
Nov 28 '18 at 14:17
@Rohit Give this a try? serverfault.com/a/306675
– Chris Beard
Nov 28 '18 at 14:17
1
1
ssh-copy-id isn't reading from standard input; it's trying to read directly from the terminal.– chepner
Nov 28 '18 at 14:22
ssh-copy-id isn't reading from standard input; it's trying to read directly from the terminal.– chepner
Nov 28 '18 at 14:22
|
show 8 more comments
1 Answer
1
active
oldest
votes
It seemed it was way to tricky to be handled with subprocess and hence i had to pexpect to solve this and it worked in first go.
import pexpect
from getpass import getpass
pwd = getpass("password: ")
child = pexpect.spawn('ssh-copy-id testbox1')
child.expect('.*ssword.*:')
child.sendline(pwd)
child.expect(pexpect.EOF, timeout=None)
cmd_show_data = child.before
cmd_output = cmd_show_data.split('rn')
for data in cmd_output:
print data
good call. Edited your question title for future readers/searchers.
– Jean-François Fabre♦
Nov 28 '18 at 15:32
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%2f53521203%2fsubprocess-popen-keeps-on-asking-password-for-ssh-copy-id-command%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
It seemed it was way to tricky to be handled with subprocess and hence i had to pexpect to solve this and it worked in first go.
import pexpect
from getpass import getpass
pwd = getpass("password: ")
child = pexpect.spawn('ssh-copy-id testbox1')
child.expect('.*ssword.*:')
child.sendline(pwd)
child.expect(pexpect.EOF, timeout=None)
cmd_show_data = child.before
cmd_output = cmd_show_data.split('rn')
for data in cmd_output:
print data
good call. Edited your question title for future readers/searchers.
– Jean-François Fabre♦
Nov 28 '18 at 15:32
add a comment |
It seemed it was way to tricky to be handled with subprocess and hence i had to pexpect to solve this and it worked in first go.
import pexpect
from getpass import getpass
pwd = getpass("password: ")
child = pexpect.spawn('ssh-copy-id testbox1')
child.expect('.*ssword.*:')
child.sendline(pwd)
child.expect(pexpect.EOF, timeout=None)
cmd_show_data = child.before
cmd_output = cmd_show_data.split('rn')
for data in cmd_output:
print data
good call. Edited your question title for future readers/searchers.
– Jean-François Fabre♦
Nov 28 '18 at 15:32
add a comment |
It seemed it was way to tricky to be handled with subprocess and hence i had to pexpect to solve this and it worked in first go.
import pexpect
from getpass import getpass
pwd = getpass("password: ")
child = pexpect.spawn('ssh-copy-id testbox1')
child.expect('.*ssword.*:')
child.sendline(pwd)
child.expect(pexpect.EOF, timeout=None)
cmd_show_data = child.before
cmd_output = cmd_show_data.split('rn')
for data in cmd_output:
print data
It seemed it was way to tricky to be handled with subprocess and hence i had to pexpect to solve this and it worked in first go.
import pexpect
from getpass import getpass
pwd = getpass("password: ")
child = pexpect.spawn('ssh-copy-id testbox1')
child.expect('.*ssword.*:')
child.sendline(pwd)
child.expect(pexpect.EOF, timeout=None)
cmd_show_data = child.before
cmd_output = cmd_show_data.split('rn')
for data in cmd_output:
print data
answered Nov 28 '18 at 15:15
RohitRohit
895216
895216
good call. Edited your question title for future readers/searchers.
– Jean-François Fabre♦
Nov 28 '18 at 15:32
add a comment |
good call. Edited your question title for future readers/searchers.
– Jean-François Fabre♦
Nov 28 '18 at 15:32
good call. Edited your question title for future readers/searchers.
– Jean-François Fabre♦
Nov 28 '18 at 15:32
good call. Edited your question title for future readers/searchers.
– Jean-François Fabre♦
Nov 28 '18 at 15:32
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%2f53521203%2fsubprocess-popen-keeps-on-asking-password-for-ssh-copy-id-command%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
I would do some debugging first. See unix.stackexchange.com/questions/36540/… for a decent starting place.
– Chris Beard
Nov 28 '18 at 14:08
1
you could try
p.stdin.write(b'mypasswordn')(with a linefeed). But those silent entry methods are sometimes more low-level, and it doesn't work. Check if the command doesn't take a password as parameter (plinkfrom Putty does)– Jean-François Fabre♦
Nov 28 '18 at 14:09
@Jean-FrançoisFabre i tried with linefeed as well and it didn't work.
– Rohit
Nov 28 '18 at 14:16
@Rohit Give this a try? serverfault.com/a/306675
– Chris Beard
Nov 28 '18 at 14:17
1
ssh-copy-idisn't reading from standard input; it's trying to read directly from the terminal.– chepner
Nov 28 '18 at 14:22