Expect script connects with ssh but does not run commands

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





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
0
down vote

favorite












I need to write a script that runs on a PC with Centos 7 and connects to another PC with Centos 7 using ssh, execute a console command, for example "ls -la" and save the output of it to a file to be able to later analyze that output.



I have written the following EXPECT script:



ssh_connection.exp :



#!/usr/bin/expect -f
set timeout 120
spawn ssh root@129.0.0.10
expect "assword:"
send "PASSWORDr"
expect "prompt#"
sleep 5
puts "Executing ls -la"
send "ls -lar"
sleep 10
puts "Executing ps -af"
puts "ps -afr"
sleep 10
puts "Closing the ssh sessionr"
send "exitr"


This script connects correctly through ssh to the machine with IP = 129.0.0.10
and displays on the screen the messages that appears on "puts":
Executing ls -la
Executing ps -af
Closing the ssh session



However, it does not show the result of executing the commands I send with send:



ls -la
ps -af


What is wrong with this script?



How can I make the output of the previous commands saved in a file to be able to analyze it later with a bash script or a C program?







share|improve this question

















  • 1




    Just use a standard ssh command with ssh keys and a heredoc
    – Raman Sailopal
    Jul 27 at 9:57











  • It seems you are overcomplicating stuff. You do not need to use expect to automate stuff via ssh.
    – Rui F Ribeiro
    Jul 27 at 10:15










  • did you try a command like spawn ssh root@129.0.0.10 ls -la>Ls?
    – Hossein Vatani
    Jul 27 at 10:33










  • Run you script with expect -d ssh_connection.exp and see where the problems are. First thing I'd recommend is to replace all the sleeps with expect "prompt#"
    – glenn jackman
    Jul 27 at 15:23
















up vote
0
down vote

favorite












I need to write a script that runs on a PC with Centos 7 and connects to another PC with Centos 7 using ssh, execute a console command, for example "ls -la" and save the output of it to a file to be able to later analyze that output.



I have written the following EXPECT script:



ssh_connection.exp :



#!/usr/bin/expect -f
set timeout 120
spawn ssh root@129.0.0.10
expect "assword:"
send "PASSWORDr"
expect "prompt#"
sleep 5
puts "Executing ls -la"
send "ls -lar"
sleep 10
puts "Executing ps -af"
puts "ps -afr"
sleep 10
puts "Closing the ssh sessionr"
send "exitr"


This script connects correctly through ssh to the machine with IP = 129.0.0.10
and displays on the screen the messages that appears on "puts":
Executing ls -la
Executing ps -af
Closing the ssh session



However, it does not show the result of executing the commands I send with send:



ls -la
ps -af


What is wrong with this script?



How can I make the output of the previous commands saved in a file to be able to analyze it later with a bash script or a C program?







share|improve this question

















  • 1




    Just use a standard ssh command with ssh keys and a heredoc
    – Raman Sailopal
    Jul 27 at 9:57











  • It seems you are overcomplicating stuff. You do not need to use expect to automate stuff via ssh.
    – Rui F Ribeiro
    Jul 27 at 10:15










  • did you try a command like spawn ssh root@129.0.0.10 ls -la>Ls?
    – Hossein Vatani
    Jul 27 at 10:33










  • Run you script with expect -d ssh_connection.exp and see where the problems are. First thing I'd recommend is to replace all the sleeps with expect "prompt#"
    – glenn jackman
    Jul 27 at 15:23












up vote
0
down vote

favorite









up vote
0
down vote

favorite











I need to write a script that runs on a PC with Centos 7 and connects to another PC with Centos 7 using ssh, execute a console command, for example "ls -la" and save the output of it to a file to be able to later analyze that output.



I have written the following EXPECT script:



ssh_connection.exp :



#!/usr/bin/expect -f
set timeout 120
spawn ssh root@129.0.0.10
expect "assword:"
send "PASSWORDr"
expect "prompt#"
sleep 5
puts "Executing ls -la"
send "ls -lar"
sleep 10
puts "Executing ps -af"
puts "ps -afr"
sleep 10
puts "Closing the ssh sessionr"
send "exitr"


This script connects correctly through ssh to the machine with IP = 129.0.0.10
and displays on the screen the messages that appears on "puts":
Executing ls -la
Executing ps -af
Closing the ssh session



However, it does not show the result of executing the commands I send with send:



ls -la
ps -af


What is wrong with this script?



How can I make the output of the previous commands saved in a file to be able to analyze it later with a bash script or a C program?







share|improve this question













