Skip to content

Instantly share code, notes, and snippets.

Maximilian Thoma lanbugs

View GitHub Profile
@lanbugs
lanbugs / read_postfix_db.py
Created Jul 5, 2018
Read postfix databases / lookup tables with python
View read_postfix_db.py
#!/usr/bin/env python
from bsddb3 import db
filename = "/var/spool/postfix/plesk/virtual.db"
target = "smtp:[backend_plesk_server]:25"
postfix_db = db.DB()
postfix_db.open(filename, None, db.DB_HASH, db.DB_DIRTY_READ)
@lanbugs
lanbugs / transport_generator.py
Created Jul 5, 2018
Export email aliases and domains from plesk backend database to build own transport map for Postfix
View transport_generator.py
#!/usr/bin/env python
import mysql.connector
cnx = mysql.connector.connect(user='psa_readonly', password='securepassword', host='127.0.0.1', database='psa')
target = "smtp:[plesk_beckend_server.xxxx.xxx]:25"
buffer = ""
@lanbugs
lanbugs / flask_write_put_to_file.py
Created Jun 27, 2018
Backup Cisco configuration by write event to external server - Example for Flask
View flask_write_put_to_file.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# write config to file with cisco archive command
# see more in blog article: https://lanbugs.de/netzwerktechnik/hersteller/cisco/cisco-python-backup-der-konfiguration-bei-write-event-auf-externen-server/
from flask import Flask
from flask import request
app = Flask(__name__)
@lanbugs
lanbugs / cisco_inventory.py
Created Jun 26, 2018
Commandline Tool for exporting Cisco Hardware Inventory via SNMP
View cisco_inventory.py
#!/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 Jun 22, 2018
Search in file and get line number with python
View search_linenumber.py
#!/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 Jun 22, 2018
Search and replace in file with python 2.x
View search_and_replace_p2.py
#!/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 Jun 22, 2018
Search and replace in files with python3
View search_and_replace_p3.py
#!/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 Jun 22, 2018
Calculate age of file with python
View age_of_file.py
#!/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 Jun 22, 2018
Repair damaged UTF-8 string in python
View damaged_utf8.py
#!/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 Jun 22, 2018
Multiprocessing with result in python
View multiprocessing_with_result.py
#!/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
You can’t perform that action at this time.