Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

automate telnet - how to

Status
Not open for further replies.

minus0

Programmer
Feb 20, 2003
73
US
I was wondering if its possible to TELNET to a unix box from another using a shell script. I tried FTP'ing some files and it did work but wasnt able to TELNET.

Appreciate any ideas

-0
 
There should be no problem with doing a telnet from within a script. We do it all the time to re-direct a given user to a different system. Are you trying to run a command on the target system? Are you wanting to automatically telnet over there without stopping at another login prompt?
 
motoslide,

Two parts to my requirement.
part1:

I usually FTP some files over to a unix box in another environment (this is automated) and then

part2:
telnet manually to that box
* create some links (symbolic),
*archive the logs for the previous build

Its part2 I am trying to automate and posted the question about doing a TELNET from within a script.

Thanks

-0
 
You may consider expect.
Another way is rsh or ssh.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
PH,

I am aware of expect but unfortunately my SA says expect can't be installed and since I am in a development environment rsh/ssh is not an option either. Thanks for your suggestions though.

-0
 
If you are able to establish user equivalency, you should be able to run a remote command. The exact command differs on different flavors of UNIX (remsh, rcmd, etc.).

Or, you can create a user on the remote system with some commands (or a script) in their login script ($HOME/.profile in a bourne shell) which will accomplish your goals. You'll still have to log onto that box, but that would be all.

It sounds like you need to discuss these options further with your Systems Admin. He/She might not allow these options due to some policy issues.
 
you can use a 'poor man' non-interactive telnet - your mileage may vary though....
Code:
    (
    sleep 3
    print "userName"
    sleep 1
    print "userPassword"
    sleep 2
    print "ls ~"
    sleep 1
    print "exit"
    sleep 3
    ) | telnet myHost

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
vlad, I'm afraid that most of the telnet programs don't read their stdin as the keyboard ...

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
minus0, I don't understand why a development box can't use remote shell ...

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Okay. The choices are pretty limited.
Using telnet or any of the 'r' services is bad
mojo on any network where there is an outside
chance of security ever becoming an issue.

Personally I would code it myself. It would not
be very hard to do the right thing that way.
client-server and your own trivial protocol.
Dozens of libraries and methods to perform
encryption and obfuscation of your data on the
wire.

 
Code:
         [ $# -lt 1 ] && set -- 'ifconfig -a'     

         user=USERLOGIN
         passwd=PASSWORD    
         host=HOSTNAME

         (
             sleep 2     
             echo "$user";   sleep 2
             echo "$passwd"; sleep 2
             echo "$*"   
             sleep 2     
         ) | telnet "$host"

This is what I use.
 
On linux, you may create a .netrc - file in your home.
Insert a section:
Code:
machine  <name as in /etc/hosts>
    login <username>
    passwort <password>
and chmod it -rw-------.
This automates logins via ssh without prompting for login and password.
Of course you shouldn't insert clearnames, if you don't trust root - but that's true for the other scripts too.

I hadn't tested it with telnet, because - well - nobody uses telnet anymore...

seeking a job as java-programmer in Berlin:
 
And if you use a .netrc file a security audit will find it as a negative.
 
kHz,

I am sorry I couldnt test if the script you suggested would work until today - well I used the script, I see myself on the other box but thats about it - I can't execute any commands, the connection gets closed and I am back on box1. I have couple questions, in the beginning of your script looks like you have an if condition [ $# -lt 1 ] && set -- 'ifconfig -a' what are you trying to do here and after entering the password there is an echo "$*" - what is this doing?
 
The [ $# -lt 1 ] && set -- ### this says if the command line arguments are less than 1, then set the argument to 'ifconfig -a'

The '$*' is running all arguments.

If you are trying to enter any commands, you cannot. That is what the set is for in the script or command line on the server you are running it from.
 
You can put whatever command you want. I used 'ifconfig -a' as an example.
 
You should try an get ssh keys set up. Make scripting across hosts easier (and you could use sftp / scp etc)
eg: ssh user@host "ln -s /a/b/c /d" without the password.

Do you have Python on the server? (if so, use pexpect instead of getting Admin to install Expect)


To automate the normal FTP Portion:
#!/usr/bin/env ksh
FTPSERVER=xxx
FTPUSER=john
FTPPW=secret
ftp -i -v -t -n $FTPSERVER <<-EOF>> $LINXLOGS/${DAY}_$FILE.log
user $FTPUSER $FTPPW
bin
hash
cd $FTPDIR
lcd $DIR
put $FILENAME
bye
EOF




&quot;If you always do what you've always done, you will always be where you've always been.&quot;
 
I also tried vgersh99's idea but used echo's instead

#!/usr/bin/env ksh
( echo open host2
sleep 5
echo myuser
sleep 2
echo mypass
sleep 2
echo oslevel
sleep 2
echo some more output, etc. ) | telnet

Worked on AIX and Linux

&quot;If you always do what you've always done, you will always be where you've always been.&quot;
 
All,

Thanks a bunch for your help and I am sorry it took me this long to get back but I was away on vacation.

This is what I am doing now

#!/bin/ksh

user=$UserName
passwd=$PassWord
host=$Host
(
sleep 2
echo "$user"; sleep 2
echo "$passwd"; sleep 2
# echo whatever command to be performed on the box
# for example list the files
echo ls -l
sleep 1
) | telnet "$host"

To overcome the security issues, I am passing the hostname, user and password as parameters.

Thanks again
-0
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top