Mixing Expect and BASH - bash

I have written a small Expect script to log into a Cisco device; once logged in I want to repeatedly run a command and grep the output.
#!/usr/bin/expect
send_user "Device name: "
expect_user -re "(.*)\n"
set host $expect_out(1,string)
send_user "Username: "
expect_user -re "(.*)\n"
set user $expect_out(1,string)
stty -echo
send_user -- "Password: "
expect_user -re "(.*)\n"
set pass $expect_out(1,string)
stty echo
send_user "show int "
expect_user -re "(.*)\n"
set intf $expect_out(1,string)
send_user "\n"
spawn telnet $host
expect "Username:"
send "$user\r"
expect "Password:"
send "$pass\r"
expect ">"
At this point we have logged into the device, I want to execute the command "show int xxx" repeatedly and grep the output for a specific line. grep isn't in Expect, nor a command like sleep, so I can loop round executing the show int command, grepping out my specific line. How can I mix Expect and Bash like this?
UPDATE: I've pretty much done the script now, I'll post the full script once I get over this last hurdle. A line set bytesnow [exec grep "packets input" \< showint | cut -d \ -f 9] is throwing the error;
child process exited abnormally
while executing
"exec grep "packets input" < \showint | cut -d \ -f 9"
But it works fine in a test script I wrote. The file ./showint is there, running that command on the command line works fine? I can't work out what's wrong?
UPDATE: More investigation (http://wiki.tcl.tk/8489) has shown me that the grep exits with status code 1, which means no pattern matches were found, put the command works just fine from the command line? Even with /full/path/to/showint.
END: I fixed my mistake by realising what a fool I had been, answered below. Thanks all for your help :D

This is what I would do
log_user 0
while(1) {
send -- "sh int $intf | i packets input\r"
set timeout 5
expect {
-re "^ +(\d+) packets" { send_user -- "$expect_out(1,string)" }
timeout { send_user "broke?\n" }
}
}
That'll get you the number of packets input.

This is my first Expect script, its purpose is to give the live (almost, 1 second!) throughput of an interface. The below example gives an interface input speed, because we grep for the line containing "packets input". Change this to "packets output" to get a live output rate for that interface.
#!/usr/bin/expect
# Long delay for those tricky hostnames
set timeout 60
# Prompt user for device name/IP, username, password,
# and interface to query (gi0/2)
send_user "Device name: "
expect_user -re "(.*)\n"
set host $expect_out(1,string)
send_user "Username: "
expect_user -re "(.*)\n"
set user $expect_out(1,string)
stty -echo
send_user "Password: "
expect_user -re "(.*)\n"
set pass $expect_out(1,string)
send_user "\n"
stty echo
send_user "show int "
expect_user -re "(.*)\n"
set intf $expect_out(1,string)
send_user "\n"
spawn telnet $host
expect "Username:"
send "$user\r"
expect "Password:"
send "$pass\r"
expect ">"
set byteslast 0
set bytesnow 0
log_user 0
# Enter a continuous loop grabbing the number of bytes that
# have passed through an interface, each second.
# The different in this number each cycle, is essentially
# how much traffic this interface is pushing.
while { true } {
send "show int $intf\r"
expect ">"
set showint [open "showint" "w"]
puts $showint $expect_out(buffer)
close $showint
set bytesnow [exec grep "packets input" \< showint | cut -d \ -f 9]
if { $bytesnow > $byteslast } {
set diff [expr $bytesnow - $byteslast]
set bps [exec expr "$diff" \* 8]
set kbps [exec expr "$bps" \/ 1000]
} elseif { $bytesnow < $byteslast } {
set diff [expr $byteslast - $bytesnow]
set bps [exec expr "$diff" \* 8]
set kbps [exec expr "$bps" \/ 1000]
} elseif { $bytesnow == $byteslast } {
set kbps 0
}
set byteslast $bytesnow
puts "$kbps Kbps\r"
sleep 1
}
As this is my first Expect script, I have no doubt it could be written more efficiently and clearly (that always the case I find), so if anyone has any pointers on this one I'm all ears! :)
My problem with my exec grep command turned out to be that prior to that, the file I had opened "showint", I hadn't closed, and I was trying to access another file; school boy mistake!

Related

use expect to check if command output contains a word (else error)

I want to check if there is an interface in the output of the command:
ip a
If there is not, then I want to stop the execution. I've tried this code:
#!/usr/bin/expect -f
spawn bash
set spi_bash $spawn_id
send "ip a\r"
expect {
-re "docker0" {puts "docker0 is there\n"; exp_continue }
timeout {puts "no docker0\n"; exit 1}
}
exit
but exp_continue continues to timeout case.
I'd suggest you don't need expect for this:
set output [exec ip a]
if {[string first docker0 $output] != -1} {
puts "no docker0"
}
But if this is part of a larger expect program, you can write
spawn bash
set spi_bash $spawn_id
# a regex matching your prompt: a dollar sign, a space, and end of input
# adjust as required
set prompt {\$ $}
send "ip a\r"
set has_docker false
expect {
-re "docker0" {set has_docker true; exp_continue}
-re $prompt
}
send "exit\r"
expect eof
if {$has_docker}
puts "docker0 is there"
} else {
puts "no docker0"
}
exit [expr {!$has_docker}]

