Skip to content

SysAdmin

This page has useful snippets for sysadmin tasks, shell scripting, and other related things used frequently(and unfrequently) for things like managing servers, firewalls, networking, monitoring and so on.

CPU Usage w/ no external tools

This snippet(s) gets the CPU usage of the system using /proc/stat over the course of 0.5 seconds. this can be changed to 1 second for a more accurate reading.

CPU Usage
prev_idle=$(awk '/^cpu /{print $5}' /proc/stat)
prev_total=$(awk '/^cpu /{s=0; for(i=2;i<=NF;i++) s+=$i; print s}' /proc/stat)
sleep 0.5
idle=$(awk '/^cpu /{print $5}' /proc/stat)
total=$(awk '/^cpu /{s=0; for(i=2;i<=NF;i++) s+=$i; print s}' /proc/stat)
diff_idle=$((idle - prev_idle))
diff_total=$((total - prev_total))
usage=$(echo "scale=1; 100 * ($diff_total - $diff_idle) / $diff_total" | bc -l)
echo "$usage"

Same command but in a single line:

CPU Usage One-liner
prev_idle=$(awk '/^cpu /{print $5}' /proc/stat); prev_total=$(awk '/^cpu /{s=0; for(i=2;i<=NF;i++) s+=$i; print s}' /proc/stat); sleep 0.5; idle=$(awk '/^cpu /{print $5}' /proc/stat); total=$(awk '/^cpu /{s=0; for(i=2;i<=NF;i++) s+=$i; print s}' /proc/stat); diff_idle=$((idle - prev_idle)); diff_total=$((total - prev_total)); usage=$(echo "scale=1; 100 * ($diff_total - $diff_idle) / $diff_total" | bc -l); echo "$usage"

Every Second

CPU Usage Every Second
while true
do
prev_idle=$(awk '/^cpu /{print $5}' /proc/stat)
prev_total=$(awk '/^cpu /{s=0; for(i=2;i<=NF;i++) s+=$i; print s}' /proc/stat)
sleep 0.5
idle=$(awk '/^cpu /{print $5}' /proc/stat)
total=$(awk '/^cpu /{s=0; for(i=2;i<=NF;i++) s+=$i; print s}' /proc/stat)
diff_idle=$((idle - prev_idle))
diff_total=$((total - prev_total))
usage=$(echo "scale=1; 100 * ($diff_total - $diff_idle) / $diff_total" | bc -l)
echo "$usage%"
done

One liner for the above:

CPU Usage Every Second One-liner
while true; do prev_idle=$(awk '/^cpu /{print $5}' /proc/stat); prev_total=$(awk '/^cpu /{s=0; for(i=2;i<=NF;i++) s+=$i; print s}' /proc/stat); sleep 0.5; idle=$(awk '/^cpu /{print $5}' /proc/stat); total=$(awk '/^cpu /{s=0; for(i=2;i<=NF;i++) s+=$i; print s}' /proc/stat); diff_idle=$((idle - prev_idle)); diff_total=$((total - prev_total)); usage=$(echo "scale=1; 100 * ($diff_total - $diff_idle) / $diff_total" | bc -l); echo "$usage%"; done

Alternative command to above

Does the same thing but differently

CPU Usage Alternative
_STAT_PREV_IDLE=$(awk '/^cpu /{print $5}' /proc/stat)
prev_total=$(awk '/^cpu /{s=0; for(i=2;i<=NF;i++) s+=$i; print s}' /proc/stat)
while true
do
sleep 0.5
idle=$(awk '/^cpu /{print $5}' /proc/stat)
total=$(awk '/^cpu /{s=0; for(i=2;i<=NF;i++) s+=$i; print s}' /proc/stat)
diff_idle=$((idle - prev_idle))
diff_total=$((total - prev_total))
usage=$(echo "scale=1; 100 * ($diff_total - $diff_idle) / $diff_total" | bc -l)
echo "CPU Usage: $usage%"
prev_idle=$idle
prev_total=$total
done

One liner for the above:

