expect not taking ssh arguments in while/for loop

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
1
down vote

favorite












I'm trying to fetch remote machine details (dmidecode) for many hosts using some expect command.



Below is the expect script which I'm using for this purpose.



while read i; do

/usr/bin/expect<<EOF
spawn ssh "root@$i" dmidecode
expect "Password:"
send "xxxxr";
interact
EOF

done<iplist


But here command which is not excecuting on the remote machine. I tried with single, double quotes and caret symbol still no luck.



Here I want to execute remote command as argument like : ssh <ip> <remote command>



Please shed me some views, I might be missed some params here, please help me







share|improve this question

















  • 2




    The final EOF may not be preceded by a space character. I'm not 100% certain that's the only error since I'm not used to expect. The ... may be removed in any case.
    – Kusalananda
    Jun 22 at 15:18











  • take a look at sexpect with which you can write "Expect" scripts with shell code only and you don't have to learn the Tcl/Expect language.
    – pynexj
    Jun 22 at 15:43










  • Try adding a -n option to the ssh command.
    – Mark Plotnick
    Jul 8 at 15:18














up vote
1
down vote

favorite












I'm trying to fetch remote machine details (dmidecode) for many hosts using some expect command.



Below is the expect script which I'm using for this purpose.



while read i; do

/usr/bin/expect<<EOF
spawn ssh "root@$i" dmidecode
expect "Password:"
send "xxxxr";
interact
EOF

done<iplist


But here command which is not excecuting on the remote machine. I tried with single, double quotes and caret symbol still no luck.



Here I want to execute remote command as argument like : ssh <ip> <remote command>



Please shed me some views, I might be missed some params here, please help me







share|improve this question

















  • 2




    The final EOF may not be preceded by a space character. I'm not 100% certain that's the only error since I'm not used to expect. The ... may be removed in any case.
    – Kusalananda
    Jun 22 at 15:18











  • take a look at sexpect with which you can write "Expect" scripts with shell code only and you don't have to learn the Tcl/Expect language.
    – pynexj
    Jun 22 at 15:43










  • Try adding a -n option to the ssh command.
    – Mark Plotnick
    Jul 8 at 15:18












up vote
1
down vote

favorite









up vote
1
down vote

favorite











I'm trying to fetch remote machine details (dmidecode) for many hosts using some expect command.



Below is the expect script which I'm using for this purpose.



while read i; do

/usr/bin/expect<<EOF
spawn ssh "root@$i" dmidecode
expect "Password:"
send "xxxxr";
interact
EOF

done<iplist


But here command which is not excecuting on the remote machine. I tried with single, double quotes and caret symbol still no luck.



Here I want to execute remote command as argument like : ssh <ip> <remote command>



Please shed me some views, I might be missed some params here, please help me







share|improve this question













I'm trying to fetch remote machine details (dmidecode) for many hosts using some expect command.



Below is the expect script which I'm using for this purpose.



while read i; do

/usr/bin/expect<<EOF
spawn ssh "root@$i" dmidecode
expect "Password:"
send "xxxxr";
interact
EOF

done<iplist


But here command which is not excecuting on the remote machine. I tried with single, double quotes and caret symbol still no luck.



Here I want to execute remote command as argument like : ssh <ip> <remote command>



Please shed me some views, I might be missed some params here, please help me









share|improve this question












share|improve this question




share|improve this question








edited Jun 22 at 17:36









Ouki

3,58021324




3,58021324









asked Jun 22 at 15:00









jay

61




61







  • 2




    The final EOF may not be preceded by a space character. I'm not 100% certain that's the only error since I'm not used to expect. The ... may be removed in any case.
    – Kusalananda
    Jun 22 at 15:18











  • take a look at sexpect with which you can write "Expect" scripts with shell code only and you don't have to learn the Tcl/Expect language.
    – pynexj
    Jun 22 at 15:43










  • Try adding a -n option to the ssh command.
    – Mark Plotnick
    Jul 8 at 15:18












  • 2




    The final EOF may not be preceded by a space character. I'm not 100% certain that's the only error since I'm not used to expect. The ... may be removed in any case.
    – Kusalananda
    Jun 22 at 15:18











  • take a look at sexpect with which you can write "Expect" scripts with shell code only and you don't have to learn the Tcl/Expect language.
    – pynexj
    Jun 22 at 15:43










  • Try adding a -n option to the ssh command.
    – Mark Plotnick
    Jul 8 at 15:18







