Click to See Complete Forum and Search --> : Automate telnet commands?
Hydro
01-17-2001, 10:49 AM
At present I have to connect through a telnet session and run a series of (static) commands every hour. I want to automate this so I only have to run a script.
So, I was wondering if it is possible to echo keystrokes to a telnet through a shell script or a C program?
For example, I want to be able to log in to the telnet session, enter 1, then enter 5, then enter 6 and then finally log out.
Is this possible?
Thanks.
YaRness
01-17-2001, 11:29 AM
here's a perl module for connecting and doing stuff with telnet: http://search.cpan.org/doc/JROGERS/Net-Telnet-3.02/lib/Net/Telnet.pm
i'm sure it can be done with several other languages as well. do you want to try and do this yourself, or you just looking for someone to hand you an out-of-the-box answer? because if you can program, it doesnt look like it'd be hard to implement this module, or something similar in another language.
------------------
"Assembly of Japanese bicycle require great peace of mind."
Registered Linux User #188285 http://counter.li.org/
Hydro
01-17-2001, 11:31 AM
I want to do it myself - Just looking for a few pointers as to the best way to do it.
Never used Perl so I guess that would be ideal.
Thanks.
YaRness
01-17-2001, 11:54 AM
no problem. good luck.
------------------
"Assembly of Japanese bicycle require great peace of mind."
Registered Linux User #188285 http://counter.li.org/
Muzzafarath
01-17-2001, 01:26 PM
Would it be possible to put the commands to send in a file and then redirect telnet's stdin to that file?
commands contains (for example):
my_username
my_password
command1
command2
logout
And then when you start telnet:
telnet hostname.com 666 < commands
Muzzafarath
01-17-2001, 01:30 PM
Another idea: why not set up cron so it'll execute a script containing the commands you want to execute (on the server of course)?
YaRness
01-17-2001, 02:47 PM
Originally posted by Muzzafarath:
Would it be possible to put the commands to send in a file and then redirect telnet's stdin to that file?
commands contains (for example):
my_username
my_password
command1
command2
logout
And then when you start telnet:
telnet hostname.com 666 < commands
it might just dump all that into the telnet port before the other computer is even ready to receive the username, let alone the password or commands.
------------------
"Assembly of Japanese bicycle require great peace of mind."
Registered Linux User #188285 http://counter.li.org/
Strike
01-17-2001, 03:21 PM
YaRness is right - that's why expect was created. I think the homepage is something like http://expect.nist.gov so I'll check.
----edit----
yeah, the link above is right
[This message has been edited by Strike (edited 17 January 2001).]
YaRness
01-17-2001, 03:39 PM
damn, that's a neat tool.
i also discovered cURL today, from looking at Gotmail (from some post in General Linux Questions). it's knowing about little stuff like this that can like get you hired and stuff.
potential employer: "i have to go in and change all the links from this list of stuff i download from a site that i log into etc. etc."
you, the ubergeek: "ummm, i can make all that point-and-click for you."
pe: "WOW! here, have a job"
------------------
"Assembly of Japanese bicycle require great peace of mind."
Registered Linux User #188285 http://counter.li.org/
James Bond
01-17-2001, 05:30 PM
What about just writing a shell script with all the commands you want to run (assuming that your user can run them), or, better yet, create an account that, in the .bash_profile (i think, please correct me if I am wrong) you put all of the commands you wish to execute on login.
Or, which is (IMHO) a much better idea is to use the cron and write a shell script like this
#!/usr/bin/sh
/path1/command1
/path2/command2 -options
Then just add into the cron the path of the shell script you just wrote.
-JB
Sterling
01-17-2001, 07:06 PM
Yeah, a cron job on the server works. Programming something to act as a telnet client is really simple too - you just read text the server sends you from the socket, and write anything to go to the server into the socket.
------------------
-Sterling
"There is no Linuxnewbie.org cabal..."