Neelkanth's Expect Script Notes

#################################################################################
Neelkanth's expect notes
#################################################################################


##################################################################
How to tell bash that the line continues on the next line
##################################################################
The character is a backslash \
From the bash manual:
The backslash character ‘\’ may be used to remove any special 
meaning for the next character read and for line continuation.

################################################################
How to execute linux system shell commands in expect
################################################################
system "ls -lrt"

###########################################################
how to access member of an array:
###########################################################
set pwd_list {
          admin123
          admiaskdfhasgdfkasdghfkasdhgn123
          admin123
}
set prev_password [lindex $pwd_list 0]
puts "prev_password $prev_password"


###########################################################
command line arguments
###########################################################
$ cat  print_cmdline_args.exp
#!/usr/bin/expect
puts 'argv0 : [lindex $argv 0]';
puts 'argv1 : [lindex $argv 1]';


puts "\nCommand line argument password: argv0 : [lindex $argv 0]   reboot count: argv1 : [lindex $argv 1]"
# pass command line argument
set password [lindex $argv 0]
if {$password  == ""} {
    puts "enter password as 1st argument to the script"
    send "quit\r"
    exit
}

set REBOOT_COUNT [lindex $argv 1]
if {$REBOOT_COUNT  == ""} {
    puts "enter reboot count as 2nd argument to the script"
    send "quit\r"
    exit
}

############################################################
ctrl + c in expect
############################################################
send \x03

############################################################
# send CTRL + ]
############################################################
send “35\r”

##########################################################################################
#  Why send "[xyz\r]" should be in send "\[xyz\r\]"
##########################################################################################
    -re "[sudo] password for subhasish:" {send "$password\r"}
    In Tcl (and hence in expect) the square brackets are the syntax
    for command substitution (like backticks in the shell). So you
    either need to escape the brackets or use different quotes that
    prevent various expansions:

    -re {[sudo] password for subhasish:} {send "$password\r"}
    That brings up a different issue: are you expecting to see these
    exact characters? Because you're instructing expect to treat that as
    a regular expression, and square brackets in a regular expression means
    a character class, so it will match a single character,
    either a 's', 'u', 'd' or 'o'. So what you probably need is this:
    -re {\[sudo\] password for subhasish:} {send "$password\r"}
###########################################################################################



############################################################
How to use multiple conditions in expect scripting
############################################################
spawn ssh remotessh@$ADMIN_IP_ADDRESS
expect "*password" {send "admin123\r"}
# If ssh login is successful, test case is failed
sleep 2
expect {
    "remotessh completed login" {
                                 terminate_telnet_ssh_session ssh
                                 sleep 1
                                }
    "Permission denied, please try again" {
                                        send “35\r”
                                        # If ssh login fails, test case is failed
                                        send ""
                                          }
}




If the program terminates, there can be no more output forthcoming. 
expect recognizes this. Specifically, expect recognizes the closing of
 the connection to the spawned process. This closing is referred to as end of file or more succinctly, eof.[20


######################################################################
How to fetch the output string into a variable after expect
######################################################################
When matching a pattern, 
any matching and previously unmatched output is saved in the variable expect_out(buffer). 
The matched output may be found in expect_out(0,string).

If a regular expression with sub-expressions was used, then the matching sub-expressions 
(up to 9 in total) may be found in expect_out(1,string) through expect_out(1,string).


  expect "=="
  set  a  $expect_out(buffer)
  set  b  $expect_out(0,string)
  #The easiest way is to use regexp -all -inline to select and return all words.
  set c [regexp -all -inline {\S+} $a]

  set res [lindex $c 2]