Skip to content

Instantly share code, notes, and snippets.

@seadog007
Created January 4, 2021 05:44
Show Gist options
  • Select an option

  • Save seadog007/14a0b6b4c4ffb8d257875f1b0f490dcf to your computer and use it in GitHub Desktop.

Select an option

Save seadog007/14a0b6b4c4ffb8d257875f1b0f490dcf to your computer and use it in GitHub Desktop.

Revisions

  1. seadog007 revised this gist Jan 4, 2021. No changes.
  2. seadog007 created this gist Jan 4, 2021.
    47 changes: 47 additions & 0 deletions phpipam.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,47 @@
    #!/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"