How to make expect timestamp logfile

I'm trying to make my expect script to create one log file with a timestamp and create another with timestamp next time it runs but can't find any info on how.
I have a file with a list of hosts that expect connects to and run a set of command that I want to log all events from in one file when it runs.
Host1
Host2
Host3
etc
I have manage to create log file with timestamp:
log_file -a ~/log/[exec date]_results.log
Also tried with:
log_file -a -noappend ~/log/[exec date]_results.log
But it creates a new one for every line of hosts:
Thu Mar 8 15:28:24 CET 2018_results.log
Thu Mar 8 15:28:25 CET 2018_results.log
Thu Mar 8 15:28:26 CET 2018_results.log
Thu Mar 8 15:28:27 CET 2018_results.log
Thu Mar 8 15:28:28 CET 2018_results.log
I found one solution.
Added this to my .sh
timestamp=`date +%y-%m-%d_%H:%M`
logdir=~/log/
# This will rotate and append date stamp...
logfile=$logdir/results.log
newlogfile=$logfile.$timestamp
cp $logfile $newlogfile
This works but will only add timestamp to the rotated file, the results.log file will be the newest one and and the rotated file will be stamped with date and time from when you run the script, so the timestamp of that file will be wrong.
Here are both .sh and .exp scripts if there is a solution to timestamp all the files with the correct date/time.
.sh:
#!/bin/bash
# Collect the current user's ssh password file to copy.
echo -n "Enter the telnet password for $(whoami) : "
read -s -e password
echo -ne '\n'
echo -n "Enter the command to run on device : "
read -e command
echo -ne '\n'
timestamp=`date +%y-%m-%d_%H:%M`
logdir=~/log/
# This will rotate and append date stamp...
logfile=$logdir/results.log
newlogfile=$logfile.$timestamp
cp $logfile $newlogfile
names=()
ips=()
n=0
while read name ip; do
[[ -z $ip ]] && continue
names[n]=$name
ips[n]=$ip
((++n))
done < hostlist
for ((i=0; i<n; ++i)); do
./script.exp ${names[i]} ${ips[i]} $device "$password" "$command"
done
.exp:
#!/usr/bin/expect -f
# Set variables
set hostname [lindex $argv 0]
set ipadd [lindex $argv 1]
set username $env(USER)
set password [lindex $argv 2]
set command [lindex $argv 3]
set timeout 20
# Log results
log_file -a ~/log/results.log
# Announce which device we are working on and at what time
send_user "\n"
send_user ">>>>> Working on $hostname # [exec date] <<<<<\n"
send_user "\n"
expect_after timeout {send_user "Timeout happened connecting to $hostname; So, exiting....";exit 0}
spawn telnet $hostname
expect "*sername:"
send "$username\n"
expect "*assword:"
send "$password\n"
expect "*#"
send "$command $ipadd\n"
expect "*#"
send "exit\n"
expect ":~\$"
exit
You can do all of this from within Expect:
#!/usr/bin/expect -f
set host_file "hostlist"
if {![file readable $host_file]} {
error "cannot read $host_file"
}
# get info from user
set me [exec whoami]
stty -echo
send_user "Enter the telnet password for $me : "
expect_user -re {(.*)\n}
set password $expect_out(1,string)
send_user "\n"
stty echo
send_user "Enter the command to run on device : "
expect_user -re {(.*)\n}
set command $expect_out(1,string)
set timestamp [timestamp -format %Y-%m-%d_%H:%M]
set logfile $env(HOME)/log/results_$timestamp.log
log_file -a $logfile
expect_after timeout {send_user "Timeout happened connecting to $hostname; So, exiting....";exit 0}
set fh [open $host_file r]
while {[gets $fh line] != -1} {
lassign [regexp -all -inline {\S+} $line] hostname ip
if {$hostname eq "" || $ip eq ""} continue
send_user "\n"
send_user ">>>>> Working on $hostname # [timestamp -format %c] <<<<<\n"
send_user "\n"
spawn telnet $hostname
expect "*sername:"
send "$me\r"
expect "*assword:"
send "$password\r"
expect "*#"
send "$command $ip\r"
expect "*#"
send "exit\r"
expect eof
}
To handle timeouts for user input, there are 2 strategies:
set the timeout variable to -1 to wait indefinitely:
set prev_timeout $timeout
set timeout -1
send_user "Enter the thing: "
expect_user -re "(.*)\n" ;# waits forever
set the_thing $expect_out(1,string)
set timeout $prev_timeout
use an infinite loop to provide feedback to the user
while 1 {
send_user "Enter the thing: "
expect_user {
-re "(.*)\n" {break}
timeout {send_error "timeout!\n"}
}
}
set the_thing $expect_out(1,string)

