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 [email protected]
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?
spawn ssh [email protected] ls -la>Ls
?expect -d ssh_connection.exp
and see where the problems are. First thing I'd recommend is to replace all the sleeps withexpect "prompt#"