expect script for getting output for multiple servers

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












1















I am trying to get output of IQN from multiple servers. I am getting the below error.



Error output



spawn /usr/bin/ssh VM2 | /usr/bin/cat /etc/iscsi/initiatorname.iscsi
root@VM2's password:
bash: -c: line 0: syntax error near unexpected token `|'
bash: -c: line 0: `| /usr/bin/cat /etc/iscsi/initiatorname.iscsi'


script



#!/bin/bash

HOSTS="VM2 VM3 VM4 VM1"

read -p "Password: " PASSWORD

for HOST in $HOSTS
do
expect -c "
spawn cat /etc/iscsi/initiatorname.iscsi
expect
"*password:*" send $PASSWORDr;interact

exit
"
done


Am I doing something wrong?










share|improve this question
























  • where is spawn in your script? Make sure your code and your error output correspond

    – glenn jackman
    Nov 8 '16 at 19:06











  • added spawn in the script but still not getting the output.

    – user155159
    Nov 9 '16 at 2:51















1















I am trying to get output of IQN from multiple servers. I am getting the below error.



Error output



spawn /usr/bin/ssh VM2 | /usr/bin/cat /etc/iscsi/initiatorname.iscsi
root@VM2's password:
bash: -c: line 0: syntax error near unexpected token `|'
bash: -c: line 0: `| /usr/bin/cat /etc/iscsi/initiatorname.iscsi'


script



#!/bin/bash

HOSTS="VM2 VM3 VM4 VM1"

read -p "Password: " PASSWORD

for HOST in $HOSTS
do
expect -c "
spawn cat /etc/iscsi/initiatorname.iscsi
expect
"*password:*" send $PASSWORDr;interact

exit
"
done


Am I doing something wrong?










share|improve this question
























  • where is spawn in your script? Make sure your code and your error output correspond

    – glenn jackman
    Nov 8 '16 at 19:06











  • added spawn in the script but still not getting the output.

    – user155159
    Nov 9 '16 at 2:51













1












1








1


1






I am trying to get output of IQN from multiple servers. I am getting the below error.



Error output



spawn /usr/bin/ssh VM2 | /usr/bin/cat /etc/iscsi/initiatorname.iscsi
root@VM2's password:
bash: -c: line 0: syntax error near unexpected token `|'
bash: -c: line 0: `| /usr/bin/cat /etc/iscsi/initiatorname.iscsi'


script



#!/bin/bash

HOSTS="VM2 VM3 VM4 VM1"

read -p "Password: " PASSWORD

for HOST in $HOSTS
do
expect -c "
spawn cat /etc/iscsi/initiatorname.iscsi
expect
"*password:*" send $PASSWORDr;interact

exit
"
done


Am I doing something wrong?










share|improve this question
















I am trying to get output of IQN from multiple servers. I am getting the below error.



Error output



spawn /usr/bin/ssh VM2 | /usr/bin/cat /etc/iscsi/initiatorname.iscsi
root@VM2's password:
bash: -c: line 0: syntax error near unexpected token `|'
bash: -c: line 0: `| /usr/bin/cat /etc/iscsi/initiatorname.iscsi'


script



#!/bin/bash

HOSTS="VM2 VM3 VM4 VM1"

read -p "Password: " PASSWORD

for HOST in $HOSTS
do
expect -c "
spawn cat /etc/iscsi/initiatorname.iscsi
expect
"*password:*" send $PASSWORDr;interact

exit
"
done


Am I doing something wrong?







shell-script expect






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 9 '16 at 2:51

























asked Nov 8 '16 at 10:08







user155159



















  • where is spawn in your script? Make sure your code and your error output correspond

    – glenn jackman
    Nov 8 '16 at 19:06











  • added spawn in the script but still not getting the output.

    – user155159
    Nov 9 '16 at 2:51

















  • where is spawn in your script? Make sure your code and your error output correspond

    – glenn jackman
    Nov 8 '16 at 19:06











  • added spawn in the script but still not getting the output.

    – user155159
    Nov 9 '16 at 2:51
















where is spawn in your script? Make sure your code and your error output correspond

– glenn jackman
Nov 8 '16 at 19:06





where is spawn in your script? Make sure your code and your error output correspond

– glenn jackman
Nov 8 '16 at 19:06













added spawn in the script but still not getting the output.

– user155159
Nov 9 '16 at 2:51





added spawn in the script but still not getting the output.

– user155159
Nov 9 '16 at 2:51










2 Answers
2






active

oldest

votes


















0














