Operating System - HP-UX
1753925 Members
8835 Online
108810 Solutions
New Discussion юеВ

Calling Telnet from a Script called from Cron

 
SOLVED
Go to solution
Jeff Murphy
Occasional Advisor

Calling Telnet from a Script called from Cron

Hey guys,
Scripting is something rare to non-existent in our environment, so I apologize in advance.
I've searched several sites, forums etc, but I'm not able to put the 2 and 2 together on this.

What we have is a script that runs at regular intervals and looks for a file in a folder. When the file is there, we have a While/Do that calls a 2nd script that will telnet into itself, and issue keyboard commands (echo) to log in and process the file.
When fired off manually, the 1st script sees the new file, calls the 2nd script and processes the file perfectly (well it works anyway...LOL).
When we have the execution ran from the cron process, the log (Cron) doesn't show anything of real value. The 1st script files off, calls the 2nd script but I think I'm missing something, since it doesn't actually run. The first script does finish as it moved the file to the "processed" subfolder. I know this is really basic stuff, but any help is greatly appreciated!

Here is the 2nd script (edited for content):

( echo open myself
sleep 2
echo login
sleep 1
echo password
sleep 1
echo more commands
echo log out of application
) | telnet

I know, I know, color me stupid. Having a real brain fart today.

Thanks a million!!!
22 REPLIES 22
Patrick Wallek
Honored Contributor

Re: Calling Telnet from a Script called from Cron

USUALLY when a script runs from the command line, but doesn't from cron the problem is environment related.

You must remember that the environment in cron is very sparse. Your /etc/profile and ~/.profile do NOT get sourced so any PATH or other environment variables you have when logged in are NOT available.

If you are referencing any variables in your scripts, double check and make sure those variables are defined in the script.
Jeff Murphy
Occasional Advisor

Re: Calling Telnet from a Script called from Cron

Thanks Patrick,
I'm purely passing keystrokes to the telnet session. I'm not using any variables (so far).
I keep finding data that points to environment as you described, but what would I actually need?
Tim Nelson
Honored Contributor

Re: Calling Telnet from a Script called from Cron

Without a tty (which you are NOT going to get with a cron job) I am not sure how telnet is going to work.

How about remsh or ssh and supply the commands.


Deepak Kr
Respected Contributor

Re: Calling Telnet from a Script called from Cron

Hi,

Can you tell me how did you set cron entry here...?
"There is always some scope for improvement"
Jeff Murphy
Occasional Advisor

Re: Calling Telnet from a Script called from Cron

Sure thing.
From the contab -l :

0,5,10,15,20,25,30 12-20 * * * /usr/ndm/tmp/dir/./File1.scr > /usr/ndm/tmp/dir/File1.log

The log files are empty.
Jeff Murphy
Occasional Advisor

Re: Calling Telnet from a Script called from Cron

The crontab calls this first script:

cd /usr/ndm/dir1/Inbound
ls file1*.csv | while read file
do
/usr/ndm/tmp/dir/./Script2.scr
mv file1*.csv /usr/ndm/dir1/Inbound/done/
done

Deepak Kr
Respected Contributor

Re: Calling Telnet from a Script called from Cron

Thanks!!

Can you attach both scripts here...
"There is always some scope for improvement"
Jeff Murphy
Occasional Advisor

Re: Calling Telnet from a Script called from Cron

Do you think that it's moving my files before the telnet session can process them?
Jeff Murphy
Occasional Advisor

Re: Calling Telnet from a Script called from Cron

Here is script1