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

U880D
  • 8,601
  • 6
  • 24
  • 40
vincent
  • 45
  • 1
  • 9
  • 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? – larsks Jun 25 '18 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) – vincent Jun 25 '18 at 14:51

1 Answers1

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
Reishin
  • 1,854
  • 18
  • 21
  • i am going to try it tomorrow. Thank you for your help. – vincent Jun 25 '18 at 17:43
  • 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 – Reishin Jun 26 '18 at 10:02
  • 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. – vincent Jun 26 '18 at 12:31
  • It works. I have put the solution into my question. Thank you @Reishin for your help. – vincent Jun 27 '18 at 08:28
  • @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. – Reishin Jun 27 '18 at 14:25
  • I am going to clarify: I used a csv with 48 possibilities. I firstly realized the operations with the .sh script. Secondly, i realized the operation another time with data of the csv file and telnet. It was directly by using the computer console and the command line. Thirdly, i verified the egality between the results and it was the same. Please, could you clarify why it is not a good method ? I am not an expert and i need a bigger explication. Thank you for your help – vincent Jun 28 '18 at 07:13
  • @vincent by posting data to opened port via netcat you forcing server to process 'QUIT' command, which may cause errors as server could not understand what it is - and it will close connection with errors in the logs or even crash. Best practice is to open port and close it without sending any data, so server will not processing any unknown formatted commands. – Reishin Jun 29 '18 at 04:12