Julio Jose
asked on
unix port test script
I want to perform the port test verification on the script using telnet command but it always want to send break when the port is not reachable, any idea to do that? I don't use nc because telnet is the command on all Unix Linux platform.
telnet -c 1.1.1.1 443 </dev/null 2>&1
telnet -c 1.1.1.1 443 </dev/null 2>&1
ASKER
Hi, I want to use existing "telnet" command will complete the test because all the OS have this command come from the build
Is the problem that the telnet hangs for a long time if the port is unreachable?
In that case, you could put the telnet in the background, and kill it if it doesn't complete within a short time (here, 30 seconds), such as:
In that case, you could put the telnet in the background, and kill it if it doesn't complete within a short time (here, 30 seconds), such as:
host=1.1.1.1
port=443
telnet ${host} ${port} </dev/null 2>&1 &
tpid=$!
scount=0
maxcount=30
while [ $scount -lt $maxcount ]; do
if kill -0 $tpid 2>/dev/null; then
# Telnet still running - wait a bit more
((scount += 1))
sleep 1
else
break
fi
done
if [ $scount -eq $maxcount ]; then
kill -9 $tpid
echo Failed to connect to ${1:-1.1.1.1}:${2:-443} after $maxcount seconds
fi
wait $tpid
rc=$?
echo Return code from telnet was $rc
By the way, telnet on Debian doesn't support the "-c" parameter
ASKER
Hi Simon,
I run the script and get the desired output, the platform is RHEL, AIX and Solaris
Fail
Success
My current code using nc test to 2 IP address need to replace with this telnet test to output the result to the log file
Standard output on console
The installation log output
I need to run the port test for 2 different IP address, can complete in the same script?
The standard output error can suppress?
I run the script and get the desired output, the platform is RHEL, AIX and Solaris
Fail
Trying 1.1.1.1...
Failed to connect to 1.1.1.1:443 after 30 seconds
./port_test.sh: line 20: 4722 Killed telnet ${host} ${port} < /dev/null 2>&1
Return code from telnet was 137
Success
Trying 192.168.0.1...
Connected to (192.168.0.1).
Escape character is '^]'.
Connection closed by foreign host.
Return code from telnet was 1
My current code using nc test to 2 IP address need to replace with this telnet test to output the result to the log file
echo "---------------------------------------------------------------------------" >> /tmp/"$HOSTNAME"-installation.log
nc -z -v -w3 $SERVER_IP 443 2>&1 | tee -a /tmp/"$HOSTNAME"-agent-installation.log
echo "---------------------------------------------------------------------------" >> /tmp/"$HOSTNAME"-installation.log
#host=192.168.0.1
#port=80
telnet $SERVER_IP 443 </dev/null 2>&1 &
tpid=$!
scount=0
maxcount=30
while [ $scount -lt $maxcount ]; do
if kill -0 $tpid 2>/dev/null; then
# Telnet still running - wait a bit more
((scount += 1))
sleep 1
else
break
fi
done
if [ $scount -eq $maxcount ]; then
kill -9 $tpid
echo Failed to connect to ${1:-1.1.1.1}:${2:-443} after $maxcount seconds >> /tmp/"$HOSTNAME"-installation.log
fi
wait $tpid
rc=$?
echo Return code from telnet was $rc >> /tmp/"$HOSTNAME"-agent-installation.log
Standard output on console
Trying 10.21.151.160...
./port_test.sh: line 30: 4809 Killed telnet $SERVER_IP 443 < /dev/null 2>&1
The installation log output
Failed to connect to 1.1.1.1:443 after 30 seconds
Return code from telnet was 137
I need to run the port test for 2 different IP address, can complete in the same script?
The standard output error can suppress?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi, I updated it to same file and I change the error code message to successful connect work that make sense because I don't need the error code.
Regarding the code I tested in single script it work great but integrate to my my existing script need your advice how to test for 6 IP address based on the condition.
This is my existing code
Thanks
Regarding the code I tested in single script it work great but integrate to my my existing script need your advice how to test for 6 IP address based on the condition.
This is my existing code
if [ "$12" == "Apple" ] || [ "$34" == "Orange" ]
then
pingserv $SERVER1_IP
pingserv $SERVER2_IP
elif [ "$56" == "Watermelon" ] || [ "$78" == "Pineapple" ]
pingserv $SERVER3_IP
pingserv $SERVER4_IP
else
pingserv $SERVER5_IP
pingserv $SERVER6_IP
fi
Thanks
You could include the function code into your script, but it might be easier to put the code in a file of its own, and call it pingserv.sh
Couple of things in your script = you use "=" in "if [" tests (it's only "==" in "if [[" tests), and in a Bash script you only have $1 to $9 - $12 is not a valid variable name.
#!/bin/bash
function pingserv {
(function body as before)
}
pingserv $1
Make it executable (chmod +x pingserv.sh) and put it somewhere in your PATH (/usr/local/bin, for example). then you can call it from another script, like
#!/bin/bash
if [ $1 = "apple ]; then
pingserv.sh $SERVER1_IP
pingserv.sh $SERVER2_IP
elif [ $1 = "banana" ] || [ $1 = "orange ]; then
pingserv.sh $SERVER3_IP
pingserv.sh $SERVER4_IP
else
pingserv.sh $SERVER5_IP
pingserv.sh $SERVER6_IP
fi
Couple of things in your script = you use "=" in "if [" tests (it's only "==" in "if [[" tests), and in a Bash script you only have $1 to $9 - $12 is not a valid variable name.
ASKER
Hi Simon, can you make all the code in the single file instead using separate file?
Yes, no problem with that. Just include the pingserv function, and use the "if" test section I gave above with lines such as
pingserv $SERVER1_IP
pingserv $SERVER1_IP
ASKER
Thanks Simon
http://expect.sourceforge.net/
please see example @ below link:
https://elenako.com/2013/04/04/expect-script-start-telnet-run-commands-close-telnet/