ksh and telnet script
am 11.08.2005 17:38:50 von NOYB
I want to create a ksh script that reads in a list of category, ip and
ports, validate that the server is listening on the port, and then
return. We cannot install expect or anything else due to politics so I
want to keep all commands within the shell scripts. Below is the work
in progress. When I run the script, successful connections are fine,
the problem is in the return code on failures, it just hangs. Can
anyone assist? Does anyone have an alternative method that only does not
use expect - (perl is allowed, but without any add on modules). The
server is a Solaris 9 core installation.
virtualips.txt
Success1 xxx.xxx.xxx.xxx Port#
Success2 xxx.xxx.xxx.xxx Port#
Failure 123.123.12.21 1234
virtualiptest.ksh
#!/usr/bin/ksh
# Variables
VIRTUALIPFILE="./virtualips.txt"
# Code Begin
while read LINE
do
echo $LINE
PORTNAME=$(echo $LINE | awk '{print $1}')
IPADDRESS=$(echo $LINE | awk '{print $2}')
PORT=$(echo $LINE | awk '{print $3}')
HOST="$IPADDRESS $PORT" #awk '{print $2 " " $3}' also works
echo $HOST
(echo "^]") | telnet $HOST >> /dev/null
if [ $? == 0 ]
then
echo "Success"
else
echo "Failure"
fi
done < $VIRTUALIPFILE
--
I ordered from "More 4U Books and Music" and was disatisfied with them
- don't let it happen to you.
Re: ksh and telnet script
am 11.08.2005 20:42:54 von Icarus Sparry
On 2005-08-11, NOYB wrote:
> I want to create a ksh script that reads in a list of category, ip and
> ports, validate that the server is listening on the port, and then
> return. We cannot install expect or anything else due to politics so I
> want to keep all commands within the shell scripts. Below is the work
> in progress. When I run the script, successful connections are fine,
> the problem is in the return code on failures, it just hangs. Can
> anyone assist? Does anyone have an alternative method that only does not
> use expect - (perl is allowed, but without any add on modules). The
> server is a Solaris 9 core installation.
>
>
>
> virtualips.txt
> Success1 xxx.xxx.xxx.xxx Port#
> Success2 xxx.xxx.xxx.xxx Port#
> Failure 123.123.12.21 1234
>
>
>
> virtualiptest.ksh
>
> #!/usr/bin/ksh
> # Variables
>
> VIRTUALIPFILE="./virtualips.txt"
>
> # Code Begin
>
> while read LINE
> do
> echo $LINE
> PORTNAME=$(echo $LINE | awk '{print $1}')
> IPADDRESS=$(echo $LINE | awk '{print $2}')
> PORT=$(echo $LINE | awk '{print $3}')
> HOST="$IPADDRESS $PORT" #awk '{print $2 " " $3}' also works
> echo $HOST
> (echo "^]") | telnet $HOST >> /dev/null
> if [ $? == 0 ]
> then
> echo "Success"
> else
> echo "Failure"
> fi
> done < $VIRTUALIPFILE
You might care to read this thread
http://groups-beta.google.com/group/comp.unix.shell/browse_f rm/thread/90db952a0ef89df5/f8397286f844679d
If your "failure" host is not local, then you can wait for 4 minutes to
get the failure code back.
In my experience 'telnet' does not produce useful return codes.
I would write this something like the following.
#!/usr/bin/ksh
# Variables
VIRTUALIPFILE="./virtualips.txt"
testport(){
# Fill in here from the code in the thread referenced
read up < /dev/tcp/$1/$2
}
while read PORTNAME IPADDRESS PORT
do
if testport $IPADDRESS $PORT
then
echo "Success"
else
echo "Failure"
fi
done < "$VIRTUALIPFILE"
Re: ksh and telnet script
am 12.08.2005 18:04:45 von NOYB
Thanks for the information. I used the example you had in the thread as
it had the nice 15 seconds timeout feature.
I am not getting the desired results; I still get failure for tests that
"telnet port" succeeds. Any suggestions?
cat test2.ksh
#!/usr/bin/ksh
exec 2>/dev/null
date
var=$(echo "hello"
ksh -c '(sleep 15; kill $$)&
read fred < /dev/tcp/xxx.xxx.xx.xxx/18012
kill $!
echo "$fred"
')
echo $? $var
date
Test one using port 22 - should succeed
# ./test2.ksh
Fri Aug 12 10:43:59 EDT 2005
0 hello SSH-2.0-Sun_SSH_1.0.1
Fri Aug 12 10:44:14 EDT 2005
#
Test two using port 18012 - should succeed, but doesn't I get the same
result using an invalid ip address.
# ./test2.ksh
Fri Aug 12 10:39:21 EDT 2005
143 hello
Fri Aug 12 10:39:36 EDT 2005
This port does successfully reply instantly to telnet thoughÂ…
# telnet xxx.xxx.xx.xxx 18012
Trying xxx.xxx.xx.xxx...
Connected to xxx.xxx.xx.xxx.
Escape character is '^]'.
^]
telnet> quit
Connection to xxx.xxx.xx.xxx closed.
#
Test three using port 220999 - should fail
Fri Aug 12 10:42:15 EDT 2005
0 hello
Fri Aug 12 10:42:30 EDT 2005
Icarus Sparry wrote:
> On 2005-08-11, NOYB wrote:
>
>>I want to create a ksh script that reads in a list of category, ip and
>>ports, validate that the server is listening on the port, and then
>>return. We cannot install expect or anything else due to politics so I
>>want to keep all commands within the shell scripts. Below is the work
>>in progress. When I run the script, successful connections are fine,
>>the problem is in the return code on failures, it just hangs. Can
>>anyone assist? Does anyone have an alternative method that only does not
>>use expect - (perl is allowed, but without any add on modules). The
>>server is a Solaris 9 core installation.
>>
>>
>>
>>virtualips.txt
>>Success1 xxx.xxx.xxx.xxx Port#
>>Success2 xxx.xxx.xxx.xxx Port#
>>Failure 123.123.12.21 1234
>>
>>
>>
>>virtualiptest.ksh
>>
>>#!/usr/bin/ksh
>># Variables
>>
>>VIRTUALIPFILE="./virtualips.txt"
>>
>># Code Begin
>>
>>while read LINE
>>do
>>echo $LINE
>> PORTNAME=$(echo $LINE | awk '{print $1}')
>> IPADDRESS=$(echo $LINE | awk '{print $2}')
>> PORT=$(echo $LINE | awk '{print $3}')
>> HOST="$IPADDRESS $PORT" #awk '{print $2 " " $3}' also works
>> echo $HOST
>> (echo "^]") | telnet $HOST >> /dev/null
>> if [ $? == 0 ]
>> then
>> echo "Success"
>> else
>> echo "Failure"
>> fi
>>done < $VIRTUALIPFILE
>
>
> You might care to read this thread
>
> http://groups-beta.google.com/group/comp.unix.shell/browse_f rm/thread/90db952a0ef89df5/f8397286f844679d
>
> If your "failure" host is not local, then you can wait for 4 minutes to
> get the failure code back.
>
> In my experience 'telnet' does not produce useful return codes.
> I would write this something like the following.
>
> #!/usr/bin/ksh
> # Variables
>
> VIRTUALIPFILE="./virtualips.txt"
>
> testport(){
> # Fill in here from the code in the thread referenced
> read up < /dev/tcp/$1/$2
> }
>
> while read PORTNAME IPADDRESS PORT
> do
> if testport $IPADDRESS $PORT
> then
> echo "Success"
> else
> echo "Failure"
> fi
> done < "$VIRTUALIPFILE"