LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   another script for telnet connection (https://www.linuxquestions.org/questions/linux-newbie-8/another-script-for-telnet-connection-4175474897/)

socalheel 08-27-2013 11:51 AM

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 


szboardstretcher 08-27-2013 12:10 PM

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.

cbtshare 08-27-2013 12:40 PM

er*

cbtshare 08-27-2013 01:29 PM

Expect is built for this and can handle the input/output plus timeouts etc....sys admin , is your school teacher?

socalheel 08-27-2013 02:47 PM

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.

szboardstretcher 08-27-2013 02:55 PM

I've given you a simple example and explanation of how to do this. Is there a problem?

carlitos_30 08-27-2013 03:11 PM

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

socalheel 08-27-2013 03:13 PM

Quote:

Originally Posted by szboardstretcher (Post 5017022)
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.

jpollard 08-27-2013 05:57 PM

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.

socalheel 08-28-2013 09:03 AM

Quote:

Originally Posted by szboardstretcher (Post 5016947)
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.

socalheel 08-28-2013 01:23 PM

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



All times are GMT -5. The time now is 12:36 PM.