LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 11-10-2008, 01:32 AM   #1
athreyavc
Member
 
Registered: May 2004
Location: bangalore
Distribution: Cent OS, Ubuntu
Posts: 105

Rep: Reputation: 15
Expect Script not getting called in the Cron Job


Hi All,

I have written a script that telnets 100 servers and gets a command output. I have written an "Expect" Script that does the telent functioning. Shell script calls the expect script to telnet each server

The script runs with No issues when executed manually.

But, when I run the script via Cron, the expect script is not getting called.

Like this I am calling the Script

/opt/sfw/bin/expect telExpect.exp $server $Username $Password

While googling I found someone saying mention the SHELL variable so that the expect script should determine the shell where it should run.

So I changed the script like this

SHELL=/opt/sfw/bin/expect

telExpect.exp $server $Username $Password

But, the expect script is still not getting called while running from the CRON.

Please please help me on this.

Regards,

Athreya
 
Old 11-10-2008, 02:17 AM   #2
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,287

Rep: Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034
Is that the entire script in cron?
Where are $server, $Username, $Password getting set?
In any case, the default env in cron is minimal, so ALWAYS specify the complete (absolute) path to ALL cmds and files.
Something like

/opt/sfw/bin/expect /path/to/script/telExpect.exp $server $Username $Password
 
Old 11-10-2008, 02:59 AM   #3
athreyavc
Member
 
Registered: May 2004
Location: bangalore
Distribution: Cent OS, Ubuntu
Posts: 105

Original Poster
Rep: Reputation: 15
Hi Chrism01 ,

Thanks for the quick reply. The variables are in the Shell Script

I call them in the expect Script like this

#!/usr/bin/expect -f
set timeout -1
spawn $env(SHELL)
match_max 100000

expect "#"

send -- "telnet [lrange $argv 0 0]\r"

expect "login:"

send -- "[lrange $argv 1 1]\r"

expect "*:"

send -- "[lrange $argv 2 2]\r"


expect "*]$ "

send -- "history\r"

expect "*]$ "

send -- "exit"

And the shell script has complete path of everything,

#!/bin/bash

# Setiing the Date Varibale

DATE=`date +%d-%m-%Y`

#Make the Result File Null
> /export/home/admin/result.txt
rm /export/home/admin/op*
> /export/home/admin/up
> /export/home/admin/down

# Reading every line in the servers.conf file
cat /home/servers.conf | while read line
do
server=`echo $line | cut -d ':' -f1` # Server name variable
echo "The Server Name" >> result.txt
echo $server >> /export/home/admin/result.txt # Echo the server name to the result filer
echo "-------------------------" >> result.txt

Username=`echo $line | cut -d ':' -f2` # User name
Password=`echo $line | cut -d ':' -f3` # Password for the user

ping -c 1 $server &> /dev/null # Ping the server to check the availability

if [ "$?" -eq 1 ];then # Check the availability of the Server

echo "$server was not pinging!!Please check" >> /export/home/admin/PingFail.txt

if [ "$?" -eq 2 ]; then

echo " " >> /export/home/admin/WrongIPFormat.txt
else
/opt/sfw/bin/expect /home/admin/telExpect.exp $server $Username $Password # Expect Script Called
fi
fi

done >> /export/home/admin/result.txt

#This section We are filering the servers which have Down links
awk '/The server Name/ {n++} {print > f n}' f=/export/home/admin/op /export/home/admin/result.txt;
for i in `ls -lrt /export/home/admin/ | awk '{print $9}' | grep op`;
do
grep down $i

if [ $? -eq 0 ]

then

echo $i >> /export/home/admin/down

else

echo $i >> /export/home/admin/up

fi

done;

cat /export/home/admin/down | while read line

do

cat $line >> /export/home/admin/result$DATE.txt

done

rm /export/home/admin/op*

Regards,

Athreya
 
Old 11-10-2008, 06:35 AM   #4
athreyavc
Member
 
Registered: May 2004
Location: bangalore
Distribution: Cent OS, Ubuntu
Posts: 105

Original Poster
Rep: Reputation: 15
Hi Chrism01 ,

I solved the issue. It took me lot of googling around.

Only 2 things I should have mentioned in the Shell Script.

The user id from which I am running the script his shell was set to csh.

I set the same in the script

export SHELL=/bin/csh
export TERM=vt100

This did the trick for me.

Please consider this Case as CLOSED.

Thanks for the help.
 
  


Reply

Tags
closed


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
adding a perl script to cron.daily / cron.d to setup a cron job CrontabNewBIE Linux - Software 6 01-14-2008 08:16 AM
can expect be called from within a bash script? johnpaulodonnell Programming 4 06-21-2007 09:42 AM
Bash script and cron job rust8y Linux - General 2 07-08-2006 07:45 AM
Cron Job with a Script bravored Linux - General 4 08-05-2005 10:27 AM
cron job for expect script ruchitadesai Programming 9 05-27-2005 04:34 AM


All times are GMT -5. The time now is 01:51 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration