Skip to content

Instantly share code, notes, and snippets.

View sbz's full-sized avatar

Sofian Brabez sbz

View GitHub Profile
@sbz
sbz / lcap.c
Last active January 26, 2024 14:33
example of using linux capabilities interface libcap(3) and dump capabilities flags for the running process
#include <sys/capability.h>
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#define nitems(x) (sizeof(x) / sizeof(x[0]))
int
main(void) {
@sbz
sbz / tty-size.c
Created April 14, 2017 02:05
get terminal size using ioctl get win size (TIOCGWINSZ) in C
#include <sys/ioctl.h>
#include <stdio.h>
#include <unistd.h>
int
main(void) {
struct winsize ws;
ioctl(STDIN_FILENO, TIOCGWINSZ, &ws);
printf ("lines %d\n", ws.ws_row);
@sbz
sbz / ansible-filter-fact-with-jq.sh
Last active April 17, 2023 20:51
Get FreeBSD ansible facts using jq script function to pick selected facts
$ cat <<EOF > reduce.jq
def pick($paths):
. as $root | reduce $paths[] as $path ({}; . + { ($path): $root[$path] });
.ansible_facts | pick(["ansible_kernel", "ansible_distribution", "ansible_kernel_version"])
EOF
$ ansible -m setup localhost | sed '1 s/^.*|.*=>.*$/{/g' | jq -f reduce.jq
{
"ansible_kernel": "13.1-RELEASE-p3",
@sbz
sbz / xsock.c
Created July 13, 2011 13:53
create a unix socket on X11 display in C
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/un.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#define _PATH_UNIX_X "/tmp/.X11-unix/X%d"
#define DISPLAY_PORT 0
@sbz
sbz / hexdump.py
Created July 13, 2011 13:04
hexdump implementation in Python
def hexdump(src, length=16):
FILTER = ''.join([(len(repr(chr(x))) == 3) and chr(x) or '.' for x in range(256)])
lines = []
for c in xrange(0, len(src), length):
chars = src[c:c+length]
hex = ' '.join(["%02x" % ord(x) for x in chars])
printable = ''.join(["%s" % ((ord(x) <= 127 and FILTER[ord(x)]) or '.') for x in chars])
lines.append("%04x %-*s %s\n" % (c, length*3, hex, printable))
return ''.join(lines)
@sbz
sbz / list-sections.c
Created August 31, 2012 10:42
example to dump sections name using gelf(3) api on FreeBSD
#include <err.h>
#include <fcntl.h>
#include <gelf.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <sysexits.h>
#include <unistd.h>
static void
@sbz
sbz / gist:3c23c52d6817246b3682922944a9c3ba
Created March 25, 2021 13:32
crowdsec patch uuid for BSD systems
diff --git c/cmd/crowdsec-cli/machines.go w/cmd/crowdsec-cli/machines.go
index bbdf11c..33e87e9 100644
--- c/cmd/crowdsec-cli/machines.go
+++ w/cmd/crowdsec-cli/machines.go
@@ -7,6 +7,7 @@ import (
"io/ioutil"
"math/big"
"os"
+ "runtime"
"strings"
@sbz
sbz / gitlab.py
Created April 23, 2014 09:10
gitlab: list your groups and projects using api v3
#!/usr/bin/env python
import os
import requests
import subprocess
import sys
from pprint import pprint as pp
TOKEN = 'YOUR_TOKEN'
@sbz
sbz / tf-init.sh
Last active August 19, 2019 15:19
Initialize terraform environment
#!/bin/bash
set -o errexit
set -o nounset
set -o pipefail
readonly TF_VERSION="v0.11"
output_line() {
@sbz
sbz / aws_route53_detection.py
Last active May 7, 2019 10:30
Detect useless ResourceRecord with type 'A' on AWS R53 pointing to private IP which doesn't exist in the region anymore
#!/usr/bin/env python3
import ipaddress
import boto3
from pprint import pprint as pp
#
# This script iterate over our regions in order to collect all the private IPs
# and check if the ResourceRecord created has still an existing IPs