Cannot get this bash/expect script to run under a crontab


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Cannot get this bash/expect script to run under a crontab
# 1  
Old 06-27-2012
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
# 2  
Old 06-27-2012
Please post what Operating System and version you are running, the line from crontab, any error messages found in the unix mail for the owner of the crontab, and any evidence you have of the failure which might give folks a clue what to look for.

First impression is that running an interactive process which requires a terminal is not going to work when there is not a terminal context (i.e. in cron). The error messages should support this.

Last edited by methyl; 06-27-2012 at 04:14 PM.. Reason: spellin
This User Gave Thanks to methyl For This Post:
# 3  
Old 06-27-2012
as half the portion of the script is bash there are no errors. The cron job is running under a normal user as i am not the admin of the box. The script by itself works just fine.

it is
Code:
Red Hat Enterprise Linux ES release 4 (Nahant Update 6)


Code:
*/5 * * * * /ftp/office_in_a_box/rap.sh


Last edited by methyl; 06-27-2012 at 06:07 PM.. Reason: please use code tags
# 4  
Old 06-27-2012
Whats your OS?

is your user in cron.allow?
Code:
cat /var/adm/cron/cron.allow

---------- Post updated at 03:54 PM ---------- Previous update was at 03:53 PM ----------

Also can you post the output of the error?
This User Gave Thanks to vpundit For This Post:
# 5  
Old 06-27-2012
Imho. There should be error messages in the unix server mail for the owner of the cron.

Anyway, this process looks like it should be using a Remote Shell not an Interactive Shell.

Though the script is particularly difficult to follow, it seems to just execute a write mem command on the remote system, store the results to a file and then parse the results.

Last edited by methyl; 06-27-2012 at 06:10 PM..
This User Gave Thanks to methyl For This Post:
# 6  
Old 06-27-2012
thanks guys but i found my stupid mistake it was the stty statement in the expect script Smilie
This User Gave Thanks to mrkool For This Post:
# 7  
Old 06-27-2012
For the benfit of those following this thread, where did you find the error message "not a terminal" or whatever?
This User Gave Thanks to methyl For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script acting funny when run from Crontab

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)
Discussion started by: mkoster
1 Replies

2. Shell Programming and Scripting

Script fails to run properly when run from CRONTAB

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)
Discussion started by: rusman
12 Replies

3. Shell Programming and Scripting

Expect script not working in crontab with minicom

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)
Discussion started by: elingtey
1 Replies

4. UNIX for Advanced & Expert Users

Unable to run the script on remote machine Using Expect script

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)
Discussion started by: Manoj Bajpai
1 Replies

5. Shell Programming and Scripting

Use expect to run an interactive shell script?

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)
Discussion started by: prometheon123
5 Replies

6. Shell Programming and Scripting

Expect Script Not working with Crontab

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)
Discussion started by: alagondar
2 Replies

7. Programming

Expect script to run a Shell script on remote server

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)
Discussion started by: yashwanthsn
2 Replies

8. Shell Programming and Scripting

Expect script on crontab

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)
Discussion started by: ap1030
6 Replies

9. Shell Programming and Scripting

how to run shell script inside expect script?

I have the code like this : shell script continues ... .... expect -c" spawn telnet $ip expect "login:" send \"$usrname\r\" expect "Password:" send \"$passwd\r\" expect "*\>" send \"$cmdstr\r\" ... (1 Reply)
Discussion started by: robbiezr
1 Replies

10. Shell Programming and Scripting

Expect script doesn't work under crontab

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)
Discussion started by: samnyc
4 Replies
Login or Register to Ask a Question