Installing pyodbc on a mac

After installing pyodbc onto my linux server that runs my scripts I needed to create my development environment on my mac so I could develop the scripts and then transfer them over. I went through a couple of websites to put together my steps:

http://www.christophers.tips/pages/pyodbc_mac.html
https://github.com/mkleehammer/pyodbc/wiki/Connecting-to-SQL-Server-from-Mac-OSX

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

brew update

brew install unixodbc freetds

Pip install pyodbc

Giving up my 100 days of code

So I am 20 days into my 100 days of code and I am going to stop going through the class I was taking on Udemy. I have finally gotten to a point where a lot of the modules they are trying to have me use aren’t working and I am spending more time troubleshooting their stuff than I am actually learning. So now I am going to start my own exercises and continue my learning on my own. I am going to try and cherry pick some of the courses in the class that still work, but will end up refining my code with what I have already learned and trying to put some the practices into place to improve my code. I will also work on automating more of the functions that need to be automated in my environment. In all I am frustrated by the fact that I could complete the Udemy course, but thankful for the few things that I have learned.

Starting my 100 days of coding

I took up the 100 days of coding challenge to work on my coding skills in Python. I am pretty good with the requests module, but that is about it. I figured this was a way to work on my coding skills and help learn some additional skills that I didn’t have before. Already 7 days in I’ve managed to learn a few things I didn’t know before and also managed to build out some programs so I can update my existing stuff and add in some password randomization. I built a git repository for all of the coding projects under one single heading. Figured this way I could reference it later if needed:

 

https://github.com/undrwatr/100daysofcode

Scripts to migrate hosts from ASA to Fortigate

For my job I am in the process of migrating from an ASA to a FortiGate firewalls. Part of this has been moving the configuration that we already have in place on the ASA and translating it too FortiGate. I needed to convert several address lists. Some of those address lists where hundreds of addresses long and I didn’t want to type those in. So I started using my python skills to build out the configuration by taking in a list of the ip addresses and then outputting the configuration needed for the FortiGate firewall.

Here is the current version of that script:

# variables needed througout:
file = input(“Name of file? “)
title = input(“Name of hosts and group? “)

addresses = open(file, “r”)

print(“config firewall address”)

incr = 1
host_entries = ‘set member’
for address in addresses:
    address = address.rstrip(‘\n’)
    print(‘edit “H_’ + title + str(incr) + ‘”’)
    host_entries = (host_entries + (‘ “H_’ + title + str(incr) + ‘”’))
    print(“set subnet ” + address + ” 255.255.255.255”)
    print(“next”)
    incr = (incr + 1)

print(‘end’)
print(“config firewall addrgrp”)
print(‘edit “G_’+ title + ‘”‘)
print(host_entries)
print(‘next’)
print(‘end’)

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])