Skip to content

Instantly share code, notes, and snippets.

View sbz's full-sized avatar

Sofian Brabez sbz

View GitHub Profile
@sbz
sbz / pyflame.sh
Last active May 30, 2018 14:17
Run pyflame against your python code, to profile it and generate a flamegraph (here it's against singularity-monitor)
#!/bin/bash
sudo apt-get install -y autoconf automake autotools-dev g++ pkg-config python-dev python3-dev libtool make
[ ! -f ./flamegraph.pl ] && {
curl -O https://raw.githubusercontent.com/brendangregg/FlameGraph/master/flamegraph.pl
}
[ ! -f /usr/local/bin/pyflame -o ! -d pyflame ] && {
git clone https://github.com/uber/pyflame.git
cd pyflame
@sbz
sbz / pf_getrules.c
Last active May 25, 2018 16:11
pf: ioctl get rules call debugging
#include <sys/cdefs.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#ifdef __FreeBSD__
#include <sys/endian.h>
#endif
#include <net/if.h>
#include <net/pfvar.h>
@sbz
sbz / mz-tab-urls.py
Created February 4, 2015 21:57
mz-tab-urls: Get Mozilla Firefox tabs URLs on stdout
#!/usr/bin/env python
import ConfigParser
import json
import os
"""
Dirty script to get Mozilla Firefox tabs URLs on stdout
source: https://raymii.org/s/snippets/Get_the_current_or_all_Firefox_tab_urls_in_Bash.html
@sbz
sbz / tty-size.py
Last active September 27, 2017 21:39
get terminal size using ioctl get win size (TIOCGWINSZ) in Python
import termios
import fcntl
import os
import struct
with open(os.ctermid(), 'r') as fd:
packed = fcntl.ioctl(fd, termios.TIOCGWINSZ, struct.pack('HHHH', 0, 0, 0, 0))
rows, cols, h_pixels, v_pixels = struct.unpack('HHHH', packed)
print rows, cols, h_pixels, v_pixels
@sbz
sbz / logit.py
Created July 13, 2011 14:43
generic logit method in order to log functions calling with arguments
import inspect
import logging
logger = logging.getLogger('application')
def logit(order, *largs, **kwargs):
return True
__line__ = int(inspect.stack()[1][2])
__function__ = inspect.stack()[1][3]
with_color=False
@sbz
sbz / stackgrow.c
Created July 13, 2011 13:11
test how system stack is growing
#include <stdio.h>
#include <sys/utsname.h>
inline static void
u() {
struct utsname uts;
uname(&uts);
printf("%s %s %s %s %s", uts.sysname, uts.nodename, uts.release, uts.version, uts.machine);
@sbz
sbz / xsock.py
Created July 13, 2011 14:05
create a unix socket on X11 display in Python
#!/usr/bin/env python
import time
import socket
_PATH_UNIX_X = "/tmp/.X11-unix/X%d"
DISPLAY_PORT = 0
s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
s.connect(_PATH_UNIX_X % DISPLAY_PORT)
@sbz
sbz / addr.c
Created July 13, 2011 12:53
retrieve INET4 interfaces address using getifaddrs(3)
#include <stdio.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <ifaddrs.h>
#include <net/if.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/types.h>
int
@sbz
sbz / retrieve-unassigned-objects.py
Created November 18, 2010 12:40
retrieve unassigned objects using gc module in python
import socket
socket.create_connection(('127.0.0.1', 22))
<socket._socketobject object at 0x9022bc4>
s=socket.create_connection(('127.0.0.1', 22))
import gc
[ i for i in gc.get_objects() if type(i) == type(s) ]
[<socket._socketobject object at 0x9022bc4>, <socket._socketobject object at 0x92a4aac>]
t = [ i for i in gc.get_objects() if type(i) == type(s) ][0]
t
<socket._socketobject object at 0x9022bc4>
@sbz
sbz / freebsd.json
Created December 19, 2014 00:37
FreeBSD config file (base, localbase, kernel?, ...) backup as json in order to redeploy/backup config
{
"@base": {
"files": [
{
"make.conf": {
"filename": "make.conf",
"dirpath": "/etc/",
"@destination": "/etc/make.conf",
"@content": {
"svn_update": "yes",