-
Notifications
You must be signed in to change notification settings - Fork 0
/
deleteIPFromWhiteList.sh
58 lines (44 loc) · 1.78 KB
/
deleteIPFromWhiteList.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
#!/bin/bash
api_host="${DEEPER_API_HOST:-34.34.34.34}"
# Check if all three arguments are provided
if [ $# -ne 3 ]; then
echo "Usage: $0 <ip_range> <username> <password>"
exit 1
fi
# Parse the IP range, credentials from arguments
ip_range=$1
username=$2
password=$3
# Function to convert IP address to integer
ip_to_int() {
local IFS='.'
read -r ip1 ip2 ip3 ip4 <<< "$1"
printf "%d\n" "$((ip1 * 256 ** 3 + ip2 * 256 ** 2 + ip3 * 256 + ip4))"
}
# Function to convert integer to IP address
int_to_ip() {
local ip=$1
printf "%d.%d.%d.%d\n" "$(((ip >> 24) & 255))" "$(((ip >> 16) & 255))" "$(((ip >> 8) & 255))" "$((ip & 255))"
}
# Run the getToken.sh script and store the output in a variable
token_output=$(/opt/getToken.sh $username $password)
# Extract the Bearer token using grep and cut
bearer_token=$(echo "$token_output" | grep -o '"token":"Bearer[^"]*' | cut -d '"' -f 4)
echo "Token: $bearer_token"
# Extract network address and prefix length from the CIDR range
read network_address prefix_length <<< $(echo "$ip_range" | awk -F '/' '{print $1, $2}')
# Calculate the total number of IP addresses in the range
total_ips=$((2 ** (32 - prefix_length)))
# Convert network address to integer
network_int=$(ip_to_int "$network_address")
# Loop to execute cURL commands for each IP in the range
for ((i = 0; i < total_ips; i++)); do
# Calculate the current IP address
current_ip=$(int_to_ip "$((network_int + i))")
# Build the cURL request command
curl_command="curl -k 'https://$api_host/api/smartRoute/deleteFromWhitelist/ip' -X 'POST' -H 'Authorization: $bearer_token' --data-binary '[\"$current_ip\"]' -H 'Content-Type: application/json'"
# Print the cURL command to console
echo "Executing cURL command: $curl_command"
# Execute the cURL command
eval "$curl_command"
done