First of all, I don't quite understand, what are you trying to achieve.



What I understood that you want to cat /etc/iscsi/initiatorname.iscsi on each server, right ?



But then why do you need to interact with each server?



I'll post solution for the first problem, where you login to each server, and cat file. I can edit solution, if you want to add interact as well.



So first of I decided to write most of the part of the script in expect itself and not pass it using bash. I think this way you'll get more errors.



So here is the bash script that is used as runner, it prompts for password, so that password is not printed or seen (it is not easy to do in expect directly)



#!/bin/bash 

expect_script="./connect_to_many_hosts_and_cat_files.exp"

[ ! -f $expect_script ] && echo expect_script does not exist && exit 1

echo "Please enter a password:"

read -s password

$expect_script $password


Now the expect script itself



#!/usr/bin/expect

if $argc != 1
puts "please run expect script using bash script"
puts "that prompts for password"
exit


set password [lindex $argv 0]

# replace hosts names with your host names !!!
set hosts "host1 host2"

foreach host [ split $hosts " " ]

set number_of_password_prompts($host) 0

puts "# "
puts "# ssh $host"
puts "# "

spawn ssh $host
expect
# here I put password check, just in case password is wrong
# look a bit ugly, but better than nothing
"Password:"
if $number_of_password_prompts($host) == 0
incr number_of_password_prompts($host)
puts "# "
puts "# Authenticating try $number_of_password_prompts($host)"
puts "# "
send "$passwordr"
exp_continue
else
puts "# "
puts "# Password is wrong"
puts "# "
exit


# in case on host /bin/sh or /bin/bash
"$ "
puts "# "
puts "# cat /etc/iscsi/initiatorname.iscsi"
puts "# "
send "cat /etc/iscsi/initiatorname.iscsi r"
expect "$ "

# in case on host /bin/csh
"% "
puts "# "
puts "# cat /etc/iscsi/initiatorname.iscsi"
puts "# "
send "cat /etc/iscsi/initiatorname.iscsi r"
expect "% "

send "exitr"
expect eof
close




Last but not the least make sure you chmod +x both files. You can also run expect other way. And also make sure you put proper location of expect. For me it is /usr/bin/expect. For you could be anything else.



Also one important thing. For expect is important what prompt is out there on your servers. If it is /bin/bash you see I've put condition with $ , if it is /bin/csh then you have to add condition with %.



It is very important for you to check how your $PS1 or other prompt will look like after ssh. Script above won't work if your prompt is



 user@host:~>


For that to work, you have to include one more condition in expect



 "> " 
puts "# "
puts "# cat /etc/iscsi/initiatorname.iscsi"
puts "# "
send "cat /etc/iscsi/initiatorname.iscsi r"
expect "> "



Please pay attention to that, I spend many days trying to figure out why things do not work.