2




2




The final EOF may not be preceded by a space character. I'm not 100% certain that's the only error since I'm not used to expect. The ... may be removed in any case.
– Kusalananda
Jun 22 at 15:18





The final EOF may not be preceded by a space character. I'm not 100% certain that's the only error since I'm not used to expect. The ... may be removed in any case.
– Kusalananda
Jun 22 at 15:18













take a look at sexpect with which you can write "Expect" scripts with shell code only and you don't have to learn the Tcl/Expect language.
– pynexj
Jun 22 at 15:43




take a look at sexpect with which you can write "Expect" scripts with shell code only and you don't have to learn the Tcl/Expect language.
– pynexj
Jun 22 at 15:43












Try adding a -n option to the ssh command.
– Mark Plotnick
Jul 8 at 15:18




Try adding a -n option to the ssh command.
– Mark Plotnick
Jul 8 at 15:18










2 Answers
2






active

oldest

votes

















up vote
2
down vote













One simplification would be to eliminate the buggy and slow shell while loop which would also free up stdin to be used for interact. That is, the input file would instead be passed to and read by TCL code in the expect script.



#!/usr/bin/expect

if [llength $argv] != 1
puts stderr "Usage: $0 iplist-file"
exit 1


set ipfh [open [lindex $argv 0]]

while [gets $ipfh ip] >= 0
spawn ssh root@$ip dmidecode
expect "Password:"
send "Hunter2r";
interact






share|improve this answer




























    up vote
    1
    down vote













    thrig has a great answer. If you want to stick with bash (even though the while-read loop is extremely slow, and tedious to get right syntax-wise), use a different file descriptor to read from the file and allow expect to hold onto stdin:



    while IFS= read -r -u3 ip; do
    # .................^^^
    /usr/bin/expect << EOF
    spawn ssh "root@$ip" dmidecode
    expect "Password:"
    send "xxxxr";
    interact
    EOF
    done 3< iplist
    # ...^^


    If the dmidecode command does not require human interaction, change interact to expect eof






    share|improve this answer























      Your Answer







      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "106"
      ;
      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',
      convertImagesToLinks: false,
      noModals: false,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      bindNavPrevention: true,
      postfix: "",
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );








       

      draft saved


      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f451326%2fexpect-not-taking-ssh-arguments-in-while-for-loop%23new-answer', 'question_page');

      );

      Post as a guest






























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      2
      down vote













      One simplification would be to eliminate the buggy and slow shell while loop which would also free up stdin to be used for interact. That is, the input file would instead be passed to and read by TCL code in the expect script.



      #!/usr/bin/expect

      if [llength $argv] != 1
      puts stderr "Usage: $0 iplist-file"
      exit 1


      set ipfh [open [lindex $argv 0]]

      while [gets $ipfh ip] >= 0
      spawn ssh root@$ip dmidecode
      expect "Password:"
      send "Hunter2r";
      interact






      share|improve this answer

























        up vote
        2
        down vote













        One simplification would be to eliminate the buggy and slow shell while loop which would also free up stdin to be used for interact. That is, the input file would instead be passed to and read by TCL code in the expect script.



        #!/usr/bin/expect

        if [llength $argv] != 1
        puts stderr "Usage: $0 iplist-file"
        exit 1


        set ipfh [open [lindex $argv 0]]

        while [gets $ipfh ip] >= 0
        spawn ssh root@$ip dmidecode
        expect "Password:"
        send "Hunter2r";
        interact






        share|improve this answer























          up vote
          2
          down vote










          up vote
          2
          down vote









          One simplification would be to eliminate the buggy and slow shell while loop which would also free up stdin to be used for interact. That is, the input file would instead be passed to and read by TCL code in the expect script.



          #!/usr/bin/expect

          if [llength $argv] != 1
          puts stderr "Usage: $0 iplist-file"
          exit 1


          set ipfh [open [lindex $argv 0]]

          while [gets $ipfh ip] >= 0
          spawn ssh root@$ip dmidecode
          expect "Password:"
          send "Hunter2r";
          interact






          share|improve this answer













          One simplification would be to eliminate the buggy and slow shell while loop which would also free up stdin to be used for interact. That is, the input file would instead be passed to and read by TCL code in the expect script.



          #!/usr/bin/expect

          if [llength $argv] != 1
          puts stderr "Usage: $0 iplist-file"
          exit 1


          set ipfh [open [lindex $argv 0]]

          while [gets $ipfh ip] >= 0
          spawn ssh root@$ip dmidecode
          expect "Password:"
          send "Hunter2r";
          interact







          share|improve this answer













          share|improve this answer



          share|improve this answer











          answered Jun 22 at 15:17









          thrig

          21.8k12751




          21.8k12751






















              up vote
              1
              down vote













              thrig has a great answer. If you want to stick with bash (even though the while-read loop is extremely slow, and tedious to get right syntax-wise), use a different file descriptor to read from the file and allow expect to hold onto stdin:



              while IFS= read -r -u3 ip; do
              # .................^^^
              /usr/bin/expect << EOF
              spawn ssh "root@$ip" dmidecode
              expect "Password:"
              send "xxxxr";
              interact
              EOF
              done 3< iplist
              # ...^^


              If the dmidecode command does not require human interaction, change interact to expect eof






              share|improve this answer



























                up vote
                1
                down vote













                thrig has a great answer. If you want to stick with bash (even though the while-read loop is extremely slow, and tedious to get right syntax-wise), use a different file descriptor to read from the file and allow expect to hold onto stdin:



                while IFS= read -r -u3 ip; do
                # .................^^^
                /usr/bin/expect << EOF
                spawn ssh "root@$ip" dmidecode
                expect "Password:"
                send "xxxxr";
                interact
                EOF
                done 3< iplist
                # ...^^


                If the dmidecode command does not require human interaction, change interact to expect eof






                share|improve this answer

























                  up vote
                  1
                  down vote










                  up vote
                  1
                  down vote









                  thrig has a great answer. If you want to stick with bash (even though the while-read loop is extremely slow, and tedious to get right syntax-wise), use a different file descriptor to read from the file and allow expect to hold onto stdin:



                  while IFS= read -r -u3 ip; do
                  # .................^^^
                  /usr/bin/expect << EOF
                  spawn ssh "root@$ip" dmidecode
                  expect "Password:"
                  send "xxxxr";
                  interact
                  EOF
                  done 3< iplist
                  # ...^^


                  If the dmidecode command does not require human interaction, change interact to expect eof






                  share|improve this answer















                  thrig has a great answer. If you want to stick with bash (even though the while-read loop is extremely slow, and tedious to get right syntax-wise), use a different file descriptor to read from the file and allow expect to hold onto stdin:



                  while IFS= read -r -u3 ip; do
                  # .................^^^
                  /usr/bin/expect << EOF
                  spawn ssh "root@$ip" dmidecode
                  expect "Password:"
                  send "xxxxr";
                  interact
                  EOF
                  done 3< iplist
                  # ...^^


                  If the dmidecode command does not require human interaction, change interact to expect eof







                  share|improve this answer















                  share|improve this answer



                  share|improve this answer








                  answered Jun 22 at 15:28



























                  community wiki





                  glenn jackman























                       

                      draft saved


                      draft discarded


























                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f451326%2fexpect-not-taking-ssh-arguments-in-while-for-loop%23new-answer', 'question_page');

                      );

                      Post as a guest













































































                      Popular posts from this blog

                      Peggy Mitchell

                      Palaiologos

                      The Forum (Inglewood, California)