Pexpect ssh tunnel with an interactive logon to a host via a proxy doesn't accept the password
I'm writing a script that would have to run a remote script on a bunch of remote private hosts in different domains.
Each of the hosts being is isolated from the rest of the infrastructure. They are running in a private network segment that is only exposed through a proxy jumphost I need to ssh via. The authentication to the jumpbox is passwordless, so there're no issues with that, however the second hop I do in a subshell to the actual host I need to execute the script on doesn't really accept the password. I suspect this to be an issue with either the way I handle my pexpect tunnel, or how I escape my passwords (containing all sorts of special characters and generated dynamically).
cmd = 'ssh -o ProxyCommand="ssh -o StrictHostKeyChecking=no {}@a{}@{} nc {}} 22" -o StrictHostKeyChecking=no {}@a{}@{} -t sudo su'.format(username, domain, jumphost, remotehost, username, domain, remotehost)
child = pexpect.spawn(cmd)
child.expect('.*')
child.sendline('yes')
child.expect('.*')
child.sendline('yes')
child.expect('assword:')
child.sendline(password)
child.expect('assword')
child.sendline(password)
child.expect('root@.*# ')
child.sendline('<execute remote code here>')
Whenever I do tests from the console and at any given point I do child.interact() I always get the password prompt, however the expect statements always return 0 (the index of the expect expression in the provided list) and I get back the number of bytes I'm writing, but upon interact() I always get a password prompt. What am I missing here?
python ssh pexpect ssh-tunnel
add a comment |
I'm writing a script that would have to run a remote script on a bunch of remote private hosts in different domains.
Each of the hosts being is isolated from the rest of the infrastructure. They are running in a private network segment that is only exposed through a proxy jumphost I need to ssh via. The authentication to the jumpbox is passwordless, so there're no issues with that, however the second hop I do in a subshell to the actual host I need to execute the script on doesn't really accept the password. I suspect this to be an issue with either the way I handle my pexpect tunnel, or how I escape my passwords (containing all sorts of special characters and generated dynamically).
cmd = 'ssh -o ProxyCommand="ssh -o StrictHostKeyChecking=no {}@a{}@{} nc {}} 22" -o StrictHostKeyChecking=no {}@a{}@{} -t sudo su'.format(username, domain, jumphost, remotehost, username, domain, remotehost)
child = pexpect.spawn(cmd)
child.expect('.*')
child.sendline('yes')
child.expect('.*')
child.sendline('yes')
child.expect('assword:')
child.sendline(password)
child.expect('assword')
child.sendline(password)
child.expect('root@.*# ')
child.sendline('<execute remote code here>')
Whenever I do tests from the console and at any given point I do child.interact() I always get the password prompt, however the expect statements always return 0 (the index of the expect expression in the provided list) and I get back the number of bytes I'm writing, but upon interact() I always get a password prompt. What am I missing here?
python ssh pexpect ssh-tunnel
add a comment |
I'm writing a script that would have to run a remote script on a bunch of remote private hosts in different domains.
Each of the hosts being is isolated from the rest of the infrastructure. They are running in a private network segment that is only exposed through a proxy jumphost I need to ssh via. The authentication to the jumpbox is passwordless, so there're no issues with that, however the second hop I do in a subshell to the actual host I need to execute the script on doesn't really accept the password. I suspect this to be an issue with either the way I handle my pexpect tunnel, or how I escape my passwords (containing all sorts of special characters and generated dynamically).
cmd = 'ssh -o ProxyCommand="ssh -o StrictHostKeyChecking=no {}@a{}@{} nc {}} 22" -o StrictHostKeyChecking=no {}@a{}@{} -t sudo su'.format(username, domain, jumphost, remotehost, username, domain, remotehost)
child = pexpect.spawn(cmd)
child.expect('.*')
child.sendline('yes')
child.expect('.*')
child.sendline('yes')
child.expect('assword:')
child.sendline(password)
child.expect('assword')
child.sendline(password)
child.expect('root@.*# ')
child.sendline('<execute remote code here>')
Whenever I do tests from the console and at any given point I do child.interact() I always get the password prompt, however the expect statements always return 0 (the index of the expect expression in the provided list) and I get back the number of bytes I'm writing, but upon interact() I always get a password prompt. What am I missing here?
python ssh pexpect ssh-tunnel
I'm writing a script that would have to run a remote script on a bunch of remote private hosts in different domains.
Each of the hosts being is isolated from the rest of the infrastructure. They are running in a private network segment that is only exposed through a proxy jumphost I need to ssh via. The authentication to the jumpbox is passwordless, so there're no issues with that, however the second hop I do in a subshell to the actual host I need to execute the script on doesn't really accept the password. I suspect this to be an issue with either the way I handle my pexpect tunnel, or how I escape my passwords (containing all sorts of special characters and generated dynamically).
cmd = 'ssh -o ProxyCommand="ssh -o StrictHostKeyChecking=no {}@a{}@{} nc {}} 22" -o StrictHostKeyChecking=no {}@a{}@{} -t sudo su'.format(username, domain, jumphost, remotehost, username, domain, remotehost)
child = pexpect.spawn(cmd)
child.expect('.*')
child.sendline('yes')
child.expect('.*')
child.sendline('yes')
child.expect('assword:')
child.sendline(password)
child.expect('assword')
child.sendline(password)
child.expect('root@.*# ')
child.sendline('<execute remote code here>')
Whenever I do tests from the console and at any given point I do child.interact() I always get the password prompt, however the expect statements always return 0 (the index of the expect expression in the provided list) and I get back the number of bytes I'm writing, but upon interact() I always get a password prompt. What am I missing here?
python ssh pexpect ssh-tunnel
python ssh pexpect ssh-tunnel
asked Jan 18 at 11:55
vlexvlex
8610
8610
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
I figured it out - sendline() sends just whatever arguments pass to it and a newline char, but doesn't send a return carriage character as well. All I had to do is either sendline(password+'r') or send(password+'rn').
I was assuming (apparently wrong) that sendline would append a r AND n to the line, not just the n. Let this be a lesson for someone else who's stuck on the same stupid thing.
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%2f54253526%2fpexpect-ssh-tunnel-with-an-interactive-logon-to-a-host-via-a-proxy-doesnt-accep%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
I figured it out - sendline() sends just whatever arguments pass to it and a newline char, but doesn't send a return carriage character as well. All I had to do is either sendline(password+'r') or send(password+'rn').
I was assuming (apparently wrong) that sendline would append a r AND n to the line, not just the n. Let this be a lesson for someone else who's stuck on the same stupid thing.
add a comment |
I figured it out - sendline() sends just whatever arguments pass to it and a newline char, but doesn't send a return carriage character as well. All I had to do is either sendline(password+'r') or send(password+'rn').
I was assuming (apparently wrong) that sendline would append a r AND n to the line, not just the n. Let this be a lesson for someone else who's stuck on the same stupid thing.
add a comment |
I figured it out - sendline() sends just whatever arguments pass to it and a newline char, but doesn't send a return carriage character as well. All I had to do is either sendline(password+'r') or send(password+'rn').
I was assuming (apparently wrong) that sendline would append a r AND n to the line, not just the n. Let this be a lesson for someone else who's stuck on the same stupid thing.
I figured it out - sendline() sends just whatever arguments pass to it and a newline char, but doesn't send a return carriage character as well. All I had to do is either sendline(password+'r') or send(password+'rn').
I was assuming (apparently wrong) that sendline would append a r AND n to the line, not just the n. Let this be a lesson for someone else who's stuck on the same stupid thing.
answered Jan 18 at 16:08
vlexvlex
8610
8610
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%2f54253526%2fpexpect-ssh-tunnel-with-an-interactive-logon-to-a-host-via-a-proxy-doesnt-accep%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