Skip to content

Instantly share code, notes, and snippets.

@Jamesits
Jamesits / asa_request_cert.sh
Last active May 6, 2024 08:30
Let's Encrypt cert requesting and signing (using acme.sh) for Cisco ASA / AnyConnect
#!/bin/bash
set -Eeuo pipefail
# Automatic cert requesting and signing for Cisco ASA
#
# Requirements
# - Domain hosted in Aliyun DNS (for other DNS services, adjust the acme.sh arguments)
# - ASA with rest-agent enabled
# - Local computer: openssl, git, ca-certificates
#
@Jamesits
Jamesits / postgresql.tf
Created April 18, 2024 08:15
Auto set max_connections on Azure PostgreSQL Flexible Server with Terraform
terraform {
required_providers {
azurerm = {
source = "hashicorp/azurerm"
}
}
}
variable "sku_name" {
type = string
@Jamesits
Jamesits / pghero.sql
Last active April 11, 2024 08:24
The missing database migration for PgHero, with permission isolation setup (separate schema for PgHero, read only access for the application schema)
-- database
CREATE DATABASE "app" WITH OWNER = "azure_pg_admin";
\c app
-- app user (just as an example)
CREATE ROLE "appuser" LOGIN ENCRYPTED PASSWORD '114514' ADMIN azure_pg_admin;
GRANT ALL PRIVILEGES ON DATABASE "app" TO "appuser";
GRANT ALL ON SCHEMA public TO "appuser";
-- PgHero user
@Jamesits
Jamesits / caddy.sh
Last active January 27, 2024 14:47
Install Caddy Server on Ubuntu with Systemd.
# Should work on all Debian based distros with systemd; tested on Ubuntu 16.04+.
# This will by default install all plugins; you can customize this behavior on line 6. Selecting too many plugins can cause issues when downloading.
# Run as root (or sudo before every line) please. Note this is not designed to be run automatically; I recommend executing this line by line.
apt install curl
curl https://getcaddy.com | bash -s personal dns,docker,dyndns,hook.service,http.authz,http.awses,http.awslambda,http.cache,http.cgi,http.cors,http.datadog,http.expires,http.filemanager,http.filter,http.forwardproxy,http.geoip,http.git,http.gopkg,http.grpc,http.hugo,http.ipfilter,http.jekyll,http.jwt,http.locale,http.login,http.mailout,http.minify,http.nobots,http.prometheus,http.proxyprotocol,http.ratelimit,http.realip,http.reauth,http.restic,http.upload,http.webdav,net,tls.dns.auroradns,tls.dns.azure,tls.dns.cloudflare,tls.dns.cloudxns,tls.dns.digitalocean,tls.dns.dnsimple,tls.dns.dnsmadeeasy,tls.dns.dnspod,tls.dns.dyn,tls.
@Jamesits
Jamesits / docker-wrapper.sh
Created September 3, 2021 08:02
Run Docker in 2021 - supports Docker & Podman in root or rootless mode.
#!/bin/bash
set -Eeuo pipefail
DOCKER_IMAGE="${DOCKER_IMAGE:-hello-world}"
DOCKER_FLAGS="${DOCKER_FLAGS:-}"
function __docker() {
if command -v docker >/dev/null; then
docker "$@"
elif command -v podman >/dev/null; then
podman "$@"
@Jamesits
Jamesits / mtr_lineprotocol.sh
Last active September 4, 2023 07:32
Convert MTR CSV output to InfluxDB line protocol
#!/bin/bash
# Converts MTR result to InfluxDB line protocol.
# Intended to be run from telegraf like this:
#
# [[inputs.exec]]
# commands = ["/path/to/your/mtr_lineprotocol.sh"]
# interval = "120s"
# timeout = "120s"
# data_format = "influx"
# Memory usage
# https://blog.cloudflare.com/the-story-of-one-latency-spike/
# https://cloud.google.com/architecture/tcp-optimization-for-network-performance-in-gcp-and-hybrid/
# https://zhensheng.im/2021/01/31/linux-wmem-and-rmem-adjustments.meow
# https://github.com/redhat-performance/tuned/blob/master/profiles/network-throughput/tuned.conf
# ReceiveBuffer: X - (X / (2 ^ tcp_adv_win_scale)) = RTT * Bandwidth / 8
# SendBuffer: RTT * Bandwidth / 8 * 0.7
net.core.netdev_max_backlog = 262144
net.ipv4.tcp_max_syn_backlog = 8192
net.core.rmem_max = 67108864
@Jamesits
Jamesits / high-frequency-square-wave-generator-esp8266.ino
Last active August 14, 2023 23:01
High-accuracy square wave generator (up to 250KHz) based on ESP8266, with runtime adjustable frequency, PWM width and offset.
// High-accuracy square wave generator
// based on ESP8266
// with runtime adjustable frequency, PWM width and offset
// Output wave at pin 5 (configurable from 0 to 15, but not 16)
// by James Swineson <github@public.swineson.me>, 2017-10
// https://gist.github.com/Jamesits/92394675c0fe786467b26f90e95d3904
// See https://blog.swineson.me/implementation-of-6mbps-high-speed-io-on-esp8266-arduino-ide/
// for more information (article in Chinese)
// Arduino UNO version: https://gist.github.com/Jamesits/8d164818946a65d0cafcd6203e3e5049
@Jamesits
Jamesits / NPS-Config-Sync.ps1
Created December 21, 2019 14:00
Sync 2 Network Policy Servers' configuration
###Network Policy Server Synchronization Script
#This script copies the configuration from the NPS Master Server and imports it on the secondary server.
#The Account that this script runs under must have Local Administrator rights to the NPS Master.
#This was designed to be run as a scheduled task on the NPS Secondary Servers on an hourly,daily, or as-needed basis.
# Modified from https://deployhappiness.com/two-network-policy-server-tricks-subnets-and-syncing/
###Variables
#NPSMaster - Your Primary Network Policy Server you want to copy the config from.
$NPSMaster = "adds0.corp.contoso.com"
$NPSSecondary = "adds1.corp.contoso.com"
@Jamesits
Jamesits / grafana-latency-loss-graph.md
Last active July 9, 2023 04:42
Latency/packet loss graph demo for InfluxDB 2 (Flux Query Language) + Grafana

Grafana dashboard setup:

  • Query options
    • Max data points: 600 if your data collection interval is 1min, adjust on your needs
  • Visualization: Graph
  • Display:
    • Bars: off
    • Lines: off
    • Points: off
  • Series overrides