how to use expect with bash












2















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.










share|improve this question























  • What about authorized keys? help.ubuntu.com/community/SSH/OpenSSH/Keys More security!

    – prophecy201
    Mar 19 '13 at 10:52
















2















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.










share|improve this question























  • What about authorized keys? help.ubuntu.com/community/SSH/OpenSSH/Keys More security!

    – prophecy201
    Mar 19 '13 at 10:52














2












2








2








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.










share|improve this question














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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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



















  • 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










3 Answers
3






active

oldest

votes


















2














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.






share|improve this answer


























  • Resurrect: Are your double quotes inside supposed to be escaped?

    – Relic
    Nov 15 '17 at 0:40



















1














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 …






share|improve this answer































    0














    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.






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


      }
      });














      draft saved

      draft discarded


















      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









      2














      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.






      share|improve this answer


























      • Resurrect: Are your double quotes inside supposed to be escaped?

        – Relic
        Nov 15 '17 at 0:40
















      2














      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.






      share|improve this answer


























      • Resurrect: Are your double quotes inside supposed to be escaped?

        – Relic
        Nov 15 '17 at 0:40














      2












      2








      2







      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.






      share|improve this answer















      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.







      share|improve this answer














      share|improve this answer



      share|improve this answer








      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



















      • 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













      1














      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 …






      share|improve this answer




























        1














        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 …






        share|improve this answer


























          1












          1








          1







          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 …






          share|improve this answer













          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 …







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 19 '13 at 11:37









          MvGMvG

          1,1491922




          1,1491922























              0














              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.






              share|improve this answer




























                0














                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.






                share|improve this answer


























                  0












                  0








                  0







                  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.






                  share|improve this answer













                  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.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 15 '17 at 0:57









                  RelicRelic

                  101




                  101






























                      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%2f269878%2fhow-to-use-expect-with-bash%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

                      How to make a Squid Proxy server?

                      Is this a new Fibonacci Identity?

                      19世紀