• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 978
  • Last Modified:

Port checking using the ksh shell

I am after checking ports using telnet in the ksh shell, i need to telnet into the relevant ip and port check if there is connectivity if not display an error.



Thanks for Your Help,
Mike
0
Slightly_Imperfect
Asked:
Slightly_Imperfect
  • 2
2 Solutions
 
liddlerCommented:
Which ports are you checking? all? Just a few?

You are going to have to issue the telnet, and pipe stderr somewhere, like
telnet $ip $port 2>$out

You may want this in a loop for your ports, if there are many and in a second loop if there are lots of ips

for ip in 10.10.0.1 10.0.0.2 10.0.0.2
do
for port in 20 21 22 25 80 110 443
cat /dev/null >/tmp/out
do
telnet $ip $port 2> /tmp/out
fail=`grep Unable /tmp/out`  #bit messey using a tmp file
if [[ -n $fail ]];then
echo "failed"
done
done

However, telnet may hang if it actually connects, so you might want to use kill / pkill to kill off any telnet spwned by this script running after a short time (say 1 second?)

 
0
 
Slightly_ImperfectAuthor Commented:
how would you add the kill part to the actual script ?



Thanks,
Mike
0
 
liddlerCommented:
something like
telnet $ip $port 2> /tmp/out &
sleep 1
pkill telnet

0
 
TintinCommented:
telnet isn't the best tool for this type of job.  You'd be better off with something like netcat or similar.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now