Firesight API Explorer

I am having to make some changes to an object group in the Firesight management platform for my firewalls. Some of these changes required me to add and remove a 50-100 network entries at one time and doing it by hand was going to take a while and could also be prone to errors. I started to research the options for using the api interface for Firesight and came across this YouTube video from Cisco Devnet showing how to use the API Explorer built into firesight and showing how I could easily take and copy out a program so that I could build the object group very easily with a python script. So now I have taken what was started in the API Explorer and modifying it to pull the data out of my central address repository, format the data, and then build the script to run. You can see the in process script here.

Python Program to sort RAW vs JPG files from a photo shoot

I shoot a lot of photos at airshows and needed a quick to sort the RAW photos from the JPGs and to put them into different directories. I may not always use the RAW photos, but I don’t want to discard, I also may not want them in my Lightroom library. This program will quickly sort them, name them, and put them into separate directories for import into Lightroom.

The programs repository is here: https://github.com/undrwatr/SONY_FILE_SORT

This is of course version 1:

#!/usr/bin/env python3

#Program to sort my photo files between Sony RAW and JPG files. Sorts into two separete directores so I can chooose what to import into Lightroom.

import os
import shutil

# ask for the name of the directory
SORT_DIR = os.getcwd()

DIRECTORY = input("What is the name of the Directory to be created? ")


DIRECTORY_RAW = (DIRECTORY + '_RAW')
DIRECTORY_JPG = (DIRECTORY + '_JPG')



# List the files in the source directory
list_ = os.listdir(SORT_DIR)

# rename the files based on the requested directory name
INCR = 0

for file_ in list_:
    name, ext = os.path.splitext(file_)
    INCR = INCR +1
    os.rename(file_, ((DIRECTORY) + "-" + str(INCR) + ext))


# create a directory for jpg
# create a directory for raw files

os.mkdir(DIRECTORY_RAW)
os.mkdir(DIRECTORY_JPG)

CURRENT_DIR = os.getcwd()

list1_ = os.listdir(SORT_DIR)

# Sort the photos into the directories, but do not sort the directory

for file1_ in list1_:
    if file1_.endswith('.ARW'):
        shutil.move(((CURRENT_DIR) + "/" + (file1_)), ((CURRENT_DIR) + "/" + (DIRECTORY_RAW)))
    if file1_.endswith('.JPG'):
        shutil.move(((CURRENT_DIR) + "/" + (file1_)), ((CURRENT_DIR) + "/" + (DIRECTORY_JPG)))
    else:
        continue

Installing PYODBC onto Ubuntu

Steps to install PYODBC on Ubuntu and the requirements that needed to be done to convert from PYMSSQL to PYODBC for connecting to an MS SQL database to pull information. Just wanted to get all of these steps in one place and everything that I went through to get it working in my Linux environment.

sudo curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add –

#Download appropriate package for the OS version
#Choose only ONE of the following, corresponding to your OS version

#Ubuntu 16.04
curl https://packages.microsoft.com/config/ubuntu/16.04/prod.list > /etc/apt/sources.list.d/mssql-release.list

#Ubuntu 18.04
curl https://packages.microsoft.com/config/ubuntu/18.04/prod.list > /etc/apt/sources.list.d/mssql-release.list

#Ubuntu 19.10
curl https://packages.microsoft.com/config/ubuntu/19.10/prod.list > /etc/apt/sources.list.d/mssql-release.list

sudo apt-get update
sudo ACCEPT_EULA=Y apt-get install msodbcsql17
sudo apt-get install unixodbc-dev

Once the driver is installed you then need to install pyodbc with pip
pip3 install pyodbc

After I did these steps it then did require me to reinstall the requests module for some reason to resolve some HTTPS issues. Not really sure what that issue was or the reason for it.

Reference links for the installs from Microsoft:

https://docs.microsoft.com/en-us/sql/connect/python/pyodbc/step-3-proof-of-concept-connecting-to-sql-using-pyodbc?view=sql-server-ver15

https://docs.microsoft.com/en-us/sql/connect/odbc/linux-mac/installing-the-microsoft-odbc-driver-for-sql-server?view=sql-server-ver15

Reboot Meraki APs

I have found myself several times over the last couple of months needing to reboot all of the APs within a Meraki network. Sometimes due to changes or sometimes due to them not responding for some reason. There really isn’t a clean way of going through and rebooting them aside from one a time within the console. I thought hey I can make this one better and do it via the API. So I went through and built this script to allow someone to put in the Org id and then it will pull back all of the networks that are in that Org and allow you to choose one to reboot all of the APs. It will ask should it go as fast as possible or would you like to put in a delay so that they all don’t go down at the same time. I’ve tested it a couple of times and everything works as its supposed to. As always I look forward to any comments or updates that I can put into the code to make it better.

https://github.com/undrwatr/MERAKI_AP_REBOOT

As usually my code isn’t fancy or special, just serviceable and able to get done what I need and save me some time and headaches.

Retrieve SSID info from Meraki Wireless Network

I support multiple Meraki Wireless networks and I wanted the ability to pull in pertinent data from the wireless networks that I support. This will allow me to pull in the data and then upload it into the wireless mapping programs and design software. This will also allow me to easily pull the channels, power settings, SSIDs, and other pertinent information. Here is the link to my GitHub site where I will be keeping the most up to date program as I fix things and work to improve it.

