How to auto answer to a multi-stage command?How to execute sudo commands with Expect & send commands in bash script?Make a Launcher for a Privileged Terminal CommandRun command on another(new) terminal windowProvide password to `nmcli con up` in terminal 14.04Automate keystroke enter in curses with command from bashautomatically answer input in command lineUbuntu 16.04.5 LTS (GUI stop but no command terminal)
How do lasers measure short distances (<1cm) when electronics are too slow for time-of-flight to work?
Are there any privately owned large commercial airports?
Why didn't Snape ask Dumbledore why he let "Moody" search his office?
Does UPDATE without WHERE clause lock a table in PostgreSQL?
How much money should I save in order to generate $1000/month for the rest of my life?
Why would they pick a gamma distribution here?
What is the fastest way to move in Borderlands 3?
What is the next number in the series: 21, 21, 23, 20, 5, 25, 31, 24,?
"Es gefällt ihm." How to identify similar exceptions?
What if a quote contains an error
Does "Op. cit." stand for "opus citatum" or "opere citato"?
Meaning/translation of title "The Light Fantastic" By Terry Pratchett
How to find an internship in OR/Optimization?
Can a Creature at 0 HP Take Damage?
Why has Donald Trump's popularity remained so stable over a rather long period of time?
how do you value what your leisure time is worth?
Low-magic medieval fantasy clothes that allow the wearer to grow?
Why are engines with carburetors hard to start in cold weather?
Is there a magnetic attraction between two parallel electron beams?
This fell out of my toilet when I unscrewed the supply line. What is it?
Can I perform Umrah while on a Saudi Arabian visit e-visa
Would we have more than 8 minutes of light, if the sun "went out"?
How can I remove rest of file from string for all files?
How are steel imports supposed to threaten US national security?
How to auto answer to a multi-stage command?
How to execute sudo commands with Expect & send commands in bash script?Make a Launcher for a Privileged Terminal CommandRun command on another(new) terminal windowProvide password to `nmcli con up` in terminal 14.04Automate keystroke enter in curses with command from bashautomatically answer input in command lineUbuntu 16.04.5 LTS (GUI stop but no command terminal)
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty
margin-bottom:0;
I'm new in Ubuntu terminal. I have a command to connect to a VPN server. in first step I must type sudo vpn_command and so I must type root password, then in next step I must Enter "yes" and vpn username and vpn password respectively.
I want to do all these actions in one command. can you help me?
command-line 18.04 automation execute-command
add a comment
|
I'm new in Ubuntu terminal. I have a command to connect to a VPN server. in first step I must type sudo vpn_command and so I must type root password, then in next step I must Enter "yes" and vpn username and vpn password respectively.
I want to do all these actions in one command. can you help me?
command-line 18.04 automation execute-command
3
If this involves storing your password in plaintext to feed it to commands, you probably shouldn't. It would be helpful if you told us whichvpn_command
you are talking about. Running commands as root without password prompt is possible. Maybe yourvpn_command
supports parameters like-y
to assume yes to all answers, and maybe it supports specifiying the username on the command line, so you don't have to provide it interactively. And finally, if your vpn provider supports key-based authentication, you wouldn't have to enter the password.
– danzel
Apr 17 at 15:41
Please edit your question and provide more detailed information so we can get rid of the ifs and maybes.
– danzel
Apr 17 at 15:42
add a comment
|
I'm new in Ubuntu terminal. I have a command to connect to a VPN server. in first step I must type sudo vpn_command and so I must type root password, then in next step I must Enter "yes" and vpn username and vpn password respectively.
I want to do all these actions in one command. can you help me?
command-line 18.04 automation execute-command
I'm new in Ubuntu terminal. I have a command to connect to a VPN server. in first step I must type sudo vpn_command and so I must type root password, then in next step I must Enter "yes" and vpn username and vpn password respectively.
I want to do all these actions in one command. can you help me?
command-line 18.04 automation execute-command
command-line 18.04 automation execute-command
edited Apr 17 at 14:32
Ehsan
asked Apr 17 at 14:23
EhsanEhsan
751 silver badge8 bronze badges
751 silver badge8 bronze badges
3
If this involves storing your password in plaintext to feed it to commands, you probably shouldn't. It would be helpful if you told us whichvpn_command
you are talking about. Running commands as root without password prompt is possible. Maybe yourvpn_command
supports parameters like-y
to assume yes to all answers, and maybe it supports specifiying the username on the command line, so you don't have to provide it interactively. And finally, if your vpn provider supports key-based authentication, you wouldn't have to enter the password.
– danzel
Apr 17 at 15:41
Please edit your question and provide more detailed information so we can get rid of the ifs and maybes.
– danzel
Apr 17 at 15:42
add a comment
|
3
If this involves storing your password in plaintext to feed it to commands, you probably shouldn't. It would be helpful if you told us whichvpn_command
you are talking about. Running commands as root without password prompt is possible. Maybe yourvpn_command
supports parameters like-y
to assume yes to all answers, and maybe it supports specifiying the username on the command line, so you don't have to provide it interactively. And finally, if your vpn provider supports key-based authentication, you wouldn't have to enter the password.
– danzel
Apr 17 at 15:41
Please edit your question and provide more detailed information so we can get rid of the ifs and maybes.
– danzel
Apr 17 at 15:42
3
3
If this involves storing your password in plaintext to feed it to commands, you probably shouldn't. It would be helpful if you told us which
vpn_command
you are talking about. Running commands as root without password prompt is possible. Maybe your vpn_command
supports parameters like -y
to assume yes to all answers, and maybe it supports specifiying the username on the command line, so you don't have to provide it interactively. And finally, if your vpn provider supports key-based authentication, you wouldn't have to enter the password.– danzel
Apr 17 at 15:41
If this involves storing your password in plaintext to feed it to commands, you probably shouldn't. It would be helpful if you told us which
vpn_command
you are talking about. Running commands as root without password prompt is possible. Maybe your vpn_command
supports parameters like -y
to assume yes to all answers, and maybe it supports specifiying the username on the command line, so you don't have to provide it interactively. And finally, if your vpn provider supports key-based authentication, you wouldn't have to enter the password.– danzel
Apr 17 at 15:41
Please edit your question and provide more detailed information so we can get rid of the ifs and maybes.
– danzel
Apr 17 at 15:42
Please edit your question and provide more detailed information so we can get rid of the ifs and maybes.
– danzel
Apr 17 at 15:42
add a comment
|
1 Answer
1
active
oldest
votes
This is possible. Although as mentionned in comments probably not the best idea. Please note that you have to use your own password for sudo, not root's password (that's precisely the point of running sudo). It's probably best to configure sudo to not ask a password. Also your vpn probably has an option that takes a credential filename.
The program you need is called expect. So first sudo apt-get install expect
. Expect, as its name suggests, expects strings on terminal and sends back other strings. It is the perfect tool when a program prompts for answer. It's a quite complex program but here, it can be used in a very basic way :
- run sudo command and monitor output on terminal
- when you see this string, send this answer
- when you see this other string, send this other answer
- etc...
see askubuntu question 307067 or stackoverflow question 38458053 for example. Your search engine will find many examples once you know you have to ask for "expect password examples".
sudo will not always ask for your password. if you rerun a sudo command in the same terminal within 5 minutes or so, it won't ask for a password the second time. You can use expect timeout in such situations.
You could write a script called myexpectscript with such content :
#!/usr/bin/expect -d
set timeout 5
set pw YOUR ubuntu GNU/linux P4ßw0rd
set vpnuser yourvpnusername
set vpnpass vpn-PAs$w0rd
spawn sudo -p sudoisaskingforyourpassword vpncommand -foo -b ar etc...
expect "sudoisaskingforyourpassword" send "$pwn"
expect Do you really want to do that send "Yesn"
expect vpn user prompt send "$vpnusern"
expect vpn password prompt send "$vpnpassn"
interact
make it executable with chmod 700 myexpectscript
, then run ./myexpectscript
.
Change -d
to -f
to get rid of the debugging outpout. You have to adjust the timeout, the command and the various prompts and passwords.
All this is, of course, completely untested.
Last but not least : expect uses the TCL language. TCL is a very elegant and friendly language, it's just a bit picky about who its friends are. If you get errors, be prepared to read docs for a long time.
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/4.0/"u003ecc by-sa 4.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%2f1134672%2fhow-to-auto-answer-to-a-multi-stage-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
This is possible. Although as mentionned in comments probably not the best idea. Please note that you have to use your own password for sudo, not root's password (that's precisely the point of running sudo). It's probably best to configure sudo to not ask a password. Also your vpn probably has an option that takes a credential filename.
The program you need is called expect. So first sudo apt-get install expect
. Expect, as its name suggests, expects strings on terminal and sends back other strings. It is the perfect tool when a program prompts for answer. It's a quite complex program but here, it can be used in a very basic way :
- run sudo command and monitor output on terminal
- when you see this string, send this answer
- when you see this other string, send this other answer
- etc...
see askubuntu question 307067 or stackoverflow question 38458053 for example. Your search engine will find many examples once you know you have to ask for "expect password examples".
sudo will not always ask for your password. if you rerun a sudo command in the same terminal within 5 minutes or so, it won't ask for a password the second time. You can use expect timeout in such situations.
You could write a script called myexpectscript with such content :
#!/usr/bin/expect -d
set timeout 5
set pw YOUR ubuntu GNU/linux P4ßw0rd
set vpnuser yourvpnusername
set vpnpass vpn-PAs$w0rd
spawn sudo -p sudoisaskingforyourpassword vpncommand -foo -b ar etc...
expect "sudoisaskingforyourpassword" send "$pwn"
expect Do you really want to do that send "Yesn"
expect vpn user prompt send "$vpnusern"
expect vpn password prompt send "$vpnpassn"
interact
make it executable with chmod 700 myexpectscript
, then run ./myexpectscript
.
Change -d
to -f
to get rid of the debugging outpout. You have to adjust the timeout, the command and the various prompts and passwords.
All this is, of course, completely untested.
Last but not least : expect uses the TCL language. TCL is a very elegant and friendly language, it's just a bit picky about who its friends are. If you get errors, be prepared to read docs for a long time.
add a comment
|
This is possible. Although as mentionned in comments probably not the best idea. Please note that you have to use your own password for sudo, not root's password (that's precisely the point of running sudo). It's probably best to configure sudo to not ask a password. Also your vpn probably has an option that takes a credential filename.
The program you need is called expect. So first sudo apt-get install expect
. Expect, as its name suggests, expects strings on terminal and sends back other strings. It is the perfect tool when a program prompts for answer. It's a quite complex program but here, it can be used in a very basic way :
- run sudo command and monitor output on terminal
- when you see this string, send this answer
- when you see this other string, send this other answer
- etc...
see askubuntu question 307067 or stackoverflow question 38458053 for example. Your search engine will find many examples once you know you have to ask for "expect password examples".
sudo will not always ask for your password. if you rerun a sudo command in the same terminal within 5 minutes or so, it won't ask for a password the second time. You can use expect timeout in such situations.
You could write a script called myexpectscript with such content :
#!/usr/bin/expect -d
set timeout 5
set pw YOUR ubuntu GNU/linux P4ßw0rd
set vpnuser yourvpnusername
set vpnpass vpn-PAs$w0rd
spawn sudo -p sudoisaskingforyourpassword vpncommand -foo -b ar etc...
expect "sudoisaskingforyourpassword" send "$pwn"
expect Do you really want to do that send "Yesn"
expect vpn user prompt send "$vpnusern"
expect vpn password prompt send "$vpnpassn"
interact
make it executable with chmod 700 myexpectscript
, then run ./myexpectscript
.
Change -d
to -f
to get rid of the debugging outpout. You have to adjust the timeout, the command and the various prompts and passwords.
All this is, of course, completely untested.
Last but not least : expect uses the TCL language. TCL is a very elegant and friendly language, it's just a bit picky about who its friends are. If you get errors, be prepared to read docs for a long time.
add a comment
|
This is possible. Although as mentionned in comments probably not the best idea. Please note that you have to use your own password for sudo, not root's password (that's precisely the point of running sudo). It's probably best to configure sudo to not ask a password. Also your vpn probably has an option that takes a credential filename.
The program you need is called expect. So first sudo apt-get install expect
. Expect, as its name suggests, expects strings on terminal and sends back other strings. It is the perfect tool when a program prompts for answer. It's a quite complex program but here, it can be used in a very basic way :
- run sudo command and monitor output on terminal
- when you see this string, send this answer
- when you see this other string, send this other answer
- etc...
see askubuntu question 307067 or stackoverflow question 38458053 for example. Your search engine will find many examples once you know you have to ask for "expect password examples".
sudo will not always ask for your password. if you rerun a sudo command in the same terminal within 5 minutes or so, it won't ask for a password the second time. You can use expect timeout in such situations.
You could write a script called myexpectscript with such content :
#!/usr/bin/expect -d
set timeout 5
set pw YOUR ubuntu GNU/linux P4ßw0rd
set vpnuser yourvpnusername
set vpnpass vpn-PAs$w0rd
spawn sudo -p sudoisaskingforyourpassword vpncommand -foo -b ar etc...
expect "sudoisaskingforyourpassword" send "$pwn"
expect Do you really want to do that send "Yesn"
expect vpn user prompt send "$vpnusern"
expect vpn password prompt send "$vpnpassn"
interact
make it executable with chmod 700 myexpectscript
, then run ./myexpectscript
.
Change -d
to -f
to get rid of the debugging outpout. You have to adjust the timeout, the command and the various prompts and passwords.
All this is, of course, completely untested.
Last but not least : expect uses the TCL language. TCL is a very elegant and friendly language, it's just a bit picky about who its friends are. If you get errors, be prepared to read docs for a long time.
This is possible. Although as mentionned in comments probably not the best idea. Please note that you have to use your own password for sudo, not root's password (that's precisely the point of running sudo). It's probably best to configure sudo to not ask a password. Also your vpn probably has an option that takes a credential filename.
The program you need is called expect. So first sudo apt-get install expect
. Expect, as its name suggests, expects strings on terminal and sends back other strings. It is the perfect tool when a program prompts for answer. It's a quite complex program but here, it can be used in a very basic way :
- run sudo command and monitor output on terminal
- when you see this string, send this answer
- when you see this other string, send this other answer
- etc...
see askubuntu question 307067 or stackoverflow question 38458053 for example. Your search engine will find many examples once you know you have to ask for "expect password examples".
sudo will not always ask for your password. if you rerun a sudo command in the same terminal within 5 minutes or so, it won't ask for a password the second time. You can use expect timeout in such situations.
You could write a script called myexpectscript with such content :
#!/usr/bin/expect -d
set timeout 5
set pw YOUR ubuntu GNU/linux P4ßw0rd
set vpnuser yourvpnusername
set vpnpass vpn-PAs$w0rd
spawn sudo -p sudoisaskingforyourpassword vpncommand -foo -b ar etc...
expect "sudoisaskingforyourpassword" send "$pwn"
expect Do you really want to do that send "Yesn"
expect vpn user prompt send "$vpnusern"
expect vpn password prompt send "$vpnpassn"
interact
make it executable with chmod 700 myexpectscript
, then run ./myexpectscript
.
Change -d
to -f
to get rid of the debugging outpout. You have to adjust the timeout, the command and the various prompts and passwords.
All this is, of course, completely untested.
Last but not least : expect uses the TCL language. TCL is a very elegant and friendly language, it's just a bit picky about who its friends are. If you get errors, be prepared to read docs for a long time.
answered Apr 17 at 18:06
exoreexore
7165 silver badges9 bronze badges
7165 silver badges9 bronze badges
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%2f1134672%2fhow-to-auto-answer-to-a-multi-stage-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
3
If this involves storing your password in plaintext to feed it to commands, you probably shouldn't. It would be helpful if you told us which
vpn_command
you are talking about. Running commands as root without password prompt is possible. Maybe yourvpn_command
supports parameters like-y
to assume yes to all answers, and maybe it supports specifiying the username on the command line, so you don't have to provide it interactively. And finally, if your vpn provider supports key-based authentication, you wouldn't have to enter the password.– danzel
Apr 17 at 15:41
Please edit your question and provide more detailed information so we can get rid of the ifs and maybes.
– danzel
Apr 17 at 15:42