-
Notifications
You must be signed in to change notification settings - Fork 0
/
portcheck.sh
executable file
·132 lines (113 loc) · 3.08 KB
/
portcheck.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#!/bin/bash
#Script for checking port connectivity. Provide CSV in format COMMENT;ALIAS;IP;PORT...;PORT
#Results output as csv
#Depends on netcat, openbsd-netcat package recommended
timeout=3
tmp_file='tmp.txt'
output_file='results.csv'
method="ip"
# Function definitions
#========================================================
#Perform cleanup on interrupt
trap ctrl_c INT
function ctrl_c() {
rm $tmp_file
exit 1
}
usage (){
echo ""
echo "$(basename $0):"
echo "Utility script for checking port connectivity"
echo "Output saved in results.csv"
echo ""
echo "Usage:"
echo "$(basename $0) [-i] [-a] [-t X] path"
echo "where 'path' leads to a csv file in the format"
echo " \"name ; alias ; ip ; port [; port ; ... ; port]\" "
echo ""
echo "Options:"
echo -e "\t -h Display help"
echo -e "\t -i Set connection method to IP"
echo -e "\t -a Set connection method to alias/DNS-name"
echo -e "\t -t X Set timeout to X seconds"
exit 1
}
check_port(){
nc -zv -w $timeout $1 $2
return $?
}
check_all_ports(){
#set delimiter
IFS=';'
#split into comment alias ip and ports
while read comment alias ip portstr; do
#split ports by space into array
if [ $(wc -w <<< $ip ) -gt 0 ]
then
#read ports into array and iterate
read -ra ports <<< $portstr
for port in "${ports[@]}"; do
#check if port contains a word, else string is garbage
if [ $(wc -w <<< $port ) -gt 0 ]; then
echo -n $comment';'$alias';'$ip';'$port';' >> $output_file
case $method in
ip )
check_port $ip $port
;;
alias )
check_port $alias $port
;;
esac
#check port connectivity and write results
if (( $? == 0 )); then
echo yes >> $output_file
else
echo no >> $output_file
fi
fi
done
fi
#use input from temp file in read func
done < $tmp_file
}
#Parse options
#===============================================
while getopts "iat:" opt; do
case $opt in
i )
method="ip"
;;
a )
method="alias"
;;
t )
timeout="$OPTARG"
;;
h )
usage
;;
\? )
echo "Invalid option: $OPTARG"
usage
;;
esac
done
shift $((OPTIND-1))
#Script begin
#=================================================
input_file=$1
if [ ! -f "$input_file" ];then
echo "Error reading or no input file specified "$input_file
usage
fi
#trim windows-style newlines into temp working file
cat $input_file | tr -d '\r' > $tmp_file
#Set column descriptors
echo 'COMMENT;ALIAS;IP;PORT;REACHABLE?' > $output_file
echo Checking ports
echo Connection method: $method
echo Timeout: $timeout
check_all_ports
#delete temp file after
rm $tmp_file
exit 0