Created
January 4, 2021 05:44
-
-
Save seadog007/14a0b6b4c4ffb8d257875f1b0f490dcf to your computer and use it in GitHub Desktop.
Shell Defined Network - Syncing the IP lease setting from phpIPAM to RouterOS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# | |
# Author: seadog007 | |
# Date: 2021/01/03 | |
# Description: Adding DHCP Lease & Static ARP & IP Whitelist from phpIPAM | |
# which makes phpIPAM actually managed IPs | |
# | |
ipam='192.168.1.5' | |
user='admin' | |
pass='password' | |
ipbase='192.168.' | |
# Login & Create Session | |
token=$(curl -s -k 'https://'"$ipam"'/api/ros/user/' -X POST --user "$user"':'"$pass" | jq -r '.data.token') | |
cmd=$(mktemp) | |
# Clean Previous Settings | |
echo '/ip dhcp-server lease remove [find comment="Managed by Shell Defined Network"]' >> "$cmd" | |
echo '/ip arp remove [find where comment="Managed by Shell Defined Network"]' >> "$cmd" | |
echo '/ip firewall address-list remove [find where comment="Managed by Shell Defined Network"]' >> "$cmd" | |
# Dump IP MAC TSV ($ipbase . x . 1~251) | |
curl -s -k -H 'token: '"$token" 'https://'"$ipam"'/api/ros/addresses/tags/2/addresses' | jq -r '.data[] | if ((.ip | test(".25(2|3|4)$") | not) and (.ip | test("^'"$ipbase"'")) and .mac) then . else empty end | [.ip, .mac] | @tsv' | while read line | |
do | |
ip=$(echo "$line" | awk -F '\t' '{print $1}') | |
mac=$(echo "$line" | awk -F '\t' '{print $2}') | |
# Add DHCP Lease | |
echo '/ip dhcp-server lease add address='"$ip"' mac-address='"$mac"' comment="Managed by Shell Defined Network"' >> "$cmd" | |
# Add Static ARP Binding | |
echo '/ip arp add address='"$ip"' mac-address='"$mac"' interface=([/ip route check dst-ip='"$ip"' once as-value]->"interface") comment="Managed by Shell Defined Network"' >> "$cmd" | |
# Add IP Whitelist | |
echo '/ip firewall address-list add address='"$ip"' list="whitelist" comment="Managed by Shell Defined Network"' >> "$cmd" | |
done | |
# Logout & Delete Session | |
curl -s -k -H 'token: '"$token" 'https://'"$ipam"'/api/ros/user/delete' -X DELETE > /dev/null | |
# Deploy rules to ROSs | |
ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -T [email protected] < "$cmd" | |
# Clean up | |
#echo "$cmd" | |
rm "$cmd" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment