Skip to content

Instantly share code, notes, and snippets.

@yunazuno
yunazuno / ethtool.py
Last active March 12, 2024 10:51
ethtool -S in Python
#!/usr/bin/env python
import socket
import fcntl
import struct
import array
SIOCETHTOOL = 0x8946
ETHTOOL_GSTRINGS = 0x0000001b
ETHTOOL_GSSET_INFO = 0x00000037
ETHTOOL_GSTATS = 0x0000001d
@yunazuno
yunazuno / drop_ipv4_tcp_80.c
Last active November 2, 2023 09:21
Testing a XDP program with BPF_PROG_TEST_RUN through Python
#include <uapi/linux/if_ether.h>
#include <uapi/linux/in.h>
#include <uapi/linux/ip.h>
#include <uapi/linux/tcp.h>
int drop_ipv4_tcp_80(struct xdp_md *ctx) {
void *data = (void *)(long)ctx->data;
void *data_end = (void *)(long)ctx->data_end;
struct ethhdr *eth;
@yunazuno
yunazuno / dns64.py
Created September 2, 2015 05:29
A DNS64 resolver with AAAA filter-like behavior (similar to the Internet Shareing on OSX El Capitan)
#!/usr/bin/env python2
from twisted.internet import reactor
from twisted.names import client, dns, server
import ipaddr
import socket
class SixFourResolver(client.Resolver):
def __init__(
self,
import hashlib
class MaglevHash(object):
def __init__(self, m=65537):
self.m = m
self.permutation = {}
self.entry = [None] * self.m
def add(self, node):
@yunazuno
yunazuno / drop_80_8080.c
Last active August 26, 2022 15:15
XDP: drop 80/tcp and 8080/tcp
#define KBUILD_MODNAME "xdp_drop_80_8080"
#include <uapi/linux/bpf.h>
#include <linux/in.h>
#include <linux/if_ether.h>
#include <linux/if_packet.h>
#include <linux/if_vlan.h>
#include <linux/ip.h>
#include <linux/ipv6.h>
BPF_HASH(counter, uint32_t, long);
@yunazuno
yunazuno / ts_delete_devices.sh
Created April 15, 2022 10:21
Delete all devices on the Tailnet except for no expiry devices
#!/bin/bash
# Delete all devices on the Tailnet except for no expiry devices
# Released under the MIT license
set -eo pipefail
TAILSCALE_TAILNET=${TAILSCALE_TAILNET:-}
TAILSCALE_API_KEY=${TAILSCALE_API_KEY:-}
SCRIPT_REAL_RUN=${SCRIPT_REAL_RUN:false}
if [ -z "${TAILSCALE_API_KEY}" ]; then
@yunazuno
yunazuno / uridecodebin.py
Created March 2, 2011 04:56
Audio autoplugin example using 'uridecodebin' element
#!/usr/bin/env python
# uridecodebin.py - Audio autoplugin example using 'uridecodebin' element
#
# Based on:
# decodebin.py - Audio autopluging example using 'decodebin' element
# Copyright (C) 2006 Jason Gerard DeRose <jderose@jasonderose.org>
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Library General Public
@yunazuno
yunazuno / skk.py
Created January 18, 2012 06:44
Python port of libskk/tools/tool.vala (prototype)
#!/usr/bin/python
# -*- coding: utf-8 -*-
from gi.repository import Skk
import sys
Skk.init()
dictionaries = []
dictionary = Skk.FileDict.new("/usr/share/skk/SKK-JISYO.L", "EUC-JP")
dictionaries.append(dictionary)
@yunazuno
yunazuno / tcl3switch.py
Created July 8, 2018 07:40
PoC: Offloading L3 FIB into a NIC hardware through tc
#!/usr/bin/env python3
import pyroute2
import socket
from pyroute2.netlink import rtnl
import subprocess
import time
from operator import itemgetter
class TCL3Switch(object):
@yunazuno
yunazuno / description.md
Last active October 27, 2017 11:42
In Consul v0.8.4 or later, `consul watch -type event HANDLER` gives same events several time to the `HANDLER`.

Summary

In Consul v0.8.4 or later, consul watch -type event HANDLER gives same events several time to the HANDLER.

Reproduction steps

  1. Invoke consul watch -type event HANDLER to wait events
  2. Invoke consul event -name "foo" several time to fire events

Expected result