Code Clip

Shell

nftables

List set elements

nft -j list set inet filter https  | \
    python3 -c "import sys,json;
    print(
      '\n'.join([
        i['elem']['val']
          for i in json.load(sys.stdin)['nftables'][1]['set']['elem']
      ]))"

VPS OS reinstall

Debian

wget --no-check-certificate 'https://raw.githubusercontent.com/veip007/dd/master/InstallNET.sh' 
chmod +x ./InstallNET.sh 
./InstallNET.sh -d 10 -v 64 -a \
                --ip-addr 144.76.229.245 \
                --ip-gate 144.76.229.1 \
                --ip-mask 255.255.255.0 \
                -p "kBrboUMta9aQhSm9" \
                --mirror 'http://deb.debian.org/debian/'

Arch

https://github.com/felixonmars/vps2arch

wget https://felixc.at/vps2arch
chmod +x vps2arch
./vps2arch

git workflow

patch

create:

git diff > _patch_file.patch

apply with git command

git apply patch_file.patch 

C

bpf

get thread group leader commname

char commname[16];
struct task_struct *gp_task;
bpf_probe_read_kernel(&gp_task, sizeof(void*),  &((struct task_struct *)bpf_get_current_task())->group_leader);
bpf_probe_read_kernel_str(&commname, 16, gp_task->comm);

Golang

pprof

Example code

package main

import  (
        "log"
        "fmt"
        "net/http"
        _ "net/http/pprof"
)
func init(){
        go func() {
                log.Println(http.ListenAndServe(":6060", nil))
        }()
}

func main(){
        fmt.Println("hello world")
}

Generate profile file

From curl

curl -o profile.out http://127.0.0.1:6060/debug/pprof/profile?seconds=30

From go test:

go test -cpuprofile cpu.out # [-blockprofile block.out] [-memprofile mem.out] [-mutexprofile mutex.out] [-trace trace.out]

From code:

import  (
        "runtime/pprof"
)

func TestDemo(){
        f, _ := os.Create("demo-cpu.pprof")
        defer f.Close()
        _ = pprof.StartCPUProfile(f)
        defer pprof.StopCPUProfile()
}


View

go tool pprof profile.out

Python

Sysadm scripts

pty

stackoverflow - PTY Termianl Resize

import termios
import struct
import fcntl

def set_winsize(fd, row, col, xpix=0, ypix=0):
        winsize = struct.pack("HHHH", row, col, xpix, ypix)
        fcntl.ioctl(fd, termios.TIOCSWINSZ, winsize)

Author: Lindsay Zhou

Created: 2023-10-20 Fri 13:42