#!/usr/bin/env python

#import necessary modules
import cred
import requests
#Meraki site information
MERAKI_DASHBOARD = 'https://api.meraki.com'
HEADERS = {'X-Cisco-Meraki-API-Key': (cred.key), 'Content-Type': 'application/json'}
#NETWORK = input(str("What network are we looking at? "))
NETWORK = cred.network
NETWORK_URL = MERAKI_DASHBOARD + '/api/v0/networks/%s/devices' % NETWORK
NETWORK_GET = requests.get(NETWORK_URL, headers=HEADERS)
NETWORK_RESPONSE = NETWORK_GET.json()
#Create a function pull in the information
def WIRELESS_SETTINGS():
WIRELESS_SETTINGS_URL = MERAKI_DASHBOARD + '/api/v0/networks/%s/devices/%s/wireless/status' % (NETWORK, DEVICE['serial'])
WIRELESS_SETTINGS_GET = requests.get(WIRELESS_SETTINGS_URL, headers=HEADERS)
WIRELESS_SETTINGS_RESPONSE = WIRELESS_SETTINGS_GET.json()
for SSIDS in WIRELESS_SETTINGS_RESPONSE['basicServiceSets']:
if SSIDS['enabled'] == True:
print("SSID " + (SSIDS['ssidName']) + " BAND " + (SSIDS['band']) + " BSSID " + str(SSIDS['bssid']) + " Channel " + str(SSIDS['channel']) + " Power " + str(SSIDS['power']))
#Loops through the network and the devices to find all of the information.
for DEVICE in NETWORK_RESPONSE:
if DEVICE['model'] == "MR42":
print("AP " + DEVICE['name'])
WIRELESS_SETTINGS()

Moving from Pymssql to Pyodbc

I was in the process of updating one of my linux servers with Pymssql when I kept getting an error message about it trying to do the install:

ERROR: Could not find a version that satisfies the requirement pymmssql (from versions: none)
ERROR: No matching distribution found for pymmssql

Turns out pymmsql had been deprecated and was no longer being supported. I use pymmsql to connect to my sql database in order to pull data for ip addresses and other network specific information to build my stores in the Meraki Portal.

For the most part the conversion was fairly easy, I think the biggest issue I had was in getting the drivers installed for MS SQL for pyodbc. This site from MS was helpful in installing the ODBC drivers on Mac and Linux. Once I got the drivers installed I then had some issues with making https calls via my python programs. I had to reinstall python via pyenv reinstall. Once the version was reinstalled then it resolved the issues with the API calls and the requests module. Here is an example of the code I was running and the code that I am now running.

PYMSSQL CODE:

import pymssql as mdb

sql_host = cred.sql_host
sql_username = cred.sql_username
sql_password = cred.sql_password
sql_database = cred.sql_database
store = str(input(“What store are we creating?: “))
sql_connection = mdb.connect(sql_host,sql_username,sql_password,sql_database)
cursor = sql_connection.cursor()
cursor.execute(“select [VID2GW] from tblDSlip where [Store #] = (%s)”, (store))
VLAN2GW = str(cursor.fetchone()[0])

PYODBC CODE:

import pyodbc
store = str(input(“What store are we creating?: “))

if len(store) == 2:
sql_store = ’00’ + store
elif len(store) == 3:
sql_store = ‘0’ + store
else:
sql_store = store

server = cred.sql_host
database = cred.sql_database
username = cred.sql_username
password = cred.sql_password

cnxn = pyodbc.connect(‘DRIVER={ODBC Driver 17 for SQL Server};SERVER=’+server+’;DATABASE=’+database+’;UID=’+username+’;PWD=’+ password)
cursor = cnxn.cursor()
cursor.execute(“select [VID2GW] from tblDSlip where [Store #] = ?”, sql_store)
VLAN2GW = str(cursor.fetchone()[0])

 

Date/Time Conversion in Python – ISO 8601 UTC to Pacific Standard Time

I had an issue where I was getting an ISO 8601 UTC date back from one of my API requests and I was asked to convert it to Pacific time as I was providing the information in a report. Since I had no idea what I was doing I first had to figure out what kind of date I was dealing with so that I could then figure out what I was trying to get to.

Here is what an ISO 8601 date looks like:  2019-12-24T15:04:18Z


Here is what the format of the data will look like after the process has been run: 12-24-2019 7:04:18

#Modules needed
import datetime
import pytz

#set the variable for Pacific timezone
pst = pytz.timezone(‘US/Pacific’)

#create the time variable date_time that will be used throughout, the data is coming from an api request and I am getting back json data.
date_time = (ISO 8601 Date)
#set the format for the date time
date_time = datetime.datetime.strptime(date_time, “%Y-%m-%dT%H:%M:%SZ”)
#tell python the current timezone is UTC
date_time = pytz.timezone(‘UTC’).localize(date_time)
#tell python to change the timezone to Pacific
date_time = date_time.astimezone(pst)
#update the variable with the new time and date in the new format
date_time = date_time.strftime(“%m-%d-%y %H:%M:%S”)