how to use expect with bash
I Have created a shell script as given below.This script will login to a remote server as a normal user then switch to root user for creating a directory.The script is as given below.
ssh -t qbadmin@10.3.2.0 '
su root -c "
echo "Give Directory name :";
read dir;
mkdir $dir;
";
'
Here the script will ask Password for normal user first.Then again it will ask for root Password.How could I automate this using expect
command.I want to supply the password automatically for the root user only.I think it can be done with expect
.
Please do help me.
command-line bash ssh scripts
add a comment |
I Have created a shell script as given below.This script will login to a remote server as a normal user then switch to root user for creating a directory.The script is as given below.
ssh -t qbadmin@10.3.2.0 '
su root -c "
echo "Give Directory name :";
read dir;
mkdir $dir;
";
'
Here the script will ask Password for normal user first.Then again it will ask for root Password.How could I automate this using expect
command.I want to supply the password automatically for the root user only.I think it can be done with expect
.
Please do help me.
command-line bash ssh scripts
What about authorized keys? help.ubuntu.com/community/SSH/OpenSSH/Keys More security!
– prophecy201
Mar 19 '13 at 10:52
add a comment |
I Have created a shell script as given below.This script will login to a remote server as a normal user then switch to root user for creating a directory.The script is as given below.
ssh -t qbadmin@10.3.2.0 '
su root -c "
echo "Give Directory name :";
read dir;
mkdir $dir;
";
'
Here the script will ask Password for normal user first.Then again it will ask for root Password.How could I automate this using expect
command.I want to supply the password automatically for the root user only.I think it can be done with expect
.
Please do help me.
command-line bash ssh scripts
I Have created a shell script as given below.This script will login to a remote server as a normal user then switch to root user for creating a directory.The script is as given below.
ssh -t qbadmin@10.3.2.0 '
su root -c "
echo "Give Directory name :";
read dir;
mkdir $dir;
";
'
Here the script will ask Password for normal user first.Then again it will ask for root Password.How could I automate this using expect
command.I want to supply the password automatically for the root user only.I think it can be done with expect
.
Please do help me.
command-line bash ssh scripts
command-line bash ssh scripts
asked Mar 19 '13 at 10:22
Uvais IbrahimUvais Ibrahim
2091413
2091413
What about authorized keys? help.ubuntu.com/community/SSH/OpenSSH/Keys More security!
– prophecy201
Mar 19 '13 at 10:52
add a comment |
What about authorized keys? help.ubuntu.com/community/SSH/OpenSSH/Keys More security!
– prophecy201
Mar 19 '13 at 10:52
What about authorized keys? help.ubuntu.com/community/SSH/OpenSSH/Keys More security!
– prophecy201
Mar 19 '13 at 10:52
What about authorized keys? help.ubuntu.com/community/SSH/OpenSSH/Keys More security!
– prophecy201
Mar 19 '13 at 10:52
add a comment |
3 Answers
3
active
oldest
votes
A better/easier way than using expect is SSHPASS, which sends a password through with an SSH request:
sshpass -p <PASSWORD> ssh <USER>@<SERVER> <SSH Command>
PASSWORD is the password to be entered when prompted, USER is the username, SERVER is the server IP Address (eg. 10.8.100.100) and SSH Command is the command you want to execute.
For your example:
sshpass -p <PASSWORD> ssh qbadmin@10.3.2.0 mkdir <full path for new dir>
To install SSHPASS:
sudo apt-get install sshpass
If you are wanting the SSH session to remain afterwards than you can use expect with the following,
#!/bin/bash
##Enter Username and Password Details:
userName=<UserName>
password=<Password>
expect -c "
spawn ssh ${userName}@10.8.100.100 ##put your own IP here
expect "password: " ##or whatever password prompt you get
send "$passwordr"
expect -re "Last Login: " ##or whatever the end of your welcome message is
send "su -i"
expect "password for <UserName>"
send "$passwordr"
"
This will leave the root access logged in, however until the script sends a return statement it will hold control until you hit Ctrl+c. But this should answer your question.
Resurrect: Are your double quotes inside supposed to be escaped?
– Relic
Nov 15 '17 at 0:40
add a comment |
If you want to pass only the root password, you have to
- either configure a password-less login into the user account, e.g. using public key authentication and an ssh agent on the client side,
- or configure the server (i.e. to allow login as root, and then do
ssh -t root@10.3.2.0 …
add a comment |
I don't see an accepted answer years later so I will also add something that may be helpful to others. My situation had nothing to do with SSH, but using spawn expect in the following way I was able to get it working.
expect -c "
spawn myProc
expect "Do you wish to continue with XYZ? (y/N)" {
send "nr"
exp_continue
}
"
Hope this helps.
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "89"
};
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%2faskubuntu.com%2fquestions%2f269878%2fhow-to-use-expect-with-bash%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
A better/easier way than using expect is SSHPASS, which sends a password through with an SSH request:
sshpass -p <PASSWORD> ssh <USER>@<SERVER> <SSH Command>
PASSWORD is the password to be entered when prompted, USER is the username, SERVER is the server IP Address (eg. 10.8.100.100) and SSH Command is the command you want to execute.
For your example:
sshpass -p <PASSWORD> ssh qbadmin@10.3.2.0 mkdir <full path for new dir>
To install SSHPASS:
sudo apt-get install sshpass
If you are wanting the SSH session to remain afterwards than you can use expect with the following,
#!/bin/bash
##Enter Username and Password Details:
userName=<UserName>
password=<Password>
expect -c "
spawn ssh ${userName}@10.8.100.100 ##put your own IP here
expect "password: " ##or whatever password prompt you get
send "$passwordr"
expect -re "Last Login: " ##or whatever the end of your welcome message is
send "su -i"
expect "password for <UserName>"
send "$passwordr"
"
This will leave the root access logged in, however until the script sends a return statement it will hold control until you hit Ctrl+c. But this should answer your question.
Resurrect: Are your double quotes inside supposed to be escaped?
– Relic
Nov 15 '17 at 0:40
add a comment |
A better/easier way than using expect is SSHPASS, which sends a password through with an SSH request:
sshpass -p <PASSWORD> ssh <USER>@<SERVER> <SSH Command>
PASSWORD is the password to be entered when prompted, USER is the username, SERVER is the server IP Address (eg. 10.8.100.100) and SSH Command is the command you want to execute.
For your example:
sshpass -p <PASSWORD> ssh qbadmin@10.3.2.0 mkdir <full path for new dir>
To install SSHPASS:
sudo apt-get install sshpass
If you are wanting the SSH session to remain afterwards than you can use expect with the following,
#!/bin/bash
##Enter Username and Password Details:
userName=<UserName>
password=<Password>
expect -c "
spawn ssh ${userName}@10.8.100.100 ##put your own IP here
expect "password: " ##or whatever password prompt you get
send "$passwordr"
expect -re "Last Login: " ##or whatever the end of your welcome message is
send "su -i"
expect "password for <UserName>"
send "$passwordr"
"
This will leave the root access logged in, however until the script sends a return statement it will hold control until you hit Ctrl+c. But this should answer your question.
Resurrect: Are your double quotes inside supposed to be escaped?
– Relic
Nov 15 '17 at 0:40
add a comment |
A better/easier way than using expect is SSHPASS, which sends a password through with an SSH request:
sshpass -p <PASSWORD> ssh <USER>@<SERVER> <SSH Command>
PASSWORD is the password to be entered when prompted, USER is the username, SERVER is the server IP Address (eg. 10.8.100.100) and SSH Command is the command you want to execute.
For your example:
sshpass -p <PASSWORD> ssh qbadmin@10.3.2.0 mkdir <full path for new dir>
To install SSHPASS:
sudo apt-get install sshpass
If you are wanting the SSH session to remain afterwards than you can use expect with the following,
#!/bin/bash
##Enter Username and Password Details:
userName=<UserName>
password=<Password>
expect -c "
spawn ssh ${userName}@10.8.100.100 ##put your own IP here
expect "password: " ##or whatever password prompt you get
send "$passwordr"
expect -re "Last Login: " ##or whatever the end of your welcome message is
send "su -i"
expect "password for <UserName>"
send "$passwordr"
"
This will leave the root access logged in, however until the script sends a return statement it will hold control until you hit Ctrl+c. But this should answer your question.
A better/easier way than using expect is SSHPASS, which sends a password through with an SSH request:
sshpass -p <PASSWORD> ssh <USER>@<SERVER> <SSH Command>
PASSWORD is the password to be entered when prompted, USER is the username, SERVER is the server IP Address (eg. 10.8.100.100) and SSH Command is the command you want to execute.
For your example:
sshpass -p <PASSWORD> ssh qbadmin@10.3.2.0 mkdir <full path for new dir>
To install SSHPASS:
sudo apt-get install sshpass
If you are wanting the SSH session to remain afterwards than you can use expect with the following,
#!/bin/bash
##Enter Username and Password Details:
userName=<UserName>
password=<Password>
expect -c "
spawn ssh ${userName}@10.8.100.100 ##put your own IP here
expect "password: " ##or whatever password prompt you get
send "$passwordr"
expect -re "Last Login: " ##or whatever the end of your welcome message is
send "su -i"
expect "password for <UserName>"
send "$passwordr"
"
This will leave the root access logged in, however until the script sends a return statement it will hold control until you hit Ctrl+c. But this should answer your question.
edited Feb 15 at 18:43
Kevin Bowen
14.7k155970
14.7k155970
answered Dec 21 '15 at 14:12
timtim
2616
2616
Resurrect: Are your double quotes inside supposed to be escaped?
– Relic
Nov 15 '17 at 0:40
add a comment |
Resurrect: Are your double quotes inside supposed to be escaped?
– Relic
Nov 15 '17 at 0:40
Resurrect: Are your double quotes inside supposed to be escaped?
– Relic
Nov 15 '17 at 0:40
Resurrect: Are your double quotes inside supposed to be escaped?
– Relic
Nov 15 '17 at 0:40
add a comment |
If you want to pass only the root password, you have to
- either configure a password-less login into the user account, e.g. using public key authentication and an ssh agent on the client side,
- or configure the server (i.e. to allow login as root, and then do
ssh -t root@10.3.2.0 …
add a comment |
If you want to pass only the root password, you have to
- either configure a password-less login into the user account, e.g. using public key authentication and an ssh agent on the client side,
- or configure the server (i.e. to allow login as root, and then do
ssh -t root@10.3.2.0 …
add a comment |
If you want to pass only the root password, you have to
- either configure a password-less login into the user account, e.g. using public key authentication and an ssh agent on the client side,
- or configure the server (i.e. to allow login as root, and then do
ssh -t root@10.3.2.0 …
If you want to pass only the root password, you have to
- either configure a password-less login into the user account, e.g. using public key authentication and an ssh agent on the client side,
- or configure the server (i.e. to allow login as root, and then do
ssh -t root@10.3.2.0 …
answered Mar 19 '13 at 11:37
MvGMvG
1,1491922
1,1491922
add a comment |
add a comment |
I don't see an accepted answer years later so I will also add something that may be helpful to others. My situation had nothing to do with SSH, but using spawn expect in the following way I was able to get it working.
expect -c "
spawn myProc
expect "Do you wish to continue with XYZ? (y/N)" {
send "nr"
exp_continue
}
"
Hope this helps.
add a comment |
I don't see an accepted answer years later so I will also add something that may be helpful to others. My situation had nothing to do with SSH, but using spawn expect in the following way I was able to get it working.
expect -c "
spawn myProc
expect "Do you wish to continue with XYZ? (y/N)" {
send "nr"
exp_continue
}
"
Hope this helps.
add a comment |
I don't see an accepted answer years later so I will also add something that may be helpful to others. My situation had nothing to do with SSH, but using spawn expect in the following way I was able to get it working.
expect -c "
spawn myProc
expect "Do you wish to continue with XYZ? (y/N)" {
send "nr"
exp_continue
}
"
Hope this helps.
I don't see an accepted answer years later so I will also add something that may be helpful to others. My situation had nothing to do with SSH, but using spawn expect in the following way I was able to get it working.
expect -c "
spawn myProc
expect "Do you wish to continue with XYZ? (y/N)" {
send "nr"
exp_continue
}
"
Hope this helps.
answered Nov 15 '17 at 0:57
RelicRelic
101
101
add a comment |
add a comment |
Thanks for contributing an answer to Ask Ubuntu!
- 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%2faskubuntu.com%2fquestions%2f269878%2fhow-to-use-expect-with-bash%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
What about authorized keys? help.ubuntu.com/community/SSH/OpenSSH/Keys More security!
– prophecy201
Mar 19 '13 at 10:52