Skip to content

Instantly share code, notes, and snippets.

@netmanchris
netmanchris / Add_New_Custom_View.py
Created July 25, 2015 18:29
Add New Custom View
def create_new_view():
view_name = input("Please input the name of the new view: ")
add_view_url = "/imcrs/plat/res/view/custom?resPrivilegeFilter=false&desc=false&total=false"
f_url = url + add_view_url
payload = '''{ "name": "''' + view_name + '''", "autoAddDevType" : "0"}'''
r = requests.post(f_url, data=payload, auth=auth,
headers=headers) # creates the URL using the payload variable as the contents
r.status_code
if r.status_code == 201:
return view_name
@netmanchris
netmanchris / get_custom_views.py
Created July 25, 2015 18:46
Get Custom Views
def get_custom_views():
# checks to see if the imc credentials are already available
if auth == None or url == None:
imc_creds()
get_custom_view_url = '/imcrs/plat/res/view/custom?resPrivilegeFilter=false&desc=false&total=false'
f_url = url + get_custom_view_url
# creates the URL using the payload variable as the contents
r = requests.get(f_url, auth=auth, headers=headers)
if r.status_code == 200:
view_list = (json.loads(r.text))["customView"]
@netmanchris
netmanchris / Add_custom_views.py
Created July 25, 2015 18:49
Add Custom View Main function
def main():
# checks to see if the imc credentials are already available
if auth == None or url == None:
imc_creds()
view_name = create_new_view()
view_list = get_custom_views()
view_id = get_view_id(view_name)
dev_list = filter_dev_category()
add_device_to_view(dev_list, view_id)
def get_view_id(view_name):
view_list = get_custom_views()
for i in view_list:
if i['name'].lower() == view_name.lower():
view_id = i['symbolId']
return i['symbolId']
@netmanchris
netmanchris / Add_device_to_custom_view.py
Created July 25, 2015 20:03
Add Device to Custom View
def add_device_to_view(dev_list, view_id):
modify_custom_view_url = '''/imcrs/plat/res/view/custom/'''+ str(view_id)
device_list = []
for i in dev_list:
dev_id = {'id': i['id'] }
device_list.append(dev_id)
payload = '''{"device":'''+ json.dumps(device_list) + '''}'''
f_url = url+modify_custom_view_url
r = requests.put(f_url, data = payload, auth=auth, headers=headers) #creates the URL using the payload variable as the contents
r.status_code
@netmanchris
netmanchris / XML
Last active August 29, 2015 14:26
>>> x = '''<device>
<id>116</id>
<label>Cisco2811.haw.int</label>
<ip>10.101.0.1</ip>
<mask>255.255.255.0</mask>
<status>1</status>
<statusDesc>Normal</statusDesc>
<sysName>Cisco2811.haw.int</sysName>
<contact>changed this too</contact>
<location>changed this too</location>
>>> print (json.dumps(y, indent = 4))
{
"device": {
"id": "116",
"label": "Cisco2811.haw.int",
"ip": "10.101.0.1",
"mask": "255.255.255.0",
"status": "1",
"statusDesc": "Normal",
"sysName": "Cisco2811.haw.int",
>>> print (yaml.dump(y, default_flow_style = False))
!!python/object/apply:collections.OrderedDict
dictitems:
device: !!python/object/apply:collections.OrderedDict
dictitems:
categoryId: '0'
contact: changed this too
devCategoryImgSrc: router
id: '116'
ip: 10.101.0.1
>>> y = xmltodict.parse(x)
>>> y
OrderedDict([('device', OrderedDict([('id', '116'), ('label', 'Cisco2811.haw.int'), ('ip', '10.101.0.1'), ('mask', '255.255.255.0'), ('status', '1'), ('statusDesc', 'Normal'), ('sysName', 'Cisco2811.haw.int'), ('contact', 'changed this too'), ('location', 'changed this too'), ('sysOid', '1.3.6.1.4.1.9.1.576'), ('sysDescription', None), ('devCategoryImgSrc', 'router'), ('topoIconName', 'iconroute'), ('categoryId', '0'), ('symbolId', '1147'), ('symbolName', 'Cisco2811.haw.int'), ('symbolType', '3'), ('symbolDesc', None), ('symbolLevel', '2'), ('parentId', '1'), ('typeName', 'Cisco 2811'), ('mac', '00:1b:d4:47:1e:68'), ('link', OrderedDict([('@op', 'GET'), ('@rel', 'self'), ('@href', 'http://10.3.10.220:8080/imcrs/plat/res/device/116')]))]))])
>>>
@netmanchris
netmanchris / get_netrange.py
Last active August 29, 2015 14:28
Find netrange from two IP addresses
import ipaddress
def ip_in_network(startIp, endIp):
mask = 32
startIp = ipaddress.ip_address(startIp)
endIp = ipaddress.ip_address(endIp)
netrange = ipaddress.ip_network(str(startIp)+'/'+str(mask), strict=False)
while ((endIp in netrange) == False):
mask -= 1
netrange = ipaddress.ip_network(str(startIp)+'/'+str(mask), strict=False)