0

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 "PASSWORD\r"
expect "prompt#"
sleep 5
puts "Executing ls -la"
send "ls -la\r"
sleep 10
puts "Executing ps -af"
puts "ps -af\r"
sleep 10
puts "Closing the ssh session\r"
send "exit\r"

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?

  • 1
    Just use a standard ssh command with ssh keys and a heredoc – Raman Sailopal Jul 27 '18 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 '18 at 10:15
  • did you try a command like spawn ssh root@129.0.0.10 ls -la>Ls? – Hossein Vatani Jul 27 '18 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 '18 at 15:23
0

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
0

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 "PASSWORD\r"

expect "prompt#"

sleep 5

puts "Closing the ssh session\r"

send "exit\r"**

>

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 "exit\r""

(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 "user\r"

expect ">"

sleep 5

send "sh vlan 500\r"

sleep 5

send "exit\r"

>

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.