Skip to content

Instantly share code, notes, and snippets.

View nacx's full-sized avatar

Ignasi Barrera nacx

View GitHub Profile
@nacx
nacx / inverse-cidr.py
Created October 10, 2014 08:12
Compute the inverse list of CIDR blocks
"""Use it like this: main('192.168.1.0/24')"""
IPV4_MIN = 0
IPV4_MAX = 0xFFFFFFFF
def not_network(ipv4_address, ipv4_netmask):
assert IPV4_MIN <= ipv4_address <= IPV4_MAX
assert IPV4_MIN <= ipv4_netmask <= IPV4_MAX
def hostmask_netmask(m):
@nacx
nacx / Overlap.java
Last active March 22, 2024 17:32
Network overlapping check
private static boolean overlap(final String net1, final String net2)
{
SubnetInfo subnet1 = new SubnetUtils(net1).getInfo();
SubnetInfo subnet2 = new SubnetUtils(net2).getInfo();
int mask1 = subnet1.asInteger(subnet1.getNetmask());
int mask2 = subnet2.asInteger(subnet2.getNetmask());
int maskToUse = mask1 < mask2 ? mask1 : mask2;
int addr1 = subnet1.asInteger(subnet1.getAddress()) & maskToUse;
@nacx
nacx / __init__.py
Created May 2, 2012 14:15
Pluggable command line tool
import os
# Automatically set the __all__ variable with all
# the available plugins.
plugin_dir = "plugins"
__all__ = []
for filename in os.listdir(plugin_dir):
filename = plugin_dir + "/" + filename
@nacx
nacx / go_cpu_memory_profiling_benchmarks.sh
Created May 25, 2021 07:57 — forked from arsham/go_cpu_memory_profiling_benchmarks.sh
Go cpu and memory profiling benchmarks. #golang #benchmark
go test -run=. -bench=. -benchtime=5s -count 5 -benchmem -cpuprofile=cpu.out -memprofile=mem.out -trace=trace.out ./package | tee bench.txt
go tool pprof -http :8080 cpu.out
go tool pprof -http :8081 mem.out
go tool trace trace.out
go tool pprof $FILENAME.test cpu.out
# (pprof) list <func name>
# go get -u golang.org/x/perf/cmd/benchstat
benchstat bench.txt
@nacx
nacx / Simple NexentaStor information retrieval
Created March 10, 2011 15:38
Simple NexentaStor information retrieval python script
#!/usr/bin/env python
import sys
import NZA
def print_volumes_by_pool(pool):
volumes = NZA.nms.zvol.get_names_by_prop('volume_name', pool, '')
for volume in volumes:
name = NZA.nms.zvol.get_child_prop(volume, 'name')
size = NZA.nms.zvol.get_child_prop(volume, 'size')
@nacx
nacx / cleanup.py
Created June 16, 2011 11:00
Cleanup a NexentaStor device
#!/usr/bin/env python
import sys
import NZA
def delete_volumes_in_pool(pool):
print "Removing volumes in pool %s..." % pool
volumes = NZA.nms.zvol.get_names_by_prop('volume_name', pool, '')
for volume in volumes:
try:
@nacx
nacx / Personal firewall
Created April 1, 2011 12:23
Iptables firewall for a personal computer
#!/bin/bash
#
# This is an example iptables script to configure
# a firewall for a personal machine.
#
# It filters bad packets and bans the desired IP
# addresses, and allows remote connections to a
# controlled set of ports.
#
@nacx
nacx / oauth.py
Last active December 20, 2018 08:49
Example webapp that uses OAuth to access the Abiquo API
#!/usr/bin/env python
from flask import Flask, redirect, request, session, url_for
from functools import wraps
from requests_oauthlib import OAuth1Session
import json
import os
# Application tokens (returned when the user registers an app)
@nacx
nacx / BeanLoader.java
Created June 23, 2011 08:58
Spring static Bean loader to load beans outside the Servlet or Sprign context
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Service;
@Service
public final class BeanLoader implements ApplicationContextAware
{
private static BeanLoader instance;
private ApplicationContext applicationContext;
@nacx
nacx / qos-opendpi.sh
Created December 8, 2010 11:47
Basic QoS with iproute2 and OpenDPI Netfilter wrapper
#!/bin/bash
# Basic QoS script that uses OpenDPI (http://www.opendpi.org/)
# Netfilter wrapper to identify packets by the protocol.
#
# This script enqueues packets in three queues, each one
# with a different priority:
#
# The first queue has the higher priority and gets the TCP SYN
# packets, ACKs, ICMPs and packets with Minimize-Delay TOS.