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;









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?










share|improve this question





















  • 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











  • 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


















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?










share|improve this question





















  • 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











  • 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














0












0








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?










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 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













  • 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











  • 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











1 Answer
1






active

oldest

votes


















1
















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 :



  1. run sudo command and monitor output on terminal

  2. when you see this string, send this answer

  3. when you see this other string, send this other answer

  4. 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.






share|improve this answer


























    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
    );



    );














    draft saved

    draft discarded
















    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









    1
















    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 :



    1. run sudo command and monitor output on terminal

    2. when you see this string, send this answer

    3. when you see this other string, send this other answer

    4. 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.






    share|improve this answer





























      1
















      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 :



      1. run sudo command and monitor output on terminal

      2. when you see this string, send this answer

      3. when you see this other string, send this other answer

      4. 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.






      share|improve this answer



























        1














        1










        1









        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 :



        1. run sudo command and monitor output on terminal

        2. when you see this string, send this answer

        3. when you see this other string, send this other answer

        4. 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.






        share|improve this answer













        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 :



        1. run sudo command and monitor output on terminal

        2. when you see this string, send this answer

        3. when you see this other string, send this other answer

        4. 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.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Apr 17 at 18:06









        exoreexore

        7165 silver badges9 bronze badges




        7165 silver badges9 bronze badges































            draft saved

            draft discarded















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            Tamil (spriik) Luke uk diar | Nawigatjuun

            Align equal signs while including text over equalitiesAMS align: left aligned text/math plus multicolumn alignmentMultiple alignmentsAligning equations in multiple placesNumbering and aligning an equation with multiple columnsHow to align one equation with another multline equationUsing \ in environments inside the begintabularxNumber equations and preserving alignment of equal signsHow can I align equations to the left and to the right?Double equation alignment problem within align enviromentAligned within align: Why are they right-aligned?

            Where does the image of a data connector as a sharp metal spike originate from?Where does the concept of infected people turning into zombies only after death originate from?Where does the motif of a reanimated human head originate?Where did the notion that Dragons could speak originate?Where does the archetypal image of the 'Grey' alien come from?Where did the suffix '-Man' originate?Where does the notion of being injured or killed by an illusion originate?Where did the term “sophont” originate?Where does the trope of magic spells being driven by advanced technology originate from?Where did the term “the living impaired” originate?