Skip to content

Instantly share code, notes, and snippets.

View lanbugs's full-sized avatar

Maximilian Thoma lanbugs

View GitHub Profile
@lanbugs
lanbugs / cisco_inventory.py
Created June 26, 2018 07:37
Commandline Tool for exporting Cisco Hardware Inventory via SNMP
#!/usr/bin/env python
# Need following pip packages
# - easysnmp
# - tabulate
# Checkout blog article to tool
# https://lanbugs.de/netzwerktechnik/hersteller/cisco/commandline-tool-for-exporting-cisco-hardware-inventory-via-snmp/
@lanbugs
lanbugs / search_linenumber.py
Created June 22, 2018 22:48
Search in file and get line number with python
#!/usr/bin/env python
filename = 'test.txt'
search = 'foobar'
with open(filename) as f:
for num, line in enumerate(f, 1):
if search in line:
print '%s - found at line:' % search, num
@lanbugs
lanbugs / search_and_replace_p2.py
Created June 22, 2018 22:46
Search and replace in file with python 2.x
#!/usr/bin/env python
import fileinput
import re
import sys
file = fileinput.FileInput("/etc/ssh/sshd_config", inplace=True, backup=".bak")
for line in file:
line = re.sub(r".*Banner.*","Banner /etc/issue.net", line)
sys.stdout.write(line)
file.close()
@lanbugs
lanbugs / search_and_replace_p3.py
Created June 22, 2018 22:45
Search and replace in files with python3
#!/usr/bin/env python3
import fileinput
import re
file = fileinput.FileInput("/etc/ssh/sshd_config", inplace=True, backup=".bak")
for line in file:
line = re.sub(r".*Banner.*","Banner /etc/issue.net", line)
print(line, end='')
file.close()
@lanbugs
lanbugs / age_of_file.py
Created June 22, 2018 22:27
Calculate age of file with python
#!/usr/bin/env python
import datetime
import os
file_mod_time = datetime.datetime.fromtimestamp(os.path.getmtime('foobar.txt'))
today = datetime.datetime.today()
age = today - file_mod_time
@lanbugs
lanbugs / damaged_utf8.py
Created June 22, 2018 22:26
Repair damaged UTF-8 string in python
#!/usr/bin/env python
name_kaputt = 'Gesch\xc3\xa4ftsstelle'
name = ''.join(chr(ord(c)) for c in name_kaputt).decode("utf-8")
print name_kaputt
print name
# Before: Geschäftsstelle
# Result: Geschäftsstelle
@lanbugs
lanbugs / multiprocessing_with_result.py
Created June 22, 2018 22:22
Multiprocessing with result in python
#!/usr/bin/env python
# -*- encoding: utf-8; py-indent-offset: 4 -*-
import os
from multiprocessing import Pool
def worker(job):
x, y = job
result = x ** y
@lanbugs
lanbugs / ssh_shell_to_cisco_devices.py
Created June 22, 2018 22:21
SSH shell to Cisco devices
#!/usr/bin/env python
import paramiko
import sys
def send_string_and_wait_for_string(command, wait_string, should_print):
shell.send(command)
receive_buffer = ""
@lanbugs
lanbugs / send_email_via_smtp.py
Created June 22, 2018 22:20
Mini mailer in python to send mails via SMTP
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import smtplib
from email.mime.text import MIMEText
def postmaster(mfrom, mto, msubject, message, smtphost):
msg = MIMEText(message.encode("utf-8"))
@lanbugs
lanbugs / paloalto_xmlapi.py
Created June 22, 2018 22:16
Palo Alto Networks XML API with python and beautifulsoup, example prints ARP table
#!/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