Questo il risultato, che condivido volentieri con questo forum.
Ovviamente declino ogni responsabilità su eventuali danni che possano derivare dall'esecuzione degli stessi script e anzi mi attendo un costruttivo contributo da parte vostra per il loro miglioramento.
Le istruzioni per l'installazioni sono nel file RedMeFirst
Mi scuso anche per il mio inglese (Sorry, I'm still learning...)
Codice: Seleziona tutto
$ cat RedMeFirst
INSTALLATION
=============
Please, copy the follow scripts in the same directory (ex. /opt), make
root their owner and their group, and finally make them executable by
all.
========================================================================
|
SCRIPT | DESCRIPTION
|
------------------------+-----------------------------------------------
ip_addr_check | it returns 0 exit status if the input
| string is a valid IP address, 1
| otherwise
------------------------+-----------------------------------------------
netmask_check | it returns 0 exit status if the input
| string is a netmask (also in
| Classless-Inter-Domain-Routing notation), 1
| otherwise
------------------------+------------------------------------------------
cidr_2_netmask | it converts netmasks from Decimal-Dotted
| to Classless-Inter-Domain-Routing notation
------------------------+------------------------------------------------
netmask_2_cidr | it converts netmasks from Classless-Inter-
| Domain-Routing to Decimal-Dotted notation
------------------------+------------------------------------------------
dec_2_bin | it converts positive integers numbers from
| decimal to binary notation
------------------------+------------------------------------------------
bin_2_dec | it converts positive integers numbers from
| binary to decimal notation
------------------------+------------------------------------------------
ip_addr-dec_2_bin | it converts the input IP address from
| decimal-dotted to binary notation
------------------------+------------------------------------------------
ip_addr-bin_2_dec | it converts the input IP address from
| binary to decimal-dotted notation
------------------------+------------------------------------------------
network_calc | it calculates from an IP address and subnet
| mask:
| - network id
| - first network address
| - last network address
| - broadcast address
------------------------+------------------------------------------------
network_scan | it scans network for connected/not_connected
| IP address
------------------------+------------------------------------------------
if_config | it configures network card with ip address
| and default gateway
------------------------+------------------------------------------------
set_dns | it sets the Domain Name Servers (default:
| Google DNS)
------------------------+------------------------------------------------
set_proxy | it sets environment variables for proxy server
-------------------------------------------------------------------------
Codice: Seleziona tutto
$ cat ip_addr_check
#!/bin/bash
#
# Description: it returns 0 exit status if the input string is a valid IP address, 1 otherwise
#
# Usage: ip_addr_check <a.b.c.d>
#
# Example: read -p "Please, insert an IP address: " IPv4
# while ! ./ip_addr_check $IPv4
# do
# read -p "Bad input. Please, insert a valid IP address: " IPv4
# done
#
# Author: VDA
#
# Last update: 2019.05.20
#
[ $# -ne 1 ] && { echo "Usage: $0 ip_address"; exit 1; }
if [[ $1 =~ ^(([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])$ ]]; then
exit 0
else
exit 1
fi
Codice: Seleziona tutto
$ cat netmask_check
#!/bin/bash
#
# Description: it returns 0 exit status if the input string is a netmask (also in Classless-Inter-Domain-Routing notation), 1 otherwise
#
# Usage: netmask_check <a.b.c.d>
#
# Example: read -p "Please insert an IP: " IPv4
# .netmask_check $IPv4 && echo "$IPv4 is a network mask" || echo "Not a valid network mask"
#
# Author: VDA
#
# Last update: 2019.05.20
#
[ $# -ne 1 ] && { echo "Usage: $0 {decimal_dotted_notation | cidr_notation}_netmask"; exit 1; }
if [[ $1 =~ ^\/([1-9]{1}|1[0-9]{1}|2[0-9]{1}|30)$ ]]; then
exit 0
fi
if [[ $1 =~ ^((128.0.0.0){1}|(192.0.0.0){1}|(224.0.0.0){1}|(240.0.0.0){1}|(248.0.0.0){1}|(252.0.0.0){1}|(254.0.0.0){1}|(255.0.0.0){1}|(255.128.0.0){1}|(255.192.0.0){1}|(255.224.0.0){1}|(255.240.0.0){1}|(255.248.0.0){1}|(255.252.0.0){1}|(255.254.0.0){1}|(255.255.0.0){1}|(255.255.128.0){1}|(255.255.192.0){1}|(255.255.224.0){1}|(255.255.240.0){1}|(255.255.248.0){1}|(255.255.252.0){1}|(255.255.254.0){1}|(255.255.255.0){1}|(255.255.255.128){1}|(255.255.255.192){1}|(255.255.255.224){1}|(255.255.255.240){1}|(255.255.255.248){1}|(255.255.255.252){1})$ ]]; then
exit 0
fi
exit 1
Codice: Seleziona tutto
$ cat cidr_2_netmask
#!/bin/bash
#
# Description: it converts netmasks from Decimal-Dotted to Classless-Inter-Domain-Routing notation
#
# Usage: cidr_2_netmask </n>
#
# Example: CIDR=$(./cidr_2_netmask /24)
#
# Author: VDA
#
# Last update: 2019.05.20
#
[ $# -ne 1 ] && { echo "Usage: $0 CIDR_notation_netmask"; exit 1; }
[ $1 == "/1" ] && { echo "128.0.0.0"; exit 0; }
[ $1 == "/2" ] && { echo "192.0.0.0"; exit 0; }
[ $1 == "/3" ] && { echo "224.0.0.0"; exit 0; }
[ $1 == "/4" ] && { echo "240.0.0.0"; exit 0; }
[ $1 == "/5" ] && { echo "248.0.0.0"; exit 0; }
[ $1 == "/6" ] && { echo "252.0.0.0"; exit 0; }
[ $1 == "/7" ] && { echo "254.0.0.0"; exit 0; }
[ $1 == "/8" ] && { echo "255.0.0.0"; exit 0; }
[ $1 == "/9" ] && { echo "255.128.0.0"; exit 0; }
[ $1 == "/10" ] && { echo "255.192.0.0"; exit 0; }
[ $1 == "/11" ] && { echo "255.224.0.0"; exit 0; }
[ $1 == "/12" ] && { echo "255.240.0.0"; exit 0; }
[ $1 == "/13" ] && { echo "255.248.0.0"; exit 0; }
[ $1 == "/14" ] && { echo "255.252.0.0"; exit 0; }
[ $1 == "/15" ] && { echo "255.254.0.0"; exit 0; }
[ $1 == "/16" ] && { echo "255.255.0.0"; exit 0; }
[ $1 == "/17" ] && { echo "255.255.128.0"; exit 0; }
[ $1 == "/18" ] && { echo "255.255.192.0"; exit 0; }
[ $1 == "/19" ] && { echo "255.255.224.0"; exit 0; }
[ $1 == "/20" ] && { echo "255.255.240.0"; exit 0; }
[ $1 == "/21" ] && { echo "255.255.248.0"; exit 0; }
[ $1 == "/22" ] && { echo "255.255.252.0"; exit 0; }
[ $1 == "/23" ] && { echo "255.255.254.0"; exit 0; }
[ $1 == "/24" ] && { echo "255.255.255.0"; exit 0; }
[ $1 == "/25" ] && { echo "255.255.255.128"; exit 0; }
[ $1 == "/26" ] && { echo "255.255.255.192"; exit 0; }
[ $1 == "/27" ] && { echo "255.255.255.224"; exit 0; }
[ $1 == "/28" ] && { echo "255.255.255.240"; exit 0; }
[ $1 == "/29" ] && { echo "255.255.255.248"; exit 0; }
[ $1 == "/30" ] && { echo "255.255.255.252"; exit 0; }
echo "$0 - Error: $1 is not a valid input."
exit 1
Codice: Seleziona tutto
$ cat netmask_2_cidr
#!/bin/bash
#
# Description: it converts netmasks from Classless-Inter-Domain-Routing to Decimal-Dotted notation
#
# Usage: netmask_2_cidr <a.b.c.d>
#
# Example: NETMASK=$(./netmask_2_cidr 255.255.254.0)
#
# Author: VDA
#
# Last update: 2019.05.20
#
[ $# -ne 1 ] && { echo "Usage: $0 Decimal_Dotted_Notation_netmask"; exit 1; }
[ $1 == "128.0.0.0" ] && { echo "/1"; exit 0; }
[ $1 == "192.0.0.0" ] && { echo "/2"; exit 0; }
[ $1 == "224.0.0.0" ] && { echo "/3"; exit 0; }
[ $1 == "240.0.0.0" ] && { echo "/4"; exit 0; }
[ $1 == "248.0.0.0" ] && { echo "/5"; exit 0; }
[ $1 == "252.0.0.0" ] && { echo "/6"; exit 0; }
[ $1 == "254.0.0.0" ] && { echo "/7"; exit 0; }
[ $1 == "255.0.0.0" ] && { echo "/8"; exit 0; }
[ $1 == "255.128.0.0" ] && { echo "/9"; exit 0; }
[ $1 == "255.192.0.0" ] && { echo "/10"; exit 0; }
[ $1 == "255.224.0.0" ] && { echo "/11"; exit 0; }
[ $1 == "255.240.0.0" ] && { echo "/12"; exit 0; }
[ $1 == "255.248.0.0" ] && { echo "/13"; exit 0; }
[ $1 == "255.252.0.0" ] && { echo "/14"; exit 0; }
[ $1 == "255.254.0.0" ] && { echo "/15"; exit 0; }
[ $1 == "255.255.0.0" ] && { echo "/16"; exit 0; }
[ $1 == "255.255.128.0" ] && { echo "/17"; exit 0; }
[ $1 == "255.255.192.0" ] && { echo "/18"; exit 0; }
[ $1 == "255.255.224.0" ] && { echo "/19"; exit 0; }
[ $1 == "255.255.240.0" ] && { echo "/20"; exit 0; }
[ $1 == "255.255.248.0" ] && { echo "/21"; exit 0; }
[ $1 == "255.255.252.0" ] && { echo "/22"; exit 0; }
[ $1 == "255.255.254.0" ] && { echo "/23"; exit 0; }
[ $1 == "255.255.255.0" ] && { echo "/24"; exit 0; }
[ $1 == "255.255.255.128" ] && { echo "/25"; exit 0; }
[ $1 == "255.255.255.192" ] && { echo "/26"; exit 0; }
[ $1 == "255.255.255.224" ] && { echo "/27"; exit 0; }
[ $1 == "255.255.255.240" ] && { echo "/28"; exit 0; }
[ $1 == "255.255.255.248" ] && { echo "/29"; exit 0; }
[ $1 == "255.255.255.252" ] && { echo "/30"; exit 0; }
echo "$0 - Error: $1 is not a valid input."
exit 1
Codice: Seleziona tutto
$ cat dec_2_bin
#!/bin/bash
#
# Description: it converts positive integers numbers from decimal
# to binary notation
#
# Usage: dec_2_bin <n>
#
# Example: bin=$(./dec_2_bin 255)
#
# Author: VDA
#
# Last update: 2019.05.20
#
[ $# -ne 1 ] && { echo "Usage: $0 decimal_value"; exit 1; }
if ! [[ $1 =~ ^[0-9]+$ ]]; then
echo "$0 - Error: $1 is an invalid input."
exit 1
fi
bin=""
dec=$1
if [ $dec -eq 0 ]; then
bin=0
else
while [ $dec -ne 0 ]
do
bit=$((dec%2))
dec=$((dec/2))
bin=$bit$bin
done
fi
echo $bin
exit 0
Codice: Seleziona tutto
$ cat bin_2_dec
#!/bin/bash
#
# Description: it converts positive integers numbers from binary
# to decimal notation
#
# Usage: bin_2_dec <binary_value>
#
# Example: n=$(./bin_2_dec 1100)
#
[ $# -ne 1 ] && { echo "Usage: $0 binary_value"; exit 1; }
if ! [[ $1 =~ ^[01]+$ ]]; then
echo "$0 - Error: \"$1\" is an invalid input."
exit 1
fi
bin=$1;
bin_lenght=$(echo -n $bin | wc -m)
if [ $bin_lenght -eq 1 ]; then
dec=$bin
else
dec=0
k=0
while [ $k -lt $bin_lenght ]
do
bit=${bin:k:1}
exp=$((bin_lenght-k-1))
dec=$((dec+bit*2**exp))
k=$((k+1))
done
fi
echo $dec
exit 0
Codice: Seleziona tutto
$ cat ip_addr-dec_2_bin
#!/bin/bash
#
# Description: it converts the input IP address from decimal-dotted to binary notation
#
# Usage: ip_addr-dec_2_bin <a.b.c.d>
#
# Example: bin_ip_addr=$(./ip_addr-dec_2_bin 192.168.1.17)
#
# Author: VDA
#
# Last update: 2019.05.23
#
[ $# -ne 1 ] && { echo "Usage: $0 dotted_decimal_notation_ip_address"; exit 1; }
dir=$(dirname $0)
$dir/ip_addr_check $1 || { echo "$0 - Error: $1 is not an IPv4 address."; exit 1; }
bin_ip=""
for i in 1 2 3 4
do
dec_oct=$(echo $1 | cut -d \. -f $i)
bin_oct=$($dir/dec_2_bin $dec_oct)
bin_oct_len=$(echo -n $bin_oct | wc -m)
while [ $bin_oct_len -lt 8 ]
do
bin_oct=0$bin_oct
bin_oct_len=$((bin_oct_len+1))
done
bin_ip=$bin_ip$bin_oct
done
echo $bin_ip
exit 0
Codice: Seleziona tutto
$ cat ip_addr-bin_2_dec
#!/bin/bash
#
# Description: it converts the input IP address from binary to decimal-dotted notation
#
# Usage: ip_addr-bin_2_dec <32_bit_binary_ip>
#
# Example: dec_ip_addr=$(./ip_addr-bin_2_dec 11111111111111111111111100000000)
#
# Author: VDA
#
# Last update: 2019.05.23
#
[ $# -ne 1 ] && { echo "Usage: $0 binary_ip_address"; exit 1; }
if ! [[ $1 =~ ^[01]{32}$ ]]; then
echo "$0 - Error: $1 is an invalid input."
exit 1
fi
dir=$(dirname $0)
bin_ip=$1
dec_ip=""
for i in 0 8 16 24
do
bin_oct=${bin_ip:i:8}
dec_oct=$($dir/bin_2_dec $bin_oct)
dec_ip=$dec_ip$dec_oct
[ $i -ne 24 ] && dec_ip=$dec_ip\.
done
echo $dec_ip
exit 0
Codice: Seleziona tutto
$ cat network_calc
#!/bin/bash
#
# Description: it calculates from an IP address and subnet mask:
#
# - network id
# - first network address
# - last network address
# - broadcast address
#
# Usage: network_calc <ip_addr> <netmask>
#
# Example: ret_val=$(./network_calc 192.168.1.1 /22)
# network_id=cut $ret_val -d " " - f 1
# first_ip_addr=cut $ret_val -d " " - f 2
# last_ip_addr=cut $ret_val -d " " - f 3
# broadcast_ip=cut $ret_val -d " " - f 4
#
# Author: VDA
#
# Last update: 2019.05.23
#
[ $# -ne 2 ] && { echo "Usage: $0 ip_address {decimal_dotted_notation | cidr_notation}_netmask"; exit 1; }
dir=$(dirname $0)
$dir/ip_addr_check $1 || { echo "$0 - Error: $1 is an invalid input."; exit 1; }
$dir/netmask_check $2 || { echo "$0 - Error: $2 is an invalid input."; exit 1; }
dec_ip=$1
[[ $2 =~ ^\/ ]] && dec_netmask=$($dir/cidr_2_netmask $2) || dec_netmask=$2
bin_ip=$($dir/ip_addr-dec_2_bin $dec_ip)
bin_netmask=$($dir/ip_addr-dec_2_bin $dec_netmask)
bin_network_id=""
bin_first_network_ip=""
bin_last_network_ip=""
bin_broadcast_ip=""
i=1
while [ $i -le 32 ]
do
ip_bit=$(echo ${bin_ip:((i-1)):1})
netmask_bit=$(echo ${bin_netmask:((i-1)):1})
if [ $netmask_bit -eq 1 ]; then
bin_network_id=$bin_network_id$ip_bit
bin_first_network_ip=$bin_first_network_ip$ip_bit
bin_last_network_ip=$bin_last_network_ip$ip_bit
bin_broadcast_ip=$bin_broadcast_ip$ip_bit
else
bin_network_id=$bin_network_id"0"
if [ $i -lt 32 ]; then
bin_first_network_ip=$bin_first_network_ip"0"
bin_last_network_ip=$bin_last_network_ip"1"
else
bin_first_network_ip=$bin_first_network_ip"1"
bin_last_network_ip=$bin_last_network_ip"0"
fi
bin_broadcast_ip=$bin_broadcast_ip"1"
fi
i=$((i+1))
done
network_id=$($dir/ip_addr-bin_2_dec $bin_network_id)
first_network_ip=$($dir/ip_addr-bin_2_dec $bin_first_network_ip)
last_network_ip=$($dir/ip_addr-bin_2_dec $bin_last_network_ip)
broadcast_ip=$($dir/ip_addr-bin_2_dec $bin_broadcast_ip)
#echo "Network ID: $network_id"
#echo "First IP: $first_network_ip"
#echo "Last IP: $last_network_ip"
#echo "IP addresses range: $first_network_ip - $last_network_ip"
#echo "Broadcast IP address: $broadcast_ip"
echo "$network_id $first_network_ip $last_network_ip $broadcast_ip"
exit 0
Codice: Seleziona tutto
$ cat network_scan
#!/bin/bash
#
# Description: it scans network for connected/not_connected IP address
#
# Usage: netscan <start_ip_address> <end_ip_address>
#
# Example: ./netscan 10.8.110.130 10.8.110.170
#
# Author: VDA
#
# Last update: 2019.05.23
#
[ $# -ne 2 ] && { echo "Usage: $0 start_ip_address end_ip_address"; exit 1; }
dir=$(dirname $0)
$dir/ip_addr_check $1 || { echo "$0 - Error: $1 is an invalid input."; exit 1; }
$dir/ip_addr_check $2 || { echo "$0 - Error: $2 is an invalid input."; exit 1; }
[[ $($dir/ip_addr-dec_2_bin $1) -gt $($dir/ip_addr-dec_2_bin $2) ]] && { echo "$0 - Error: $1 is greater than $2."; exit 1; }
ip_addr_1=$1
ip_addr_2=$2
oct1=$(echo $ip_addr_1 | cut -d "." -f 1)
oct2=$(echo $ip_addr_1 | cut -d "." -f 2)
oct3=$(echo $ip_addr_1 | cut -d "." -f 3)
oct4=$(echo $ip_addr_1 | cut -d "." -f 4)
next_ip_addr=$ip_addr_1
ping -c 1 -w 1 $next_ip_addr &> /dev/null
status=$?
echo -n -e "$next_ip_addr \t"
if [ $status -eq 0 ]; then
echo "connected"
else
echo "not_connected"
fi
while [ $next_ip_addr != $ip_addr_2 ]
do
if [ $oct4 -lt 255 ]; then
oct4=$((oct4+1))
else
if [ $oct3 -lt 255 ]; then
oct3=$((oct3+1))
oct4=0
else
if [ $oct2 -lt 255 ]; then
oct2=$(($oct2+1))
oct3=0
oct4=0
else
if [ $oct1 -lt 255 ]; then
oct1=$((oct1+1))
oct2=0
oct3=0
oct4=0
fi
fi
fi
fi
next_ip_addr=$oct1.$oct2.$oct3.$oct4
ping -c 1 -w 1 $next_ip_addr &> /dev/null
status=$?
echo -n -e "$next_ip_addr \t"
if [ $status -eq 0 ]; then
echo "connected"
else
echo "not_connected"
fi
done
exit 0
Codice: Seleziona tutto
$ cat if_config
#!/bin/bash
#
# Description: it configures network card with ip address and default gateway
#
# Usage: set_ip_addr <network_interface> <ip_addr> <netmask> <default_gateway>
#
# Example: ./set_ip_addr eth0 192.168.1.117 255.255.255.0 192.168.1.1
#
# or
#
# ./set_ip_addr eth0 192.168.1.117 /24
#
# Author: VDA
#
# Last update: 2019.05.23
#
dir=$(dirname $0)
[ $EUID -ne 0 ] && { echo "$0 - Error: you must be root to run this script."; exit 1; }
[ $# -ne 4 ] && { echo "Usage: $0 <network_interface_id> <ip_address> <netmask> <default_gateway>"; exit 1; }
ip link | grep $1 &> /dev/null || { echo "$0 - Error: $1 is not a network interface."; exit 1; }
[ $1 == "lo" ] && { echo "$0 - Error: \"$1\" is not a valid network interface."; exit 1; }
$dir/ip_addr_check $2 || { echo "$0 - Error: $2 is not an IP Address."; exit 1; }
$dir/netmask_check $3 || { echo "$0 - Error: $3 is not a netmask."; exit 1; }
$dir/ip_addr_check $4 || { echo "$0 - Error: $4 is not an IP Address."; exit 1; }
a=$($dir/network_calc $2 $3);
b=$($dir/network_calc $4 $3);
[[ $a != $b ]] && { echo "$0 - Error: IP addresses $2 and $4 must be in the same network"; exit 1; }
[ $2 == $(echo $a | cut -d " " -f 1) ] && { echo "$0 - Error: $2 is not an acceptable IP address"; exit 1; }
[ $4 == $(echo $a | cut -d " " -f 1) ] && { echo "$0 - Error: $4 is not an acceptable IP address"; exit 1; }
[ $2 == $(echo $a | cut -d " " -f 4) ] && { echo "$0 - Error: $2 is not an acceptable IP address"; exit 1; }
[ $4 == $(echo $a | cut -d " " -f 4) ] && { echo "$0 - Error: $4 is not an acceptable IP address"; exit 1; }
if_id=$1
ip_addr=$2
[[ $3 =~ ^\/ ]] && cidr=$3 || cidr=$(./netmask_2_cidr $3)
dg=$4
ip link show up | grep $if_id &> /dev/null || ip link set $if_id up
ip addr flush dev $if_id
ip route flush dev $if_id
ip addr add ${ip_addr}${cidr} dev $if_id
ip route add $dg dev $if_id
ip route add default via $dg dev $if_id
exit 0
Codice: Seleziona tutto
$ cat set_dns
#!/bin/bash
#
# Description: it sets the Domain Name Servers (default: Google DNS)
#
# Usage: /<path_to_script>/set_dns [<ip_addr_1>] [<ip_addr_2>]
#
# Example: ./set_dns 192.168.1.1
#
# Author: VDA
#
# Last update: 2019.05.23
#
[ $USER != "root" ] && { echo "$0 - Error: you must be root to run this script."; exit 1; }
[ $# -gt 2 ] && { echo "Usage: $0 [ip_address_1] [ip-address_2]."; exit 1; }
dns1=${1:- 8.8.8.8}
dns2=${2:- 8.8.4.4}
./check_ip_addr $dns1 || { echo "$0 - Error: \"$dns1\" is not an IP Address."; exit 1; }
./check_ip_addr $dns2 || { echo "$0 - Error: \"$dns2\" is not an IP Address."; exit 1; }
[ -e /etc/resolv.conf ] && cp /etc/resolv.conf /etc/resolv.conf.bck
# chmod go+w /etc/resolv.conf; echo -e "nameserver $dns1 \nnameserver $dns2" > /etc/resolv.conf; chmod go-w /etc/resolv.conf
echo -e "nameserver $dns1 \nnameserver $dns2" | tee /etc/resolv.conf &> /dev/null
exit 0
Codice: Seleziona tutto
$ cat set_proxy
#!/bin/bash
#
# Description: it sets environment variables for proxy server
#
# Usage: /<path_to_script>/set_proxy [{http|https}://][user[:password]@]proxy_server[:port]
#
# Example: ./set_proxy http://john:secret_password@10.854.23:8080
#
# Author: VDA
#
# Last update: 2019.05.23
#
# [ $USER != "root" ] && { echo "$0 - Error: you must be root to run this script."; exit 1; }
[ $# -ne 1 ] && { echo "Usage: $0 [{http|https}://][user[:password]@]proxy_server[:port]"; exit 1;
export http_proxy=$1
export https_proxy=$1
export ftp_proxy=$1
export rsync_proxy=$1
export no_proxy="localhost,127.0.0.1"
exit 0
