Skip to content

Instantly share code, notes, and snippets.

View andr1an's full-sized avatar
🐡

Sergey Andrianov andr1an

🐡
View GitHub Profile
"""
Safe way to call subprocess.Popen.communicate() and str from it.
"""
def shell(exe, *args, **kwargs):
if sys.version_info >= (3, 6) and 'encoding' not in kwargs: # for Python >=3.6
kwargs['encoding'] = 'utf8'
args = [exe] + list(args)
proc = subprocess.Popen(args, stdout=subprocess.PIPE)
out, _ = proc.communicate()
import re
_RE_HMS = re.compile(
r'^\s*(?:(?P<h>\d+)h)?\s*(?:(?P<m>\d+)m)?\s*(?:(?P<s>\d+)s?)?\s*$')
def to_hms(seconds):
"""Converts seconds (int) to "h m s" (str)."""
minutes, seconds = divmod(seconds, 60)
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Dumps Elasticsearch index; handy for backing up Kibana."""
import sys
import argparse
import json
import logging
import requests
@andr1an
andr1an / gist:8a80a9da0b008e645649b6f8d59383ae
Created November 20, 2018 09:21 — forked from jstrosch/gist:3190568
iptables - delete all rules/chains
#view current chains
$ iptables -L
#remove/flush all rules & delete chains
$ iptables -F
$ iptables -X
$ iptables -t nat -F
$ iptables -t nat -X
$ iptables -t mangle -F
$ iptables -t mangle -X
@andr1an
andr1an / parallel_ssh
Last active February 1, 2018 08:48
Parallel SSH commands execution in pure Bash
#!/usr/bin/env bash
#
# Example of parallel SSH execution with colorized hostnames.
# Requires a 256 color-compatible terminal.
#
set -u
declare -a SERVERS=(
my.server.001
@andr1an
andr1an / redis_cache.py
Last active September 8, 2017 20:28
Python example of how to use Redis for caching
"""Library to use Redis for the cache.
Usage:
from redis_cache import redis_cached
@redis_cached
def foo(bar, baz, misc=123):
return bar + baz + misc
"""
@andr1an
andr1an / instance_lock.py
Last active September 11, 2018 11:54
Python: single instance locking for your scripts
"""Single instance locking for your scripts.
Usage:
from instance_lock import lock_instance
def i_am_busy():
print "I'm busy!"
with lock_instance(locked_callback=i_am_busy):
do_something_long()
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Пример декоратора.
Декоратор - функция, которая принимает функцию, как аргумент, и подменяет
её имплементацию другой функцией.
Например, @cached заворачивает свою функцию-аргумент в функцию try_cache,
которая перед возвращением значения будет класть его в кеш для ускорения
повторного вызова.
@andr1an
andr1an / check_spf.sh
Last active July 11, 2021 05:08
Checks SPF records for recursion level
#!/bin/bash
#
# Checks SPF records for recursion level
# (see https://tools.ietf.org/html/rfc7208#section-4.6.4)
#
check_domain="${1:?Specify a domain to check!}"
check_record() {
local domain="$1"