Created
June 22, 2018 22:16
-
-
Save lanbugs/db3d405d40586e3b61873ab5dbe4fb0f to your computer and use it in GitHub Desktop.
Palo Alto Networks XML API with python and beautifulsoup, example prints ARP table
This file contains hidden or 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
#!/usr/bin/env python | |
# required pip packages: lxml, beautifulsoup4, tabulate | |
from bs4 import BeautifulSoup as BS | |
import urllib2 | |
import ssl | |
import urllib | |
from tabulate import tabulate | |
username = "api_test" | |
password = "supersecurepassword" | |
firewall = "192.168.111.1" | |
##################### | |
# SSL ignore Cert Check (Lab Environment only!) | |
ctx = ssl.create_default_context() | |
ctx.check_hostname = False | |
ctx.verify_mode = ssl.CERT_NONE | |
##################### | |
# Req function | |
def req_api(command, key): | |
url = "https://%s/api/?key=%s&type=op&cmd=" % (firewall, key) + urllib.quote_plus(command) | |
res = urllib2.urlopen(url, context=ctx) | |
return res.read() | |
##################### | |
# API Key request | |
req_api_url = "https://%s/api/?type=keygen&user=%s&password=%s" % (firewall, username, password) | |
res_api_key = urllib2.urlopen(req_api_url, context=ctx) | |
soup = BS(res_api_key.read(), "lxml") | |
key = soup.find('key').text | |
##################### | |
# Get arp entrys | |
soup = BS(req_api("<show><arp><entry name = 'all'/></arp></show>", key), "lxml") | |
arp_buffer = [] | |
for e in soup("entry"): | |
arp_buffer.append([e.status.text, e.ip.text, e.ttl.text, e.interface.text, e.port.text, e.mac.text]) | |
print "#" * 120 + "\n> ARP Cache Count: %s\n" %(len(arp_buffer)) + "#" * 120 | |
print tabulate(arp_buffer, headers=['Status', 'IP', 'TTL', 'Interface', 'Port', 'MAC'], tablefmt='orgtbl') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment