LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-27-2013, 11:51 AM   #1
socalheel
Member
 
Registered: Oct 2012
Location: Raleigh, NC
Distribution: CentOS / RHEL
Posts: 135

Rep: Reputation: 3
another script for telnet connection


first thing i have to say, i cannot use the following:

expect
netcat
nmap

this has to be in bash and has to be in telnet.

my senior admin asked me to come up with a script to telnet to a server on port 110, actually log in, be able to list, and then log out.

if i am able to do those things, then awesome, no action needed.

if, however, i am unable to establish a connection, i need to be notified via email no connection established.

i have the log in portion of the script developed and it works, but i do not know how to finish the script if the connection cannot be established.

any input and help is appreciated.

here is the first part of my script


PHP Code:
#!/bin/bash

(
echo 
open 123.123.123.123 110
sleep 2
echo "user username"
sleep 1
echo "pass password"
sleep 4
echo "list"
sleep 4
echo "quit"
sleep 1
) | telnet 
 
Old 08-27-2013, 12:10 PM   #2
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,204

Rep: Reputation: 1611Reputation: 1611Reputation: 1611Reputation: 1611Reputation: 1611Reputation: 1611Reputation: 1611Reputation: 1611Reputation: 1611Reputation: 1611Reputation: 1611
You could make a seperate check, before going on with the log in, such as:

Code:
telnet 127.0.0.1 > /dev/null 2>&1 || echo "connection refused, so i wont bother logging in"
reason being, is that in your script, you are calling interactive telnet and it is working. the exit code says 'yep, i opened telnet and closed telnet correctly.' It doesn't care that a command inside of it didn't work. just as if you called interactive python and typed 'woijeoijewe;fwojoa;wejfoijeijei' then exited, python would exit with a clean code, because what you did in the interpreter doesn't matter.

Last edited by szboardstretcher; 08-27-2013 at 12:13 PM.
 
1 members found this post helpful.
Old 08-27-2013, 12:40 PM   #3
cbtshare
Member
 
Registered: Jul 2009
Posts: 619

Rep: Reputation: 42
er*

Last edited by cbtshare; 08-27-2013 at 01:23 PM.
 
Old 08-27-2013, 01:29 PM   #4
cbtshare
Member
 
Registered: Jul 2009
Posts: 619

Rep: Reputation: 42
Expect is built for this and can handle the input/output plus timeouts etc....sys admin , is your school teacher?
 
Old 08-27-2013, 02:47 PM   #5
socalheel
Member
 
Registered: Oct 2012
Location: Raleigh, NC
Distribution: CentOS / RHEL
Posts: 135

Original Poster
Rep: Reputation: 3
no, sys admin is not my school teacher, i work with him and i'm not sure if he's giving me this to help me with scripting or if there is another reason.
 
Old 08-27-2013, 02:55 PM   #6
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,204

Rep: Reputation: 1611Reputation: 1611Reputation: 1611Reputation: 1611Reputation: 1611Reputation: 1611Reputation: 1611Reputation: 1611Reputation: 1611Reputation: 1611Reputation: 1611
I've given you a simple example and explanation of how to do this. Is there a problem?
 
Old 08-27-2013, 03:11 PM   #7
carlitos_30
LQ Newbie
 
Registered: Aug 2013
Posts: 9

Rep: Reputation: Disabled
Of course you can go that way, but that script is very weak. If you are not going to automate a lot of things using telnet is the way to go.

Now if you want something more powerful, Perl has a nice Telnet Module.

http://search.cpan.org/~jrogers/Net-.../Net/Telnet.pm
 
Old 08-27-2013, 03:13 PM   #8
socalheel
Member
 
Registered: Oct 2012
Location: Raleigh, NC
Distribution: CentOS / RHEL
Posts: 135

Original Poster
Rep: Reputation: 3
Quote:
Originally Posted by szboardstretcher View Post
I've given you a simple example and explanation of how to do this. Is there a problem?
not yet ... i'm working on it right now to fully understand it, which right now i don't.

and how to implement it in a non interactive script.
 
Old 08-27-2013, 05:57 PM   #9
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,696

Rep: Reputation: 1261Reputation: 1261Reputation: 1261Reputation: 1261Reputation: 1261Reputation: 1261Reputation: 1261Reputation: 1261Reputation: 1261
In general, using telent to handle passwords is NOT secure. There is no encryption used so anyone sniffing the network has the password...

ssh (with RSA authentication) can do this without a problem, without even needing a password, and be more secure at the same time.
 
Old 08-28-2013, 09:03 AM   #10
socalheel
Member
 
Registered: Oct 2012
Location: Raleigh, NC
Distribution: CentOS / RHEL
Posts: 135

Original Poster
Rep: Reputation: 3
Quote:
Originally Posted by szboardstretcher View Post
You could make a seperate check, before going on with the log in, such as:

Code:
telnet 127.0.0.1 > /dev/null 2>&1 || echo "connection refused, so i wont bother logging in"
reason being, is that in your script, you are calling interactive telnet and it is working. the exit code says 'yep, i opened telnet and closed telnet correctly.' It doesn't care that a command inside of it didn't work. just as if you called interactive python and typed 'woijeoijewe;fwojoa;wejfoijeijei' then exited, python would exit with a clean code, because what you did in the interpreter doesn't matter.
thanks for the help on this. and i now see what you mean about what the exit code is reporting ... it only cares if i open telnet IP PORT and does not care if i can log in and run a command. i started my script basing on the exit code which is not going to give me what i want.

i am now going to focus on this method and i think it will give me what i need:

Initiates the login session to port 110, using netcat and an input file that will have the log in credentials
Capture the output to a file
Grep the output file for Authentication failed
IF
Authentication failed exists = notify
Authentication does not exist = no action needed

once i get it developed and working i'll post it up for your input.
 
Old 08-28-2013, 01:23 PM   #11
socalheel
Member
 
Registered: Oct 2012
Location: Raleigh, NC
Distribution: CentOS / RHEL
Posts: 135

Original Poster
Rep: Reputation: 3
seems so simple now but man this hurt my head.

Code:
#!/bin/bash

nc 123.123.123.123 110 < credentials.txt >> output.txt


grep "Authentication fail" output.txt
if [ $? = 0 ]
then
echo -e "Unable to netcat to 123.123.123.123 from $HOSTNAME" | mail -s "Authentication error on $HOSTNAME" whomever@wherever.com

rm -rf output.txt
fi
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
telnet -> use contrl-D to exit using python telnet script athreyavc Programming 5 07-06-2009 11:17 PM
Running Telnet in a script, want to save Telnet output, howtodothis??? anil3 Linux - Software 2 03-08-2006 04:01 PM
telnet connection refused turbo_acura Linux - Networking 8 11-21-2004 09:07 AM
Telnet connection refused naveenpn Linux - Networking 2 02-17-2004 07:16 AM
disallow telnet connection mikeshn Linux - General 1 11-04-2002 11:51 AM


All times are GMT -5. The time now is 04:37 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration