1

I'm new to scripting and need some help. I am creating an expect script that from a Linux server, that will automatically ssh/log in to our HP StoreOnce Appliance

#!/usr/bin/expect -f
set password "password"
match_max 1000
spawn ssh -o StrictHostKeyChecking=no user@10.x.x.x
expect "*?assword:*"
send -- "$password\r"
send -- "\r"
interact

When executing this script, I was able to automatically login and from there I can type the commands.

Please see output below:

=================================================================
[root@dpis tmp]# ./test.exp
spawn ssh -o StrictHostKeyChecking=no user@10.x.x.x
Password:
Last login: Thu Jan 26 08:37:24 2017 from 10.x.x.x

Welcome to the HP StoreOnce Backup System Command Line Interface.
Type 'help' at the prompt for context-sensitive help.

>

But what I want to achieve is for the script to not only automatically log-in but also automatically put a command and show its output then exit.

I tried to add the following lines below after interact

expect ">"

send -- "serviceset show status"

Seeking assistance on how to achieve this? Thanks!

1

You have a good start. After you get expect to type the command, you have to hit Enter (send the character \r). Then you have to expect another prompt so the results can be displayed

expect ">"
send -- "serviceset show status\r"
expect ">"

Capturing output in expect is a bit of a PITA. You can do something like this:

# at the top of the program
log_user 0
# ... log in ...
expect ">"

set cmd "serviceset show status\r"
send -- $cmd
expect -re "$cmd\n(.*)\r\n>"
set output $expect_out(1,string)
puts $output

# disconnect
send -- "exit\r"
expect eof
  • Hi glenn, got an error when I used your suggestion for capturing output. However I can now display my result using your first suggestion then exit. Is it possible to convert the output of my command into a .txt file then send it to the Linux server's /var/tmp wherein I executed it? Can you assist me with that? Thanks – Wilfredo Tario Jan 27 '17 at 9:08
  • You could either just redirect the output of the script (expect script.exp > /var/tmp/file.txt) or use the expect log_file command. – glenn jackman Jan 27 '17 at 14:10

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.