1

I am a bit lost. I have created a script which starts by retrieving data from a CSV file. Each line contains an IP address and a few ports to test. The goal is to verify that it is possible to connect to each server (under the given IP) on specifics ports. In order to verify, the following code is used:

nc -w 3 -v $ipValeur >> retour.txt 2>&1

Nevertheless, it doesn't work and it returns Connection Timed out. It is strange. In fact, if I launch a telnet command from a terminal, it works. Nevertheless, the goal is to check if a server can be connected to a lot of others. So, if telnet is used, it will be very long (one or two days ?)...

So, I am looking for a way which permits to automatically verify the access from one server to thirty others on a few ports. You can find the code which is actually used at How to continue next iteration when an error occurs in Bash.

Thank you for your help.

Solution

#!/bin/bash

INPUT_FILE=$1

while IFS='' read -r line || [ -n "$line" ]; do
  IFS=';' read -ra cvsline <<<${line}

  HOSTNAME=${cvsline[0]}
  ports=("${cvsline[@]:1}")

  for port in ${ports[*]}; do
    echo -n "Verification ${HOSTNAME}:${port}..."
    echo 'QUIT' | nc -w 3 -v "${HOSTNAME}" "${port}" >/dev/null 2>&1
    if [ $? -eq 0 ]; then
     echo "OK"
    else
     echo "KO"
    fi
  done
done < $INPUT_FILE

Vinz

2
  • Are you positive that the value of $ipValeur is what you expect when you are using nc? Does the same nc command (not telnet) work from the command line when you run it manually? Commented Jun 25, 2018 at 14:33
  • @larsk : yes, i have verified a few times. Nevertheless, the problem should be a bad use of the nc command ? In fact, it mays come from a bad connection or deconnection at the end of the operation (one or two times, i have used ctrl-c. I guess it was connected but it didn't go on with the others rows) Commented Jun 25, 2018 at 14:51

1 Answer 1

1

The answer may be, that in command: nc -w 3 -v $ipValeur >> retour.txt 2>&1 you not passed port number, and was used default one all the times

I not really able to understand your source code, so i have written my own based on description:

#!/bin/bash

INPUT_FILE=$1

while IFS='' read -r line || [ -n "$line" ]; do
  IFS=';' read -ra cvsline <<<${line}

  HOSTNAME=${cvsline[0]}
  ports=("${cvsline[@]:1}")

  for port in ${ports[*]}; do
    echo -n "Cheking ${HOSTNAME}:${port}..."
    nc -zw 3 "${HOSTNAME}" "${port}" >/dev/null 2>&1
    if [ $? -eq 0 ]; then
     echo "connected"
    else
     echo "not connected"
    fi
  done

done < $INPUT_FILE

Usage: ./script hostlist.cvs

Where hostlist.cvs:

127.0.0.1;80;90;100;
127.0.0.2;80;88;21;
10.255.0.1;80;443;

And output sample:

$ ./test.sh /tmp/1
Cheking 127.0.0.1:80...not connected
Cheking 127.0.0.1:90...not connected
Cheking 127.0.0.1:100...not connected
Cheking 127.0.0.2:80...not connected
Cheking 127.0.0.2:88...not connected
Cheking 127.0.0.2:21...not connected
Cheking 10.255.0.1:80...connected
Cheking 10.255.0.1:443...not connected
Sign up to request clarification or add additional context in comments.

7 Comments

i am going to try it tomorrow. Thank you for your help.
how to debug: 1) comment "nc -w 3 -v..." and replace by true command, it should show everything as connected 2) check the output of nc command and exit code it returned. if problems appear at step 1 - post somewhere your cvs file & bash version, if at step 2 - nc work in wrong way , you may consider to use another tool
it seems to arrive when i have a timeout from the nc command. Please, could you explain me what are the operations to follow at step 2.
It works. I have put the solution into my question. Thank you @Reishin for your help.
@vincent your variant still wrong, as you sending to the port the data. I have updated netcat command with proper arguments for netcat. The problem was that if remote server didn't drop connection itself, netcat keeping it for sending there data, that is the reason.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.