Skip to content

Instantly share code, notes, and snippets.

@angely-dev
angely-dev / README.md
Last active April 20, 2024 17:14
Update napalm-huawei-vrp README

Quick start

(…)

Diff mechanism

The driver supports two diff modes:

  • non-contextual diff (default behavior)
  • contextual diff (as proposed in PR #23)
@angely-dev
angely-dev / loggedroute.py
Last active April 27, 2024 12:02
FastAPI log every request and response
from fastapi import Request, Response, HTTPException
from fastapi.exceptions import RequestValidationError
from fastapi.routing import APIRoute
from http.client import responses
from json import JSONDecodeError
from starlette.exceptions import HTTPException as StarletteHTTPException
from traceback import format_exc
from typing import Callable
import logging
@angely-dev
angely-dev / tree.py
Last active May 16, 2023 07:22
Indented text to tree in Python (n-ary tree)
from json import JSONEncoder
#
# Node class: it just consists of a name and a list of children.
#
class Node:
def __init__(self, name: str):
self.name = name
self.children = []
@angely-dev
angely-dev / .htaccess
Created October 7, 2022 16:57
.htaccess redirect http2https and nonwww2www (meeting HSTS requirements)
#
# As required by HSTS, two redirects:
# 1) http2https (MUST be first)
# 2) nonwww2www
#
# https://webmasters.stackexchange.com/questions/84757/htaccess-redirect-non-www-to-www-with-ssl-https
#
RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^www\.
@angely-dev
angely-dev / ssh-autocomplete
Last active October 10, 2022 15:26
Autocomplete SSH, SCP, ping, ..., based on known hosts.
#!/usr/bin/env bash
get_ssh_known_hosts() {
known_hosts=$(cat ~/.ssh/known_hosts | cut -d' ' -f1)
starts_with=$2
COMPREPLY=( $(compgen -W "$known_hosts" -- "$starts_with") )
return 0
}
complete -F get_ssh_known_hosts ssh scp ping nslookup my-custom-command
@angely-dev
angely-dev / freeradius-translate-attributes
Last active October 14, 2021 12:49
Translate RADIUS attributes from one vendor (say, Cisco) to another one (say, Huawei), using FreeRADIUS and a custom module (UNLANG).
#
# /etc/freeradius/policy.d/cisco2huawei
#
# Attributes adaptation from Cisco to Huawei.
#
cisco2huawei {
foreach &reply:Cisco-AVPair {
if ("%{Foreach-Variable-0}" =~ /ip:vrf-id=(.*)/) {
#
# Adapt the VRF attribute.
@angely-dev
angely-dev / freeradius-lns-load-balancing
Last active October 19, 2022 20:56
Load balancing between LNS (L2TP), using FreeRADIUS and a custom module (UNLANG).
#
# /etc/freeradius/policy.d/lns_load_balancing
#
# Random load balancing between two LNS.
# Note: the same LNS can be returned twice in a row.
# If it's an issue, feel free to use a non-binary rand interval (e.g., rand:9 instead of rand:2)
# and adapt below conditions (e.g., 0..4 is first LNS, 5..9 is second LNS).
#
lns_load_balancing {
#
@angely-dev
angely-dev / freeradius-force-accept
Created October 14, 2021 12:22
Force accept a RADIUS user, using FreeRADIUS and a custom module (UNLANG).
#
# /etc/freeradius/policy.d/force_accept
#
# Force accept a user.
#
# Two scenarios:
# - the user does NOT exist in the database => trigger FORCE-ACCEPT-USERNAME-NOT-FOUND
# - the user does exist but password is incorrect => trigger FORCE-ACCEPT-PASSWORD-INCORRECT
#
# This can be implemented with two modules, called at different sections in the FreeRADIUS sequence.
@angely-dev
angely-dev / logger-exithandler.py
Last active September 3, 2021 09:25
Python logging exit on error or critical levels
import logging
import logging.handlers
import sys
#
# The logger and the logging level
#
logger = logging.getLogger('mylogger')
logger.setLevel(logging.DEBUG)
@angely-dev
angely-dev / logger-username.py
Last active September 2, 2021 15:13
Python logging current username
import coloredlogs # OPTIONAL (see end of the file)
import getpass
import logging
import logging.handlers
#
# The logger and the logging level
#
logger = logging.getLogger('mylogger')
logger.setLevel(logging.DEBUG)