share|improve this answer






























    -1














    Use ansible for this case.



    yum install ansible -y
    vim /etc/ansible/hosts
    [servers]
    192.168.0.2
    192.168.0.3

    ssh-keygen


    Copy public key to destination server.
    After this try to connect to server



    ansible servers -m ping


    In ansible documentation you can find information about how to rule servers.






    share|improve this answer























    • While I agree ansible is better suited for this, that is not an answer to the original question.

      – Timothy Pulliam
      Feb 4 at 18:22










    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',
    autoActivateHeartbeat: false,
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    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%2funix.stackexchange.com%2fquestions%2f321836%2fexpect-script-for-getting-output-for-multiple-servers%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown
























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    First of all, I don't quite understand, what are you trying to achieve.



    What I understood that you want to cat /etc/iscsi/initiatorname.iscsi on each server, right ?



    But then why do you need to interact with each server?



    I'll post solution for the first problem, where you login to each server, and cat file. I can edit solution, if you want to add interact as well.



    So first of I decided to write most of the part of the script in expect itself and not pass it using bash. I think this way you'll get more errors.



    So here is the bash script that is used as runner, it prompts for password, so that password is not printed or seen (it is not easy to do in expect directly)



    #!/bin/bash 

    expect_script="./connect_to_many_hosts_and_cat_files.exp"

    [ ! -f $expect_script ] && echo expect_script does not exist && exit 1

    echo "Please enter a password:"

    read -s password

    $expect_script $password


    Now the expect script itself



    #!/usr/bin/expect

    if $argc != 1
    puts "please run expect script using bash script"
    puts "that prompts for password"
    exit


    set password [lindex $argv 0]

    # replace hosts names with your host names !!!
    set hosts "host1 host2"

    foreach host [ split $hosts " " ]

    set number_of_password_prompts($host) 0

    puts "# "
    puts "# ssh $host"
    puts "# "

    spawn ssh $host
    expect
    # here I put password check, just in case password is wrong
    # look a bit ugly, but better than nothing
    "Password:"
    if $number_of_password_prompts($host) == 0
    incr number_of_password_prompts($host)
    puts "# "
    puts "# Authenticating try $number_of_password_prompts($host)"
    puts "# "
    send "$passwordr"
    exp_continue
    else
    puts "# "
    puts "# Password is wrong"
    puts "# "
    exit


    # in case on host /bin/sh or /bin/bash
    "$ "
    puts "# "
    puts "# cat /etc/iscsi/initiatorname.iscsi"
    puts "# "
    send "cat /etc/iscsi/initiatorname.iscsi r"
    expect "$ "

    # in case on host /bin/csh
    "% "
    puts "# "
    puts "# cat /etc/iscsi/initiatorname.iscsi"
    puts "# "
    send "cat /etc/iscsi/initiatorname.iscsi r"
    expect "% "

    send "exitr"
    expect eof
    close




    Last but not the least make sure you chmod +x both files. You can also run expect other way. And also make sure you put proper location of expect. For me it is /usr/bin/expect. For you could be anything else.



    Also one important thing. For expect is important what prompt is out there on your servers. If it is /bin/bash you see I've put condition with $ , if it is /bin/csh then you have to add condition with %.



    It is very important for you to check how your $PS1 or other prompt will look like after ssh. Script above won't work if your prompt is



     user@host:~>


    For that to work, you have to include one more condition in expect



     "> " 
    puts "# "
    puts "# cat /etc/iscsi/initiatorname.iscsi"
    puts "# "
    send "cat /etc/iscsi/initiatorname.iscsi r"
    expect "> "



    Please pay attention to that, I spend many days trying to figure out why things do not work.






    share|improve this answer



























      0














      First of all, I don't quite understand, what are you trying to achieve.



      What I understood that you want to cat /etc/iscsi/initiatorname.iscsi on each server, right ?



      But then why do you need to interact with each server?



      I'll post solution for the first problem, where you login to each server, and cat file. I can edit solution, if you want to add interact as well.



      So first of I decided to write most of the part of the script in expect itself and not pass it using bash. I think this way you'll get more errors.



      So here is the bash script that is used as runner, it prompts for password, so that password is not printed or seen (it is not easy to do in expect directly)



      #!/bin/bash 

      expect_script="./connect_to_many_hosts_and_cat_files.exp"

      [ ! -f $expect_script ] && echo expect_script does not exist && exit 1

      echo "Please enter a password:"

      read -s password

      $expect_script $password


      Now the expect script itself



      #!/usr/bin/expect

      if $argc != 1
      puts "please run expect script using bash script"
      puts "that prompts for password"
      exit


      set password [lindex $argv 0]

      # replace hosts names with your host names !!!
      set hosts "host1 host2"

      foreach host [ split $hosts " " ]

      set number_of_password_prompts($host) 0

      puts "# "
      puts "# ssh $host"
      puts "# "

      spawn ssh $host
      expect
      # here I put password check, just in case password is wrong
      # look a bit ugly, but better than nothing
      "Password:"
      if $number_of_password_prompts($host) == 0
      incr number_of_password_prompts($host)
      puts "# "
      puts "# Authenticating try $number_of_password_prompts($host)"
      puts "# "
      send "$passwordr"
      exp_continue
      else
      puts "# "
      puts "# Password is wrong"
      puts "# "
      exit


      # in case on host /bin/sh or /bin/bash
      "$ "
      puts "# "
      puts "# cat /etc/iscsi/initiatorname.iscsi"
      puts "# "
      send "cat /etc/iscsi/initiatorname.iscsi r"
      expect "$ "

      # in case on host /bin/csh
      "% "
      puts "# "
      puts "# cat /etc/iscsi/initiatorname.iscsi"
      puts "# "
      send "cat /etc/iscsi/initiatorname.iscsi r"
      expect "% "

      send "exitr"
      expect eof
      close




      Last but not the least make sure you chmod +x both files. You can also run expect other way. And also make sure you put proper location of expect. For me it is /usr/bin/expect. For you could be anything else.



      Also one important thing. For expect is important what prompt is out there on your servers. If it is /bin/bash you see I've put condition with $ , if it is /bin/csh then you have to add condition with %.



      It is very important for you to check how your $PS1 or other prompt will look like after ssh. Script above won't work if your prompt is



       user@host:~>


      For that to work, you have to include one more condition in expect



       "> " 
      puts "# "
      puts "# cat /etc/iscsi/initiatorname.iscsi"
      puts "# "
      send "cat /etc/iscsi/initiatorname.iscsi r"
      expect "> "



      Please pay attention to that, I spend many days trying to figure out why things do not work.






      share|improve this answer

























        0












        0








        0







        First of all, I don't quite understand, what are you trying to achieve.



        What I understood that you want to cat /etc/iscsi/initiatorname.iscsi on each server, right ?



        But then why do you need to interact with each server?



        I'll post solution for the first problem, where you login to each server, and cat file. I can edit solution, if you want to add interact as well.



        So first of I decided to write most of the part of the script in expect itself and not pass it using bash. I think this way you'll get more errors.



        So here is the bash script that is used as runner, it prompts for password, so that password is not printed or seen (it is not easy to do in expect directly)



        #!/bin/bash 

        expect_script="./connect_to_many_hosts_and_cat_files.exp"

        [ ! -f $expect_script ] && echo expect_script does not exist && exit 1

        echo "Please enter a password:"

        read -s password

        $expect_script $password


        Now the expect script itself



        #!/usr/bin/expect

        if $argc != 1
        puts "please run expect script using bash script"
        puts "that prompts for password"
        exit


        set password [lindex $argv 0]

        # replace hosts names with your host names !!!
        set hosts "host1 host2"

        foreach host [ split $hosts " " ]

        set number_of_password_prompts($host) 0

        puts "# "
        puts "# ssh $host"
        puts "# "

        spawn ssh $host
        expect
        # here I put password check, just in case password is wrong
        # look a bit ugly, but better than nothing
        "Password:"
        if $number_of_password_prompts($host) == 0
        incr number_of_password_prompts($host)
        puts "# "
        puts "# Authenticating try $number_of_password_prompts($host)"
        puts "# "
        send "$passwordr"
        exp_continue
        else
        puts "# "
        puts "# Password is wrong"
        puts "# "
        exit


        # in case on host /bin/sh or /bin/bash
        "$ "
        puts "# "
        puts "# cat /etc/iscsi/initiatorname.iscsi"
        puts "# "
        send "cat /etc/iscsi/initiatorname.iscsi r"
        expect "$ "

        # in case on host /bin/csh
        "% "
        puts "# "
        puts "# cat /etc/iscsi/initiatorname.iscsi"
        puts "# "
        send "cat /etc/iscsi/initiatorname.iscsi r"
        expect "% "

        send "exitr"
        expect eof
        close




        Last but not the least make sure you chmod +x both files. You can also run expect other way. And also make sure you put proper location of expect. For me it is /usr/bin/expect. For you could be anything else.



        Also one important thing. For expect is important what prompt is out there on your servers. If it is /bin/bash you see I've put condition with $ , if it is /bin/csh then you have to add condition with %.



        It is very important for you to check how your $PS1 or other prompt will look like after ssh. Script above won't work if your prompt is



         user@host:~>


        For that to work, you have to include one more condition in expect



         "> " 
        puts "# "
        puts "# cat /etc/iscsi/initiatorname.iscsi"
        puts "# "
        send "cat /etc/iscsi/initiatorname.iscsi r"
        expect "> "



        Please pay attention to that, I spend many days trying to figure out why things do not work.






        share|improve this answer













        First of all, I don't quite understand, what are you trying to achieve.



        What I understood that you want to cat /etc/iscsi/initiatorname.iscsi on each server, right ?



        But then why do you need to interact with each server?



        I'll post solution for the first problem, where you login to each server, and cat file. I can edit solution, if you want to add interact as well.



        So first of I decided to write most of the part of the script in expect itself and not pass it using bash. I think this way you'll get more errors.



        So here is the bash script that is used as runner, it prompts for password, so that password is not printed or seen (it is not easy to do in expect directly)



        #!/bin/bash 

        expect_script="./connect_to_many_hosts_and_cat_files.exp"

        [ ! -f $expect_script ] && echo expect_script does not exist && exit 1

        echo "Please enter a password:"

        read -s password

        $expect_script $password


        Now the expect script itself



        #!/usr/bin/expect

        if $argc != 1
        puts "please run expect script using bash script"
        puts "that prompts for password"
        exit


        set password [lindex $argv 0]

        # replace hosts names with your host names !!!
        set hosts "host1 host2"

        foreach host [ split $hosts " " ]

        set number_of_password_prompts($host) 0

        puts "# "
        puts "# ssh $host"
        puts "# "

        spawn ssh $host
        expect
        # here I put password check, just in case password is wrong
        # look a bit ugly, but better than nothing
        "Password:"
        if $number_of_password_prompts($host) == 0
        incr number_of_password_prompts($host)
        puts "# "
        puts "# Authenticating try $number_of_password_prompts($host)"
        puts "# "
        send "$passwordr"
        exp_continue
        else
        puts "# "
        puts "# Password is wrong"
        puts "# "
        exit


        # in case on host /bin/sh or /bin/bash
        "$ "
        puts "# "
        puts "# cat /etc/iscsi/initiatorname.iscsi"
        puts "# "
        send "cat /etc/iscsi/initiatorname.iscsi r"
        expect "$ "

        # in case on host /bin/csh
        "% "
        puts "# "
        puts "# cat /etc/iscsi/initiatorname.iscsi"
        puts "# "
        send "cat /etc/iscsi/initiatorname.iscsi r"
        expect "% "

        send "exitr"
        expect eof
        close




        Last but not the least make sure you chmod +x both files. You can also run expect other way. And also make sure you put proper location of expect. For me it is /usr/bin/expect. For you could be anything else.



        Also one important thing. For expect is important what prompt is out there on your servers. If it is /bin/bash you see I've put condition with $ , if it is /bin/csh then you have to add condition with %.



        It is very important for you to check how your $PS1 or other prompt will look like after ssh. Script above won't work if your prompt is



         user@host:~>


        For that to work, you have to include one more condition in expect



         "> " 
        puts "# "
        puts "# cat /etc/iscsi/initiatorname.iscsi"
        puts "# "
        send "cat /etc/iscsi/initiatorname.iscsi r"
        expect "> "



        Please pay attention to that, I spend many days trying to figure out why things do not work.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 10 '16 at 10:19









        Nikiforov AlexanderNikiforov Alexander

        515




        515























            -1














            Use ansible for this case.



            yum install ansible -y
            vim /etc/ansible/hosts
            [servers]
            192.168.0.2
            192.168.0.3

            ssh-keygen


            Copy public key to destination server.
            After this try to connect to server



            ansible servers -m ping


            In ansible documentation you can find information about how to rule servers.






            share|improve this answer























            • While I agree ansible is better suited for this, that is not an answer to the original question.

              – Timothy Pulliam
              Feb 4 at 18:22















            -1














            Use ansible for this case.



            yum install ansible -y
            vim /etc/ansible/hosts
            [servers]
            192.168.0.2
            192.168.0.3

            ssh-keygen


            Copy public key to destination server.
            After this try to connect to server



            ansible servers -m ping


            In ansible documentation you can find information about how to rule servers.






            share|improve this answer























            • While I agree ansible is better suited for this, that is not an answer to the original question.

              – Timothy Pulliam
              Feb 4 at 18:22













            -1












            -1








            -1







            Use ansible for this case.



            yum install ansible -y
            vim /etc/ansible/hosts
            [servers]
            192.168.0.2
            192.168.0.3

            ssh-keygen


            Copy public key to destination server.
            After this try to connect to server



            ansible servers -m ping


            In ansible documentation you can find information about how to rule servers.






            share|improve this answer













            Use ansible for this case.



            yum install ansible -y
            vim /etc/ansible/hosts
            [servers]
            192.168.0.2
            192.168.0.3

            ssh-keygen


            Copy public key to destination server.
            After this try to connect to server



            ansible servers -m ping


            In ansible documentation you can find information about how to rule servers.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Aug 22 '17 at 12:22









            QuarindQuarind

            594




            594












            • While I agree ansible is better suited for this, that is not an answer to the original question.

              – Timothy Pulliam
              Feb 4 at 18:22

















            • While I agree ansible is better suited for this, that is not an answer to the original question.

              – Timothy Pulliam
              Feb 4 at 18:22
















            While I agree ansible is better suited for this, that is not an answer to the original question.

            – Timothy Pulliam
            Feb 4 at 18:22





            While I agree ansible is better suited for this, that is not an answer to the original question.

            – Timothy Pulliam
            Feb 4 at 18:22

















            draft saved

            draft discarded
















































            Thanks for contributing an answer to Unix & Linux Stack Exchange!


            • 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%2funix.stackexchange.com%2fquestions%2f321836%2fexpect-script-for-getting-output-for-multiple-servers%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 check contact read email or not when send email to Individual?

            Bahrain

            Postfix configuration issue with fips on centos 7; mailgun relay