Expect script if a user exists before creating

I wrote an expect script to create a user in unix server. It basically connects via SSH to a server using my credential and su to root to do useradd and etc. (I understand there are other methods to accomplish the same but I am restricted with such settings and environment currently.)
set prompt "(%|#|>|\\\$ )"
set prompt [string trim $prompt]
spawn ssh -o StrictHostKeyChecking=no -l $my_user $hostname
expect "?assword: "
send "$my_pass\r"
expect -re $prompt
send "/usr/bin/su - \r"
expect "?assword: "
send "$root_pass\r"
expect -re $prompt
send "/usr/sbin/useradd -d /export/home/$user -m -s /bin/sh $user \r"
expect -re $prompt
send "/usr/bin/passwd $user \r"
expect "?assword:"
send "$new_pass\r"
expect "?assword:"
send "$new_pass\r"
send "exit\r"
expect -re $prompt
send "exit\r"
expect -re $prompt
However if I am stuck at adding a logic to check whether a user already exists in the system. If it were in bash, I would have added grep -c '^USER' /etc/passwd to check for the returned number. But I am unable to capture the return number from expect. There is so much information returned once I added:
send "egrep -c '^$user' /etc/passwd \r"
set output $expect_out(buffer)
Could someone tell me how to parse out all the output? I know it is a very simple task. It is probably a simple if ... then .. else but I am unable to produce anything useful in the past week.
Assuming your shell on the remote host is sh-based, and the remote system is linux:
set cmd [format {getent passwd %s >/dev/null 2>&1; [ "$?" -eq 2 ] && /usr/sbin/useradd -d /export/home/%s -m -s /bin/sh %s} $user $user $user]
send "$cmd\r"
I'm using format (known as sprintf in other languages) to ease quoting.
After spending another few hours studying tcl, this is working now.
I replace this block of code after I enter the root_pass.
send "\r"
expect -re $prompt
expect *;
send "egrep -c '^$user:' /etc/passwd \r"
expect -re $prompt
set output $expect_out(buffer);
set ans [ split $output \n ]
set var [lindex $ans 1]
if { $var >= 1 } {
puts "Found.\r"
send "exit\r"
expect eof
} else {
puts "Not found.\r"
send "/usr/sbin/useradd -d /export/home/$user -m -s /bin/sh $user \r"
.....
}

Expect script: to perform actions after closing ssh

