Skip to content

Instantly share code, notes, and snippets.

View lanbugs's full-sized avatar

Maximilian Thoma lanbugs

View GitHub Profile
@lanbugs
lanbugs / get_subnets_of_spf_record_mynetwoks.py
Created June 22, 2018 22:03
Resolve all known ip addresses from spf record and generate cidr map for postfix
#!/usr/bin/env python
#
# get_subnets_of_spf_record_mynetwoks.py
# Resolve all known ip addresses from spf record and generate cidr map for postfix
#
# Version 1.0
# Written by Maximilian Thoma (http://www.lanbugs.de)
#
# Checkout blog article:
@lanbugs
lanbugs / check_dnspl.py
Created June 22, 2018 22:07
Check mailserver blacklists for Nagios / Check_MK
#!/usr/bin/env python
# -*- encoding: utf-8; py-indent-offset: 4 -*-
#
# check_dnspl.py - Check IP against Blacklist
# Use it on your own risk!
#
# Written 2017 - Maximilian Thoma
#
# This program is free software; you can redistribute it and/or modify it under the terms of the GNU
@lanbugs
lanbugs / add_ip_to_host.py
Created June 22, 2018 22:10
Check_MK - Tool to add static ip to hosts where only DNS name exists
#!/usr/bin/env python
#
# Tool to add static ip to hosts where only DNS name exists
# Written by Maximilian Thoma 2017
# http://lanbugs.de
#
#
# This program is free software; you can redistribute it and/or modify it under
@lanbugs
lanbugs / getadsmtp.py
Created June 22, 2018 22:13
Generates email alias list from active directory for postfix
#!/usr/bin/python
# getadsmtp.py
# Written by Maximilian Thoma 2016
# Version 1.0
# The script is an translation from the orginal perl script getadsmtp.pl
# This script will pull all users' SMTP addresses from your Active Directory
# (including primary and secondary email addresses) and list them in the
# format "user@example.com OK" which Postfix uses with relay_recipient_maps.
@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 / 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 / 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 / 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 / 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 / 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()