I need to write a script that runs on a PC with Centos 7 and connects to another PC with Centos 7 using ssh, execute a console command, for example "ls -la" and save the output of it to a file to be able to later analyze that output.



I have written the following EXPECT script:



ssh_connection.exp :



#!/usr/bin/expect -f
set timeout 120
spawn ssh root@129.0.0.10
expect "assword:"
send "PASSWORDr"
expect "prompt#"
sleep 5
puts "Executing ls -la"
send "ls -lar"
sleep 10
puts "Executing ps -af"
puts "ps -afr"
sleep 10
puts "Closing the ssh sessionr"
send "exitr"


This script connects correctly through ssh to the machine with IP = 129.0.0.10
and displays on the screen the messages that appears on "puts":
Executing ls -la
Executing ps -af
Closing the ssh session



However, it does not show the result of executing the commands I send with send:



ls -la
ps -af


What is wrong with this script?



How can I make the output of the previous commands saved in a file to be able to analyze it later with a bash script or a C program?









share|improve this question












share|improve this question




share|improve this question








edited Jul 27 at 9:42









andcoz

11.5k32938




11.5k32938









asked Jul 27 at 9:41









jstechg

11




11







  • 1




    Just use a standard ssh command with ssh keys and a heredoc
    – Raman Sailopal
    Jul 27 at 9:57











  • It seems you are overcomplicating stuff. You do not need to use expect to automate stuff via ssh.
    – Rui F Ribeiro
    Jul 27 at 10:15










  • did you try a command like spawn ssh root@129.0.0.10 ls -la>Ls?
    – Hossein Vatani
    Jul 27 at 10:33










  • Run you script with expect -d ssh_connection.exp and see where the problems are. First thing I'd recommend is to replace all the sleeps with expect "prompt#"
    – glenn jackman
    Jul 27 at 15:23












  • 1




    Just use a standard ssh command with ssh keys and a heredoc
    – Raman Sailopal
    Jul 27 at 9:57











  • It seems you are overcomplicating stuff. You do not need to use expect to automate stuff via ssh.
    – Rui F Ribeiro
    Jul 27 at 10:15










  • did you try a command like spawn ssh root@129.0.0.10 ls -la>Ls?
    – Hossein Vatani
    Jul 27 at 10:33










  • Run you script with expect -d ssh_connection.exp and see where the problems are. First thing I'd recommend is to replace all the sleeps with expect "prompt#"
    – glenn jackman
    Jul 27 at 15:23







1




1




Just use a standard ssh command with ssh keys and a heredoc
– Raman Sailopal
Jul 27 at 9:57





Just use a standard ssh command with ssh keys and a heredoc
– Raman Sailopal
Jul 27 at 9:57













It seems you are overcomplicating stuff. You do not need to use expect to automate stuff via ssh.
– Rui F Ribeiro
Jul 27 at 10:15




It seems you are overcomplicating stuff. You do not need to use expect to automate stuff via ssh.
– Rui F Ribeiro
Jul 27 at 10:15












did you try a command like spawn ssh root@129.0.0.10 ls -la>Ls?
– Hossein Vatani
Jul 27 at 10:33




did you try a command like spawn ssh root@129.0.0.10 ls -la>Ls?
– Hossein Vatani
Jul 27 at 10:33












Run you script with expect -d ssh_connection.exp and see where the problems are. First thing I'd recommend is to replace all the sleeps with expect "prompt#"
– glenn jackman
Jul 27 at 15:23




Run you script with expect -d ssh_connection.exp and see where the problems are. First thing I'd recommend is to replace all the sleeps with expect "prompt#"
– glenn jackman
Jul 27 at 15:23










2 Answers
2






active

oldest

votes

















up vote
0
down vote













You don't need to use expect to do this. The ssh command can take additional arguments of commands you want to run via the SSH connection.



Step #1



Setup a SSH key pair (google it) and then copy the SSH key to the remote server. To do this I'd recommend using ssh-copy-id. See my answer to this U&L Q&A titled: How to properly copy private keys from remote servers to my localmachine so I can connect using ssh.



Step #2



Now with the ability to SSH to a server in place using a key, your above problem turns into this:



$ ssh root@129.0.0.10 "ls -la; ps -af"


You can get fancy and use here documents (heredocs aka. here-docs) to further enhance this technique.



$ ssh root@129.0.0.10 <<EOF
> ls -la
> ps -af
> EOF


or put the commands in a file and pass them to ssh:



$ ssh root@129.0.0.10 < my.cmds





share|improve this answer





















  • Why my question here unix.stackexchange.com/questions/458801/… was marked as duplicate? I believe it's different as I want to get promoted to enter the username not just the password.
    – Tak
    Jul 27 at 11:17

