CPU Usage Alternative One-liner
_STAT_PREV_IDLE=$(awk '/^cpu /{print $5}' /proc/stat); prev_total=$(awk '/^cpu /{s=0; for(i=2;i<=NF;i++) s+=$i; print s}' /proc/stat); while true; do sleep 0.5; idle=$(awk '/^cpu /{print $5}' /proc/stat); total=$(awk '/^cpu /{s=0; for(i=2;i<=NF;i++) s+=$i; print s}' /proc/stat); diff_idle=$((idle - _STAT_PREV_IDLE)); diff_total=$((total - prev_total)); usage=$(echo "scale=1; 100 * ($diff_total - $diff_idle) / $diff_total" | bc -l); echo "CPU Usage: $usage%"; _STAT_PREV_IDLE=$idle; prev_total=$total; done



Memory Usage w/ no external tools

Memory Usage
while true
do
awk '/^MemTotal:/{total=$2}/^MemAvailable:/{avail=$2} END{used=total-avail; usage=(used/total)*100; used_gb=used/1024/1024; printf("RAM Usage: %.2f%% %.2fGB\n", usage, used_gb)}' /proc/meminfo
sleep 0.5
done

One-liner for the above:

Memory Usage One-liner
while true; do awk '/^MemTotal:/{total=$2}/^MemAvailable:/{avail=$2} END{used=total-avail; usage=(used/total)*100; used_gb=used/1024/1024; printf("RAM Usage: %.2f%% %.2fGB\n", usage, used_gb)}' /proc/meminfo; sleep 0.5; done



Create Network Interface

Easy way to isolate traffic to an interface. Useful for debugging traffic with things like wireshark without worrying about all the traffic on eth0 or wlan0 polluting the capture.

After running this any program should be able to listen / connect on 10.0.42.1/24:* .

Create Interface
IF=coolnet0 CIDR=10.0.42.1/24 && ip link add "$IF" type dummy && ip addr add "$CIDR" dev "$IF" && ip link set "$IF" up
Delete Interface
IF=coolnet0 ip link delete "$IF"

Nmap quick/clean scan

Command to print only ips from an nmap scan.

Nmap Scan IP Only Print
nmap -sn 10.0.0.1/24 -oG - | awk '/Up$/{print $2}'

TcpDump Bash For Simple Network Traffic Information

Simple tcpdump bash command that shows all network connections / traffic polling/refreshing every 10 seconds with information about when it was last seen and how long it was active.

Simple Network Traffic Info
sudo bash -c 'tcpdump -i any -nn -l -q 2>/dev/null | awk '"'"'
function update(key,now) {
if (!(key in first)) first[key]=now
last[key]=now
}
{
now=systime()
if ($0 ~ / IP6? /) {
for(i=1;i<=NF;i++) {
if($i=="IP" || $i=="IP6") {
src=$(i+1); dst=$(i+3)
sub(/:$/,"",dst)
gsub(/[,:]$/,"",src)
split(src,a,".")
split(dst,b,".")
if(src !~ /^127\./ && src !~ /^0\./) {
key=src " -> " dst
update(key,now)
}
}
}
}
if(now>=refresh+1) {
refresh=now
system("clear")
print "LIVE NETWORK CONNECTIONS | Active window: 30s | Closed retention: 10s"
print "---------------------------------------------------------------"
n=0
for(k in last) {
age=now-last[k]
if(age<=30 || (age<=10 && closed[k])) {
duration=now-first[k]
n++
keys[n]=k
ages[n]=duration
} else {
delete first[k]; delete last[k]; delete closed[k]
}
}
for(i=1;i<=n;i++) for(j=i+1;j<=n;j++) if(ages[j]>ages[i]) {
t=ages[i]; ages[i]=ages[j]; ages[j]=t
t=keys[i]; keys[i]=keys[j]; keys[j]=t
}
for(i=1;i<=n;i++) printf "%-65s | Seen: %3ds ago | Duration: %3ds\n",keys[i],now-last[keys[i]],ages[i]
fflush()
}
}'"'"''

One Liner for the above:

