Operating System - HP-UX
1821544 Members
2390 Online
109633 Solutions
New Discussion юеВ

Re: 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
Jeff Murphy
Occasional Advisor

Re: Calling Telnet from a Script called from Cron

Here is script2
OldSchool
Honored Contributor

Re: Calling Telnet from a Script called from Cron

as Tim noted, remsh, rexec, or better yet ssh is the way to go. you simply execute the script you want to run.

telnet is gonna have issues, as you don't have a tty when running via cron.
OldSchool
Honored Contributor

Re: Calling Telnet from a Script called from Cron

or there is always the question "Why don't you run the job out of cron on the target machine?"
James R. Ferguson
Acclaimed Contributor

Re: Calling Telnet from a Script called from Cron

Hi Jeff:

I agree with Tim and OldSchool --- you should look at 'remsh' or 'ssh'. It appears that you are trying to invoke a 'telnet' session simply to provide connectivity to another server and do a simple script involving selecting and renaming some files. This is what 'remsh' or 'ssh' is designed to offer.

Too, in your first script you have:

/usr/ndm/tmp/dir/./Script2.scr

That makes no sense unless it is a posting error.

Regards!

...JRF...
Jeff Murphy
Occasional Advisor

Re: Calling Telnet from a Script called from Cron

The entry to ..././Script2.scr is where the telnet script is located.

If I used something like ssh, is the format the same as the telnet session?
If I was able to call the application directly without using telnet, how can I pass the keystrokes? Can I still use Echo and sleep? Does the list of commands need to be in parenthesis?
James R. Ferguson
Acclaimed Contributor

Re: Calling Telnet from a Script called from Cron

Hi (again) Jeff:

Using 'ssh' or 'remsh' could be as simple as this (example) to list files on a remote server:

# ssh remoteboxt 'ls /home/jrf/*.log|while read FILE;do echo ${FILE};done'

Regards!

...JRF...
Jeff Murphy
Occasional Advisor

Re: Calling Telnet from a Script called from Cron

Hey again James,

Thanks a million! I am light years ahead. Only hurdle I have left is passing the password when make the SSH connection. For some reason the echo command isn't passing it. Any ideas?
James R. Ferguson
Acclaimed Contributor
Solution

Re: Calling Telnet from a Script called from Cron

Hi (again) Jeff:

Setup SSH public keys. See Matti's directions in this thread --- he does a nice job of describing the simple procedure:

http://forums12.itrc.hp.com/service/forums/questionanswer.do?threadId=1254688

I'm not sure what you mean by "...the echo command isn't passing". If you wanted to do:

# ssh remotebox 'ls /home/jrf/*.log|while read FILE;do echo ${FILE} > /var/tmp/results;done'

...then you coult retrieve the results back to your local server with:

# scp remotebox:/var/tmp/results /home/jrf/results

Regards!

...JRF...

Jeff Murphy
Occasional Advisor

Re: Calling Telnet from a Script called from Cron

Thanks James!

I am at 100% success.
When I tried to test, I was getting a password prompt, but took your advice and VOILA!

Again, thanks to everyon's feedback, I was not only able to get my problem solved, but learn a ton in the process.
OldSchool
Honored Contributor

Re: Calling Telnet from a Script called from Cron

"
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
"

"/usr/ndm/tmp/dir/./Script2.scr"
As noted previously, this makes no sense. What do you think the "." represents? This should probably be something like:

/usr/ndm/tmp/dir/Script2.scr
As I noted before, typically this would be set up in the a crontab on the "other" machine. Logging in or rexec'ing or ssh is just more overhead. It is simple enough to check for the existance of files of that name pattern and procede or not based on those results. I can see the utility of machineA:scriptA running scriptB on machineB if scriptB depends upon something that scriptA does, but that isn't clear in this case.

In any case, be aware that it is possible for "file1*.csv" to be different in the two invocations. You might consider storing the result in a variable, and then processing that variable.

What happens if another process adds "file1abc.csv" after the "ls" runs and before/during Script2.scr's execution. The "mv" will see fileabc.csv and move it, but it won't have been processed. Such errors can be a headache to find.


Dennis Handly
Acclaimed Contributor

Re: Calling Telnet from a Script called from Cron

>OldSchool: be aware that it is possible for "file1*.csv" to be different in the two invocations. You might consider storing the result in a variable

Well the variable in question is $file.
ls file1*.csv | while read file; do
...
mv $file /usr/ndm/dir1/Inbound/done/
done

>What happens if another process adds "file1abc.csv" after the "ls" runs

If you use $file above, you'll catch it next time.
OldSchool
Honored Contributor

Re: Calling Telnet from a Script called from Cron

Dennis....nice catch...my bad.

I wasn't paying enough attention to
file*, read ... file, and $file...

On the other hand, the names used don't exactly thrill me either...but thats another story.
OldSchool
Honored Contributor

Re: Calling Telnet from a Script called from Cron

1: cd /usr/ndm/dir1/Inbound
2: ls file1*.csv | while read file
3: do
4: /usr/ndm/tmp/dir/./Script2.scr
5: mv file1*.csv /usr/ndm/dir1/Inbound/done/
6: done

Dennis,

My point was that this is just plain wrong...your fix is indeed correct.

If this is indeed an iterative process (multiple files being processed 1 at a time by the script in line 4) the results won't be as expected, especially if Script2 relys on $file.

In the example presented by the OP, he will set variable "file". Script2 runs and presumably processes $file. The move, as listed, would move the 2nd and subsequent file1*.csv "out from under" the the loop. "file" in line 2 gets set a second time, but by then that file is no longer in the directory and everything falls on its face.

Scott