Operating System - Linux
1753556 Members
5471 Online
108796 Solutions
New Discussion юеВ

using telnet command in shell script

 
ramatulasi
New Member

using telnet command in shell script

Hi every body,

I need to connect to remote server, automatically using cron tab in Linux . For that i would like to write a shell script.

echo `telnet `
when i isuue this command , how can i supply username and password to this shell script.

Can any one please could help me in this same
above.

Thanks in Advance.

Rama




Rama
10 REPLIES 10
Goran Koruga
Honored Contributor

Re: using telnet command in shell script

Hi.

You need to use expect.
SOmething like this:

#!/usr/local/bin/expect
set username
set password

spawn telnet

expect {
-re "\[L|l\]ogin:" {
if { $mode != "PasswordSent" } {
set timeout 1
set mode SendLogin
exp_continue
}
}
-re ".*Password:" {
set timeout 1
set mode SendPassword
exp_continue
}
}
timeout {
switch $mode {

SendLogin {
send $username
send \n
exp_continue
}
SendPassword {
send $password
send \n

set timeout 5
exp_continue
}
}
}
}

catch { interact }

The above probably won't work just like that and it might have some errors as well. It should give you clues as what to do.

But if you're using telnet and are not worried about passwords, why not use rsh instead ? Or even ssh + RSA/DSA authentication ?

G.
John Meissner
Esteemed Contributor

Re: using telnet command in shell script

Use expect to do this ... install it from you distributions RPM's and then type:
autoexpect -f telnet hostname
#where is replace with the name you want to call the script. Then when you get your login prompt ocntinue doing whatever you want the script to do.

After the script is created you will need to edit it and remove a few lines out (such as date/time) if you don't the script won't work. I'm attaching a sample scpect script of mine used to launch stm.

All paths lead to destiny
Zafar A. Mohammed_1
Trusted Contributor

Re: using telnet command in shell script

You can also do with Perl, just install the Telnet Module.

Thanks
Zafar
Chris Vail
Honored Contributor

Re: using telnet command in shell script

I really, really don't recommend that you do this. It is NOT secure. Telnet itself is not secure (passwords are sent in cleartext) and hard-coding a password into a shell script is always a bad idea. Doing so lets anyone with read access to the script find out what your password is.

I recommend that you use secure shell instead. It is supported in almost every version of Linux. There is nothing you can do in telnet that you also can't do with the secure utilities.

I have attached a document on installing and using secure shell.


Chris
Shannon Petry
Honored Contributor

Re: using telnet command in shell script

If this is testing connectivity only, you could use ICMP. If this is to execute remote commands, use ssh or rsh.

I dont recommend opening a telnet session from a script.

Regards,
Shannon
Microsoft. When do you want a virus today?
Michael Steele_2
Honored Contributor

Re: using telnet command in shell script

While it use to be common for ftp to use the .netrc file for automatic logins the security minded have strongly encouraged against it.

Even with the limited ftp command set, this is not recommended.

If you try to automate 'telnet' you can try using the .netrc file but telnet has no limited ftp command set. In short, you're asking for trouble.

Here is one example of the .netrc login. You'd create a unique account designed for automatic logins only and add .netrc here. Here is an example; I've only tested it with ftp:

machine hpxdzg login guest password secret

http://docs.hp.com/cgi-bin/fsearch/framedisplay?top=/hpux/onlinedocs/B2355-90696/B2355-90696_top.html&con=/hpux/onlinedocs/B2355-90696/00/00/73-con.html&toc=/hpux/onlinedocs/B2355-90696/00/00/73-toc.html&searchterms=.netrc&queryid=20030414-134437

By the way, your password will be viewable to anyone over the network with tcpdump or ethereal, et al., since it's not encrypted and sent over the LAN in plain text. This is the main reason people use SSH.
Support Fatherhood - Stop Family Law
Quinat Romain
Frequent Advisor

Re: using telnet command in shell script

Hello,
If you want simply use a script you can do that. But it's totally unsecure.

In a file (name ToDo) put all commands you want to pass in the telnet session.


...



In an other file "ScriptFile"
telnet @IP < ToDo

And execute ScriptFile

It works in a lot of case.
ramatulasi
New Member

Re: using telnet command in shell script

Hi,

Thank You so much for your valuable suggestions.

when i use expect i am getting error saying bad interpretor.what does it means.

i am running it in Linux apache web server

can you help me in that.

Thanks in Advance.

Rama
Rama
ramatulasi
New Member

Re: using telnet command in shell script

Hi every one,

Thank You so much for your valuable suggestions.

when i use expect i am getting error saying bad interpretor.what does it means.

i am running it in Linux apache web server

can you help me in that.

Thanks in Advance.

Rama
Rama