Same as above but one liner
sudo bash -c 'tcpdump -i any -nn -l -q 2>/dev/null | awk '\''function update(key,now){if(!(key in first))first[key]=now;last[key]=now} {now=systime();if($0~/ IP6? /){for(i=1;i<=NF;i++)if($i=="IP"||$i=="IP6"){src=$(i+1);dst=$(i+3);sub(/:$/,"",dst);gsub(/[,:]$/,"",src);if(src!~/^127\./&&src!~/^0\./){key=src" -> "dst;update(key,now)}}}if(now>=refresh+1){refresh=now;system("clear");print "LIVE NETWORK CONNECTIONS | Active window: 30s | Closed retention: 10s";print "---------------------------------------------------------------";n=0;for(k in last){age=now-last[k];if(age<=30||(age<=10&&closed[k])){duration=now-first[k];n++;keys[n]=k;ages[n]=duration}else{delete first[k];delete last[k];delete closed[k]}}for(i=1;i<=n;i++)for(j=i+1;j<=n;j++)if(ages[j]>ages[i]){t=ages[i];ages[i]=ages[j];ages[j]=t;t=keys[i];keys[i]=keys[j];keys[j]=t}for(i=1;i<=n;i++)printf "%-65s | Seen: %3ds ago | Duration: %3ds\n",keys[i],now-last[keys[i]],ages[i];fflush()}}'\'''

DNS/Host Resolution via python for above TcpDump Bash For Simple Network Traffic Information

Add here

Same as above tcpdump command but using python for resolution
sudo python3 -u -c $'import subprocess,re,socket,time,select,concurrent.futures,sys,shutil\np=subprocess.Popen(["tcpdump","-i","any","-nn","-l","-q","-tt"],stdout=subprocess.PIPE,stderr=subprocess.DEVNULL,text=True,bufsize=1)\npool=concurrent.futures.ThreadPoolExecutor(max_workers=32)\ndns,pending,flows={},{},{}\npat=re.compile(r"(?:^|\\s)IP6?\\s+(\\S+)\\s+>\\s+(\\S+):")\ndef ipport(s):\n s=s.rstrip(":"); h,sep,port=s.rpartition(".")\n if not sep:return s\n try:socket.inet_pton(socket.AF_INET,h)\n except OSError:\n try:socket.inet_pton(socket.AF_INET6,h)\n except OSError:return s\n return h\ndef resolve(ip):\n try:return socket.gethostbyaddr(ip)[0]\n except:return ip\ndef lookup(ip):\n if ip not in dns and ip not in pending:pending[ip]=pool.submit(resolve,ip)\ndef label(ip):return dns.get(ip,ip+" [DNS...]")\ncols=shutil.get_terminal_size().columns\nrows=shutil.get_terminal_size().lines\nlast_draw=0\nold=[]\nprint("\\033[?25l\\033[2J\\033[H",end="",flush=True)\ntry:\n while True:\n ready,_,_=select.select([p.stdout],[],[],0.25)\n now=time.time(); changed=False\n if ready:\n line=p.stdout.readline()\n if not line:break\n m=pat.search(line)\n if m:\n src,dst=map(ipport,m.groups()); key=(src,dst)\n if key not in flows:flows[key]=[now,now]\n else:flows[key][1]=now\n lookup(src);lookup(dst);changed=True\n for ip,f in list(pending.items()):\n if f.done():\n try:dns[ip]=f.result()\n except:dns[ip]=ip\n del pending[ip];changed=True\n for k,v in list(flows.items()):\n if now-v[1]>30:del flows[k];changed=True\n if changed or now-last_draw>=1:\n current=["LIVE NETWORK TRAFFIC | 30s rolling window | Async DNS","-"*min(cols-1,110)]\n for (src,dst),(first,last) in sorted(flows.items(),key=lambda x:(x[1][1]-x[1][0],x[1][1]),reverse=True):\n current.append(f"{label(src):45.45} -> {label(dst):45.45} | idle {int(now-last):2}s | seen {int(now-first):3}s")\n current=current[:rows-1]\n for i,line in enumerate(current):\n print(f"\\033[{i+1};1H\\033[2K{line[:cols-1]}",end="")\n for i in range(len(current),len(old)):\n print(f"\\033[{i+1};1H\\033[2K",end="")\n old=current;last_draw=now;sys.stdout.flush()\nexcept KeyboardInterrupt:pass\nfinally:\n p.terminate();pool.shutdown(wait=False,cancel_futures=True)\n print("\\033[?25h\\033[0m\\n",flush=True)'