Cannot get this bash/expect script to run under a crontab
Code:
#!/bin/bash
#
# RAP configuration script
#
# Usage: ./rap.sh
#
# Requires: expect, tcl
#
# Script expects to find a file called rap.csv located in the same directory as the script. If the file is placed
# in a different directory, modify the custom entries section to specify the absolute path to the rap.csv file.
#
# rap.csv should be in this format:
# MAC address,AP-group,AP-name,Description
# 00:00:00:00:00:00,East-VIP,JunaidTest,TestRAP2
#
# Controller command format:
# local-userdb-ap add mac-address 00:00:00:00:00:00 ap-group East-VIP ap-name JunaidTest description TestRAP2 full-name rap-user
#
# Caveats:
#
# - A failure to execute a command on the controller is not gracefully handled and the output file shows that the entry
# has been processed when it may not have been.
# "Invalid input detected at '^' marker."
# "Userwith same remote IP already exists"
# - Login failures are not gracefully handled:
# bad username/password
# inability to connect to controller
# ssh_exchange_identification: Connection closed by remote host
# - Failure to enter enable mode on controller is not gracefully handled.
# - Script will "hang" if server on which the script is hosted has never accessed the controller (No server SSH key stored locally)
# Are you sure you want to continue connecting (yes/no)? yes
# Warning: Permanently added '10.37.192.160' (RSA) to the list of known hosts.
# (Workaround: connect once to controller from the server hosting the script and manually say "yes" so that the host is saved.)
#
# Customize this block as needed
# BEGIN custom entries
echo "This is running"
export PROG="/ftp/office_in_a_box/rap.sh"
export RAPCSV="/ftp/office_in_a_box/rap.csv"
export USER=test
export PROMPT=".*\]\$ "
export PROMPT1=".*\) >"
export PROMPT2=".* #"
export PASSWORD1="password"
export PASSWORD2="password"
export HOST="10.10.10.10."
# END custom entries
#
#
# Set common
export PATH="/usr/local/bin:/usr/bin:/usr/sbin:/bin:/sbin"
DATE=`date +%Y-%m-%d-%H%M%S`
OUTFILE=$RAPCSV.out.$DATE
#
print_usage() {
echo "Usage: $PROG"
}
while test -n "$1"
do
print_usage; exit $STATE_UNKNOWN
done
while IFS= read -r line; do
MAC=`echo $line | awk -F, '{ print $1 }'`
APGROUP=`echo $line | awk -F, '{ print $2 }'`
APNAME=`echo $line | awk -F, '{ print $3 }'`
DESC=`echo $line | awk -F, '{ print $4 }'`
ADDED=`echo $line | awk -F, '{ print $5 }'`
MACCHECK=`echo $MAC | sed -n "/^\([0-9A-Fa-f][0-9A-Fa-f]:\)\{5\}[0-9A-Fa-f][0-9A-Fa-f]$/p"`
SKIP=0
# Check to see if entry has been processed, has the right fields and if the MAC is formatted properly.
# If any check fails, the entry will be skipped and a notation written to the output file.
if [[ -n $ADDED ]]; then
echo "Entry already processed. Skipping $line." >> $OUTFILE
SKIP=1
elif [[ -z $DESC ]] && [[ $SKIP == 0 ]]; then
echo "Entry must have a Description. Skipping $line." >> $OUTFILE
SKIP=1
elif [[ -z $APNAME ]] && [[ $SKIP == 0 ]]; then
echo "Entry must have an AP Name. Skipping $line." >> $OUTFILE
SKIP=1
elif [[ -z $APGROUP ]] && [[ $SKIP == 0 ]]; then
echo "Entry must have an AP Group. Skipping $line." >> $OUTFILE
SKIP=1
elif [[ -z $MAC ]] && [[ $SKIP == 0 ]]; then
echo "Entry must have a MAC Address. Skipping $line." >> $OUTFILE
SKIP=1
elif [ -z $MACCHECK ] && [ $SKIP == 0 ]; then
echo "Incorrect format for MAC Address. Skipping $line." >> $OUTFILE
SKIP=1
fi
# If no conditions set SKIP=1, then set variable cmd to equal the command to send to the controller.
if [ $SKIP == 0 ]; then
cmd=`echo "local-userdb-ap add mac " $MAC " ap-group " $APGROUP " ap-name " $APNAME " description " $DESC " full-name rap-user "`
# Initiate expect session
# Uncomment EXEC=$(expect -d -c " line and comment out EXEC=$(expect -c " line to enable debug output to screen."
# EXEC=$(expect -d -c "
EXEC=$(expect -c "
unset -nocomplain ::env(SSH_AUTH_SOCK);
stty echo
set timeout 60
spawn ssh -o StrictHostKeyChecking=no $USER@$HOST
# Look for password prompt and send login password
expect \"*?assword:\"
send -- \"$PASSWORD1\\r\"
send -- \"\\r\"
# Look for prompt that indicates that we have successfully logged in
expect \"$PROMPT1\"
send -- \"en\\r\"
# Look for enable password prompt and send enable password
expect \"*?assword:\"
send -- \"$PASSWORD2\\r\"
send -- \"\\r\"
# Look for prompt with "#" indicating that we have successfully entered enable mode and send command to add entry
expect -re \"$PROMPT2\"
send -- \"$cmd\\r\"
send -- \"\\r\"
# Look for prompt with "#" indicating that the controller has returned to the prompt after executing the command and
# send command to save the configuration.
expect -re \"$PROMPT2\"
send -- \"write mem\\r\"
# Look for prompt with "#" indicating that the controller has returned to the prompt after executing the command and
# send logout command
expect -re \"$PROMPT2\"
send -- \"exit\\r\"
")
# Write to the output file that the entry has been processed
echo "Processed $line." >> $OUTFILE
# Process the entry line to escape any special characters and then update rap.csv by appending ",ADDED" to the entry.
safe_line=$(printf "%s\n" "$line" | sed 's/[][\.*^$(){}?+|/]/\\&/g')
safe_replace=$(printf "%s\n" "$line" | sed 's/[\&/]/\\&/g; s/$$/\\&/; s/^^/\\&/; s/[0-9].*/&\,ADDED/')
sed -i "s/${safe_line}/${safe_replace}/" $RAPCSV
fi
done < $RAPCSV
exit 0
Last edited by methyl; 06-27-2012 at 04:08 PM..
Reason: please use code tags
Hi All,
Using Expect script when I run it manually it works. But when I put the entry in crontab, the job is still running after 15 hours. The script was created as root. I don't think it's a permission issue. Any idea?
This is what I have under root crontab...
00 18 * * 1-5... (4 Replies)
Hi All,
I have an expect script called sftp to transfer using SFTP below :
# more sftp
#!/usr/local/bin/expect
# Initialisation
set authFile "/home/ap1030/transfer/.password"
# Check the authorisation file exists
if {!} { ;# Does file exist
send_user "$authFile does not exist;... (6 Replies)
Hi All,
I am using a expect script to run a shell script on remote server, the code is as follows. But the problem is that it executes only first command, and hangs it doesn't run the next commands.
spawn ssh $uid@$host
expect "password:"
send "$password\r"
expect "*\r"
send... (2 Replies)
I have the following expect script sitting on a Linux box.
===
#!/usr/bin/expect -f
#
# backup.expect
#
# Expect script to backup a firewall via a SSH session
#
#
set firewall
set username
set password
set prompt
set filename
match_max 50000
spawn ssh -l... (2 Replies)
Hi all,
I have a bit of a vexing issue here and I'm not certain how best to go about it. Basically, I want to run a shell script and automate the user prompt of hitting 1 to fully uninstall Symantec Anti-Virus for OS X.
Would expect be the best way to do this? (5 Replies)
Not able to execute the file in remote host using except utility
I am automating the SFTP keys setp process:
So i created the expect script for controlling the output of shell
below is my main code:
Code:
#!/usr/bin/expect
set fd
set password
close $fd
set df
set app
close $df... (1 Reply)
Hi All,
I am testing expect script in command prompt without issue, but in crontab it is not working, i check the output error as below:
#cat /var/log/testexp.log
spawn minicom -C /var/log/minicom1.log
No cursor motion capability (cm)
AT+COPS=?
I am new in scripting, together... (1 Reply)
Hello all,
I'm trying to write a script to gather and send data and it works just fine at the bash command line, but when executing from CRON, it does not run properly.
My scripting skills are pretty limited and there's probably a better way, but as I said it works at the command line, but... (12 Replies)
Hello, first time here. I have a script that seems to ignore the if statement when run from the cron. I am using Ubuntu 12.10
#!/bin/bash
DOWN=/usr/sbin/dcon
UP="pon dsl-provider"
LOG=/var/log/dsl-reconnect.log
RECV=`ifconfig 2>&1|grep ppp0|cut -d , -f 5|cut -d " " -f 1`
if ]
then... (1 Reply)