Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 969
  • 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

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

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