1833832 Members
1922 Online
110063 Solutions
New Discussion

Telnet through script

 
SOLVED
Go to solution
jayachandran.g
Regular Advisor

Telnet through script

Hi All

I want to write a unix script to login to a remote host and to collect logs.

But once telnet session is started then my script is not running.. once the telnet is closed my script runs.


please help to write a script so that I would be able to fire a telnet command through script.

Thanks and Regards
Jayachandran,G

8 REPLIES 8
Ralph Grothe
Honored Contributor

Re: Telnet through script

google for Expect
or use Perl Net::Telnet,
or even the Perl Expect if you don't like tcl
Madness, thy name is system administration
Peter Godron
Honored Contributor

Re: Telnet through script

Hi,
as an alternative to setting up a telnet session via expect, have you thought about remsh or just collecting the logs via ftp.
jayachandran.g
Regular Advisor

Re: Telnet through script

Hi

To add some more details..

I'm trying from solaris server.....

Peter

both cmd execution and ftp log file collection must be done in order to reach my goal.


Once I have used normal telnet commands script with some sleep option.....

Marvin Strong
Honored Contributor

Re: Telnet through script

Personally I would just setup ssh keys. and use scp. No scripted passwords, or anything that way.

Re: Telnet through script

As Marvin has told before, the ssh keys is probably the best in order to copy the files in a secure way.

But as you have to execute a command before I think you will need the expect script so probably it would be better to use it for the full task.

Anyway, be concious that the expect script must contain username and password, so be concious about the security issues it would mean.
Ralph Grothe
Honored Contributor
Solution

Re: Telnet through script

No need to fumble with Expect and/or telnet.
You can easily combine remote command execution and secure copying of files just using ssh
(there's absolutely no need for scp if you need to copy files).
In fact this is how I transfer (in a trusted network) public keys for passwordless logins.

First you need to create a pair of either DSA or RSA keys (probably both won't hurt).
If you have never before initiated an ssh session on your solaris box before then most likely $HOME/.ssh subdir is missing.
You can create it manually but because of the pickiness of ssh with file ownerships and permissions (if strictmodes is enabled, as should be the case) it's better to have ssh it done for you.
Therefore you only need to connect to another ssh host (no login there required), and accept its hostkey (in a hostile environment you should double check the issued fingerprint of the remote host key)
e.g.

$ ssh someuser@somesshhost

If you accepted the hostkey you will find a .ssh dir in your $HOME together with a known_hosts file, all with correct mode bits.

Next generate a pair of keys.

$ ssh-keygen -t rsa -b 1024 -N "" -f ~/.ssh/id_rsa

should generate a passphraseless RSA key pair.
Now on the other Solaris box where you remotely want to execute commands or copy files you need to place the public key of the just generated pair in some user's $HOME/.ssh/authorized_keys file that you have access to.
Let's assume you have root access there (but this isn't necesarry) just to show how to copy files without requiring scp.

$ ssh root@otherbox 'cat >>~otheruser/.ssh/authorized_keys' < ~/.ssh/id_rsa.pub

Similarily, you can recursively copy whole directory trees by just executing ssh commands remotely (in the very simple example above the cat command).
You can also combine several commands by separating them with quoted simicolons.
You only have to be careful to quote any meta character that should be executed on the remote box to prevent your local shell from interpreting it.

Madness, thy name is system administration
jayachandran.g
Regular Advisor

Re: Telnet through script

Hi All

Thankx a lot now I'm using ssh and trying to execute the commands and it really works fine.
jayachandran.g
Regular Advisor

Re: Telnet through script

thankx a lot to all.......