up vote
0
down vote













Thanks,



I have modified my script replacing the line:



spawn ssh root@129.0.0.10



with this other, as you tell me:



spawn ssh root@129.0.0.10 "ls -la; ps -af"



Now I see the commands output on the screen and I can redirect to a file.



Next I tried to adapt this script to execute a command on another machine that is a Cisco like switch (it is not Cisco, but it is compatible).



The script with which I connect to the switch using ssh is:



**#!/usr/bin/expect -f



set timeout 120



spawn ssh user@129.0.0.50 "show vlan 500"



expect "assword:"



send "PASSWORDr"



expect "prompt#"



sleep 5



puts "Closing the ssh sessionr"



send "exitr"**





>





This does not work on the switch and I get this error:



spawn ssh user@129.0.0.50 show vlan 501



user@129.0.0.50's password:



imish: invalid option -- 'c'



Try `imish --help' for more information.



send: spawn id exp6 not open



while executing


"send "exitr""



(file "./ssh_script_v3.exp" line 7)




>





So I must use the expect script that send the command using "send" :



!/usr/bin/expect



spawn ssh user@129.0.0.50



expect "assword:"



send "userr"



expect ">"



sleep 5



send "sh vlan 500r"



sleep 5



send "exitr"





>





Now I do not get any error, but nothing is shwon on the screen.



I have read that this happens because linux and UNIX systems automatically buffer their output when running non-interactively.
I think that Expect can make the programs think they are running interactively by means of "unbuffer", but I do not know how to use "unbuffer" with "send". My attemps to do this, do not work.






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%2f458799%2fexpect-script-connects-with-ssh-but-does-not-run-commands%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
    0
    down vote













    You don't need to use expect to do this. The ssh command can take additional arguments of commands you want to run via the SSH connection.



    Step #1



    Setup a SSH key pair (google it) and then copy the SSH key to the remote server. To do this I'd recommend using ssh-copy-id. See my answer to this U&L Q&A titled: How to properly copy private keys from remote servers to my localmachine so I can connect using ssh.



    Step #2



    Now with the ability to SSH to a server in place using a key, your above problem turns into this:



    $ ssh root@129.0.0.10 "ls -la; ps -af"


    You can get fancy and use here documents (heredocs aka. here-docs) to further enhance this technique.



    $ ssh root@129.0.0.10 <<EOF
    > ls -la
    > ps -af
    > EOF


    or put the commands in a file and pass them to ssh:



    $ ssh root@129.0.0.10 < my.cmds





    share|improve this answer





















    • Why my question here unix.stackexchange.com/questions/458801/… was marked as duplicate? I believe it's different as I want to get promoted to enter the username not just the password.
      – Tak
      Jul 27 at 11:17














    up vote
    0
    down vote













    You don't need to use expect to do this. The ssh command can take additional arguments of commands you want to run via the SSH connection.



    Step #1



    Setup a SSH key pair (google it) and then copy the SSH key to the remote server. To do this I'd recommend using ssh-copy-id. See my answer to this U&L Q&A titled: How to properly copy private keys from remote servers to my localmachine so I can connect using ssh.



    Step #2



    Now with the ability to SSH to a server in place using a key, your above problem turns into this:



    $ ssh root@129.0.0.10 "ls -la; ps -af"


    You can get fancy and use here documents (heredocs aka. here-docs) to further enhance this technique.



    $ ssh root@129.0.0.10 <<EOF
    > ls -la
    > ps -af
    > EOF


    or put the commands in a file and pass them to ssh:



    $ ssh root@129.0.0.10 < my.cmds





    share|improve this answer





















    • Why my question here unix.stackexchange.com/questions/458801/… was marked as duplicate? I believe it's different as I want to get promoted to enter the username not just the password.
      – Tak
      Jul 27 at 11:17












    up vote
    0
    down vote










    up vote
    0
    down vote









    You don't need to use expect to do this. The ssh command can take additional arguments of commands you want to run via the SSH connection.



    Step #1



    Setup a SSH key pair (google it) and then copy the SSH key to the remote server. To do this I'd recommend using ssh-copy-id. See my answer to this U&L Q&A titled: How to properly copy private keys from remote servers to my localmachine so I can connect using ssh.



    Step #2



    Now with the ability to SSH to a server in place using a key, your above problem turns into this:



    $ ssh root@129.0.0.10 "ls -la; ps -af"


    You can get fancy and use here documents (heredocs aka. here-docs) to further enhance this technique.



    $ ssh root@129.0.0.10 <<EOF
    > ls -la
    > ps -af
    > EOF


    or put the commands in a file and pass them to ssh:



    $ ssh root@129.0.0.10 < my.cmds





    share|improve this answer













    You don't need to use expect to do this. The ssh command can take additional arguments of commands you want to run via the SSH connection.



    Step #1



    Setup a SSH key pair (google it) and then copy the SSH key to the remote server. To do this I'd recommend using ssh-copy-id. See my answer to this U&L Q&A titled: How to properly copy private keys from remote servers to my localmachine so I can connect using ssh.



    Step #2



    Now with the ability to SSH to a server in place using a key, your above problem turns into this:



    $ ssh root@129.0.0.10 "ls -la; ps -af"


    You can get fancy and use here documents (heredocs aka. here-docs) to further enhance this technique.



    $ ssh root@129.0.0.10 <<EOF
    > ls -la
    > ps -af
    > EOF


    or put the commands in a file and pass them to ssh:



    $ ssh root@129.0.0.10 < my.cmds






    share|improve this answer













    share|improve this answer



    share|improve this answer











    answered Jul 27 at 10:21









    slm♦

    232k65479649




    232k65479649











    • Why my question here unix.stackexchange.com/questions/458801/… was marked as duplicate? I believe it's different as I want to get promoted to enter the username not just the password.
      – Tak
      Jul 27 at 11:17
















    • Why my question here unix.stackexchange.com/questions/458801/… was marked as duplicate? I believe it's different as I want to get promoted to enter the username not just the password.
      – Tak
      Jul 27 at 11:17















    Why my question here unix.stackexchange.com/questions/458801/… was marked as duplicate? I believe it's different as I want to get promoted to enter the username not just the password.
    – Tak
    Jul 27 at 11:17




    Why my question here unix.stackexchange.com/questions/458801/… was marked as duplicate? I believe it's different as I want to get promoted to enter the username not just the password.
    – Tak
    Jul 27 at 11:17












    up vote
    0
    down vote













    Thanks,



    I have modified my script replacing the line:



    spawn ssh root@129.0.0.10



    with this other, as you tell me:



    spawn ssh root@129.0.0.10 "ls -la; ps -af"



    Now I see the commands output on the screen and I can redirect to a file.



    Next I tried to adapt this script to execute a command on another machine that is a Cisco like switch (it is not Cisco, but it is compatible).



    The script with which I connect to the switch using ssh is:



    **#!/usr/bin/expect -f



    set timeout 120



    spawn ssh user@129.0.0.50 "show vlan 500"



    expect "assword:"



    send "PASSWORDr"



    expect "prompt#"



    sleep 5



    puts "Closing the ssh sessionr"



    send "exitr"**





    >





    This does not work on the switch and I get this error:



    spawn ssh user@129.0.0.50 show vlan 501



    user@129.0.0.50's password:



    imish: invalid option -- 'c'



    Try `imish --help' for more information.



    send: spawn id exp6 not open



    while executing


    "send "exitr""



    (file "./ssh_script_v3.exp" line 7)




    >





    So I must use the expect script that send the command using "send" :



    !/usr/bin/expect



    spawn ssh user@129.0.0.50



    expect "assword:"



    send "userr"



    expect ">"



    sleep 5



    send "sh vlan 500r"



    sleep 5



    send "exitr"





    >





    Now I do not get any error, but nothing is shwon on the screen.



    I have read that this happens because linux and UNIX systems automatically buffer their output when running non-interactively.
    I think that Expect can make the programs think they are running interactively by means of "unbuffer", but I do not know how to use "unbuffer" with "send". My attemps to do this, do not work.






    share|improve this answer

























      up vote
      0
      down vote













      Thanks,



      I have modified my script replacing the line:



      spawn ssh root@129.0.0.10



      with this other, as you tell me:



      spawn ssh root@129.0.0.10 "ls -la; ps -af"



      Now I see the commands output on the screen and I can redirect to a file.



      Next I tried to adapt this script to execute a command on another machine that is a Cisco like switch (it is not Cisco, but it is compatible).



      The script with which I connect to the switch using ssh is:



      **#!/usr/bin/expect -f



      set timeout 120



      spawn ssh user@129.0.0.50 "show vlan 500"



      expect "assword:"



      send "PASSWORDr"



      expect "prompt#"



      sleep 5



      puts "Closing the ssh sessionr"



      send "exitr"**





      >





      This does not work on the switch and I get this error:



      spawn ssh user@129.0.0.50 show vlan 501



      user@129.0.0.50's password:



      imish: invalid option -- 'c'



      Try `imish --help' for more information.



      send: spawn id exp6 not open



      while executing


      "send "exitr""



      (file "./ssh_script_v3.exp" line 7)




      >





      So I must use the expect script that send the command using "send" :



      !/usr/bin/expect



      spawn ssh user@129.0.0.50



      expect "assword:"



      send "userr"



      expect ">"



      sleep 5



      send "sh vlan 500r"



      sleep 5



      send "exitr"





      >





      Now I do not get any error, but nothing is shwon on the screen.



      I have read that this happens because linux and UNIX systems automatically buffer their output when running non-interactively.
      I think that Expect can make the programs think they are running interactively by means of "unbuffer", but I do not know how to use "unbuffer" with "send". My attemps to do this, do not work.






      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        Thanks,



        I have modified my script replacing the line:



        spawn ssh root@129.0.0.10



        with this other, as you tell me:



        spawn ssh root@129.0.0.10 "ls -la; ps -af"



        Now I see the commands output on the screen and I can redirect to a file.



        Next I tried to adapt this script to execute a command on another machine that is a Cisco like switch (it is not Cisco, but it is compatible).



        The script with which I connect to the switch using ssh is:



        **#!/usr/bin/expect -f



        set timeout 120



        spawn ssh user@129.0.0.50 "show vlan 500"



        expect "assword:"



        send "PASSWORDr"



        expect "prompt#"



        sleep 5



        puts "Closing the ssh sessionr"



        send "exitr"**





        >





        This does not work on the switch and I get this error:



        spawn ssh user@129.0.0.50 show vlan 501



        user@129.0.0.50's password:



        imish: invalid option -- 'c'



        Try `imish --help' for more information.



        send: spawn id exp6 not open



        while executing


        "send "exitr""



        (file "./ssh_script_v3.exp" line 7)




        >





        So I must use the expect script that send the command using "send" :



        !/usr/bin/expect



        spawn ssh user@129.0.0.50



        expect "assword:"



        send "userr"



        expect ">"



        sleep 5



        send "sh vlan 500r"



        sleep 5



        send "exitr"





        >





        Now I do not get any error, but nothing is shwon on the screen.



        I have read that this happens because linux and UNIX systems automatically buffer their output when running non-interactively.
        I think that Expect can make the programs think they are running interactively by means of "unbuffer", but I do not know how to use "unbuffer" with "send". My attemps to do this, do not work.






        share|improve this answer













        Thanks,



        I have modified my script replacing the line:



        spawn ssh root@129.0.0.10



        with this other, as you tell me:



        spawn ssh root@129.0.0.10 "ls -la; ps -af"



        Now I see the commands output on the screen and I can redirect to a file.



        Next I tried to adapt this script to execute a command on another machine that is a Cisco like switch (it is not Cisco, but it is compatible).



        The script with which I connect to the switch using ssh is:



        **#!/usr/bin/expect -f



        set timeout 120



        spawn ssh user@129.0.0.50 "show vlan 500"



        expect "assword:"



        send "PASSWORDr"



        expect "prompt#"



        sleep 5



        puts "Closing the ssh sessionr"



        send "exitr"**





        >





        This does not work on the switch and I get this error:



        spawn ssh user@129.0.0.50 show vlan 501



        user@129.0.0.50's password:



        imish: invalid option -- 'c'



        Try `imish --help' for more information.



        send: spawn id exp6 not open



        while executing


        "send "exitr""



        (file "./ssh_script_v3.exp" line 7)




        >





        So I must use the expect script that send the command using "send" :



        !/usr/bin/expect



        spawn ssh user@129.0.0.50



        expect "assword:"



        send "userr"



        expect ">"



        sleep 5



        send "sh vlan 500r"



        sleep 5



        send "exitr"





        >





        Now I do not get any error, but nothing is shwon on the screen.



        I have read that this happens because linux and UNIX systems automatically buffer their output when running non-interactively.
        I think that Expect can make the programs think they are running interactively by means of "unbuffer", but I do not know how to use "unbuffer" with "send". My attemps to do this, do not work.







        share|improve this answer













        share|improve this answer



        share|improve this answer











        answered Jul 27 at 16:01









        jstechg

        11




        11






















             

            draft saved


            draft discarded


























             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f458799%2fexpect-script-connects-with-ssh-but-does-not-run-commands%23new-answer', 'question_page');

            );

            Post as a guest













































































            Popular posts from this blog

            How to check contact read email or not when send email to Individual?

            Displaying single band from multi-band raster using QGIS

            How many registers does an x86_64 CPU actually have?