The question is to preserve a variable and to perform actions after closing ssh within expect script inside bash.
This is what I`ve got so far:
echo "Getting package name..."
getPackageName=$(expect -c '
exp_internal 1
log_user 1
global expect_out
# puts "Getting package name..."
spawn ssh -q -o StrictHostKeyChecking=no -o PreferredAuthentications=password -o PubkeyAuthentication=no -o RSAAuthentication=no -l user 10.20.30.40
sleep 1
expect {
"*sword*" {
send "12341234\r"
}
timeout {
send_user "Error: timeout\n"
exit 1
}
}
expect {
"*user#*>*" {
# getting name of the latest modified file
send "cd /export/home/user/Releases/build/1.3.32.0 && find * -type f -printf '"'"'%T# %p\\n'"'"' | sort -n | tail -1 | cut -f2- -d\" \"\r"
}
timeout {
send_user "Error: timeout\n"
exit 1
}
}
expect {
"BUILD_MAIN*" {
# assigning value to variable
set result_lines [split $expect_out(0,string) \r\n]
set package_filename [lindex $result_lines 0]
puts "package_filename: $package_filename"
}
timeout {
send_user "Error: timeout\n"
exit 1
}
}
expect "*#"
send "exit\r"
# here I need to perform some actions on local machine after ssh logout
expect "Connection*"
send "export LATEST_BUILD=$package_filename\r"
send_user "Message sent to user"
')
So, in the bottom block I am trying to set environment variable (LATEST_BUILD) on the local machine after closing ssh, and also to paste there a value of variable (package_filename) which has been defined earlier during ssh session.
The point here is that I see the last "Message sent to user" in the output, but the previous send "export LATEST_BUILD=12345\r" obviously does not work.
#!/bin/bash
getPackageName=$(expect -c '
# A common prompt matcher
set prompt "%|>|#|\\\$ $"
# To suppress any other form of output generated by spawned process
log_user 0
### Spawning ssh here ###
spawn ssh user#xxx.xx.xxx.xxx
expect "password"
send "welcome!2E\r"
expect -re $prompt
# Your further code
send "exit\r"
expect eof
##### The below segment is not needed ######
##### if your intention is to get only the 'package_filename' value #####
# spawn bash
# expect -re $prompt
# send "export LATEST_BUILD=54.030\r"
# expect -re $prompt
# send "echo \$LATEST_BUILD\r"
# expect -re $prompt
# send "exit\r"
# expect eof
#
##### The End ######
# Enabling logging now ...
log_user 1
# Print only the value which you want to return
puts "$package_filename"
')
echo $getPackageName
eof is used to identify the end-of-file event i.e. closure of connection.
Note : The exported variable LATEST_BUILD only be available for the spawned bash session.
Update :
log_user is used to turn off/on the logging generated by Expect at any time.
log_user 0; # Turn off logging
log_user 1; # Turn on logging
I hope that your only intention is to get the package_filename. So, we don't even need to spawn bash shell. Instead, simply print the value at last, thereby making it to be available to the parent bash script.

expect script to ssh returns invalid command name

I am trying to write an expect script which would ssh into a server, send sudo su, then check the iptables status and put the output in a log file on the server. Below is the script.
1 #!/usr/bin/expect
2 exp_internal 1
3 log_user 0
4 set timeout 10
5 set password "******"
6
7 spawn /usr/bin/ssh -l subhasish *.*.*.* -p 10022
8
9 expect {
10 -re "password: " {send "$password\r"}
11 -re "$ " {send "sudo su\r"}
12 -re "[sudo] password for subhasish:" {send "$password\r"}
13 -re "# " {send "service iptables status\r"}
14 }
15 set output $expect_out(buffer)
16 send "exit\r"
17 puts "$output\r\n" >> output.log
But while run in debug mode, I am getting error like this;
expect -d testcase
expect version 5.44.1.15
argv[0] = expect argv[1] = -d argv[2] = testcase
set argc 0
set argv0 "testcase"
set argv ""
executing commands from command file testcase
parent: waiting for sync byte
parent: telling child to go ahead
parent: now unsynchronized from child
spawn: returns {24105}
invalid command name "sudo"
while executing
"sudo"
invoked from within
"expect {
-re "password: " {send "$password\r"}
-re "$ " {send "sudo su\r"}
-re "[sudo] password for subhasish:" {send "$password\r"}
..."
(file "testcase" line 9)
Not sure where I am going wrong. It says invalid command name "sudo", I guess this is because expect doesn;t understand these command. How to go around it. Please help. Thanks.
The problem is in this line
-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"}
or
-ex {[sudo] password for subhasish:} {send "$password\r"}
Thanks Glenn, it's working now. One more reason why it was not working was it requires to sleep between normal login and sudo login, otherwise "sudo su" were being sent before the $ prompt returned, for reference to others, here is the code,
#!/usr/bin/expect -f
#! /bin/bash
set timeout 60
log_user 1
set host *.*.*.*
set password ******
set user subhasish
set logfile output.txt
spawn ssh -p 10022 $user#$host
expect "*?assword:*"
send -- "$password\r"
#log_user 1
sleep 2
expect "$"
send -- "sudo su\r"
expect -gl {*password for subhasish:*}
send -- "$password\r"
expect "#"
send -- "service iptables status\r"
log_file /home/subhasish/output.log
expect "#"
log_file
send -- "exit\r";
send -- "exit\r";
exit 0

Resources