Misc.
Remove Duplicate Lines (Without Sorting)
awk '!seen[$0]++' filename
curl HTTP/HTTPS scan of host range
curl --include --verbose --insecure --connect-timeout 1 "{http,https}://10.0.1.[1-254]" -o "10.0.1.#2_#1.html" 2>&1 | tee curl.log
Delete old files but always keep the x newest ones
find . -maxdepth 1 -type f -printf '%T@\t%p\n' | sort -t $'\t' -g | head -n -10 | cut -d $'\t' -f 2- | xargs ls -1
ls -1t | sed -e '1,10d' | xargs -d '\n' ls -1
Serving Documents via HTTP
while true; do nc -l -p 80 -q 1 < error.html; done while true; do { echo -e 'HTTP/1.1 200 OK\r\n'; cat index.html; } | nc -l 8080; done python2 -m SimpleHTTPServer 8080 python3 -m http.server 8080 twistd -n web –path . php -S localhost:8080
View markdown files in lynx
pandoc -s -f markdown -t html =(curl https://raw.github.com/cyberhouse/doc/master/README.md) | lynx -stdin pandoc -s -f markdown -t html doc/README.md | lynx -stdin
Set Host Header in curl
curl --header 'Host: www.example.com' 'http://localhost/index.php?id=23&L=5'
Cookies and Login with curl
curl -d @$HOME/.credentials --cookie-jar /tmp/cjar -k https://example.com/?do=login; curl -k --cookie /tmp/cjar --cookie-jar /tmp/cjar https://example.com/foo|lynx -stdin
Get HTTP Status Code with curl
curl -s -w "%{http_code}\\n" http://www.cyberhouse.at -o /dev/null # Add -L if you intend to follow location responses
See File Access for a Specific Path
lsof +D /var/log
Find Rogue Internet Gateways on your Network
nmap -sn 172.16.35.0/24 --script ip-forwarding --script-args="target=www.google.com"
Remote Network Protocol Analyzing with tcpdump or tshark and Wireshark
ssh server1 tcpdump -i eth3 -U -s0 -w - 'tcp port 80' | wireshark -k -w /tmp/gw.cap -b filesize:50000 -b files:10 -i - ssh server1 'tshark -f "port !22" -w -' | wireshark -k -i - tshark -i any -s0 -f 'port !22' -w $(date +%s).pcap
Create Diff with remote file
diff .ssh/config <(ssh trillian 'cat .ssh/config')
Create cronjob programmatically
crontab -l > /tmp/$(whoami)-crontab echo '* * * * * www-data /var/www/html/typo3/cli_dispatch.phpsh scheduler' >> /tmp/$(whoami)-crontab crontab /tmp/$(whoami)-crontab rm /tmp/$(whoami)-crontab
Forward Ports
socat TCP4-LISTEN:1234,fork TCP4:192.168.1.1:22' forwards your port 1234 to another machine's port 22. Very useful for quick NAT red
Sysstat's sar with 24h Time Format
sar -o /tmp/sarlog -A 5 >/dev/null 2>&1 LANG=C; S_TIME_FORMAT="%T; sar -f /tmp/sarlog|les
Setting and Removing the Immutable Bit
chattr +i /etc/shadow; lsattr /etc/shadow chattr -i /etc/shadow; lsattr /etc/shadow
Add Swap Space
dd if=/dev/zero of=/path/to/swapfile count=2097152 # create 1 G file mkswap /path/to/swapfile swapon -p 32767 /path/to/swapfile # use with lowest priority possible
Optionally create an ''/etc/fstab entry:
<code>
/path/to/swapfile none swap sw,pri=32767 0 0
</code>
===== Reset Root Password, Access to Boot Loader Required =====
Add
1 (after one blank space) or
S or
Single to the kernel boot parameter to start in single mode. Reset password with
passwd check if SELinux is disabled if it does not work (
setenforce 0). If single user mode does not work ther is an alternative: Append
init=/bin/bash to the boot options, remounting the root partition is necessary (
mount -o remount,rw /).
===== Cut Connections from a Specific Address =====
<code>
cutter 10.10.0.45
</code>
===== Copy Disk with dd with Progress Bar and Compression =====
<code>
parted /dev/sda # Get the disk size first
dd if=/dev/sda | pv -s 60022480896 | pigz –fast > /media/myExternalDrive/myBackup.img
</code>
===== Write Image with dd with Progress Bar =====
<code>
IMAGE=archlinux-2014.04.01-dual.iso && dd bs=1m if=$IMAGE | /opt/local/bin/pv -s $(wc -c $IMAGE) | dd of=/dev/disk1
</code>
===== Get the Filesystem Age =====
<code>
tune2fs -l $(df -lh / |(read; awk '{print $1; exit}')) | grep -i created
</code>
===== Strace All Processes of a Program, e.g. Apache =====
<code>
program=httpd; strace $(ps wuax|grep $program|grep -v grep|awk '{print “-p” $2}'|xargs) -s8192 -r -o /tmp/$program-$(date +%FT%T%z)
</code>
===== Log Block Activity to Syslog (or dmesg, Depending on your Ssytem) =====
<code>
echo 1 > /proc/sys/vm/block_dump
</code>
===== Sorting ps Output by RSS =====
<code>
ps auxww | sort -nk6
# or
ps auxww –sort +rss
</code>
===== Better Readable ps =====
<code>
ps auxwwef
# or to avoid line wraps
ps auxef
</code>
===== Get Buffered and Cached Read Speed of Block Device =====
<code>
hdparm -Tt /dev/sda
</code>
===== Fill Memory Up to Given Percentage =====
<code>
stress –vm-bytes $(awk '/MemFree/{printf “%d\n”, $2 * 0.9;}'< /proc/meminfo)k –vm-keep -m 1
</code>
===== Using top in Batch Mode =====
<code>
top -b -i -d 10 » /tmp/log/top
</code>
===== Simple Port Check with Bash's Built-in /dev/tcp =====
<code>
/bin/bash -c “echo >/dev/tcp/144.76.8.201/443 && echo 'open'”
</code>
===== Check sudo Permissions of User =====
sudo -U username -l
===== Show OOM score ====
dstat –top-oom
===== Transfer Files without SSH =====
remotely:
<code>
cat /usr/local/www/munin/memory-month.png | gzip -9c | uuencode /dev/stdin
</code>
locally:
<code>
pbpaste | uudecode -o /dev/stdout | gzip -cd > ~/Downloads/memory-month.png
</code>
===== List SMB shares on macOS =====
smbutil statshares -a
===== Get Public/External IP Address =====
<code>
dig +short myip.opendns.com @resolver1.opendns.com
</code>
<code>
curl ipinfo.io
</code>
<code>
curl ifconfig.me/all
</code>
<code>
curl icanhazip.com
</code>
<code>
curl myip.dnsomatic.com
</code>
<code>
curl whatismyip.akamai.com
</code>
==== Add Timestamp to Output ====
<code>
vmstat 5 | awk '{system(“/bin/echo -n $(/bin/date”); print “: ” $0}' | tee vmstat-$(date +%F)
</code>
<code>
vmstat 5 | awk '{now=strftime(“%Y-%m-%d %T ”); print now $0}'
</code>
==== Monitor Occurrence of Event or State Change and Notify by Terminal Bell ====
<code>
while sleep 3; do curl –silent https://wiki.sysop.cafe/misc|grep sleep && tput bel; done
</code>
<code>
while sleep 3; do dig foo.bar.baz +short @8.8.8.8|grep 129 && tput bel; done
</code>
==== Terminal bell character ====
<code>
printf '\a'
echo -en “\007”
tput bel
</code>
==== Test Hosts on Servers not Available via DNS ===
<code>
curl -H “Host: example.com” 10.0.0.13
</code>
<code>
wget –header=“Host: example.com” 10.0.0.13
</code>
==== .htpasswd ====
<code>
printf “John:$(openssl passwd -crypt V3Ry)\n” » .htpasswd # this example uses crypt encryption
</code>
<code>
printf “Mary:$(openssl passwd -apr1 SEcRe7)\n” » .htpasswd # this example uses apr1 (Apache MD5) encryption
</code>
<code>
printf “Jane:$(openssl passwd -1 V3RySEcRe7)\n” » .htpasswd # this example uses MD5 encryption
</code>
<code bash>
(PASSWORD=“SEcRe7PwD”;SALT=“$(openssl rand -base64 3)”;SHA1=$(printf “$PASSWORD$SALT” | openssl dgst -binary -sha1 | xxd -ps | sed 's#$#'“`echo -n $SALT | xxd -ps`”'#' | xxd -r -ps | base64);printf “Jim:{SSHA}$SHA1\n” » .htpasswd) # this example uses SSHA encryption
</code>
<code perl>
#!/usr/bin/perl
use strict;
chomp(my $filename=$ARGV[0]);
chomp(my $username=$ARGV[1]);
chomp(my $password=$ARGV[2]);
if (!$filename || !$username || !$password) {
print “USAGE: ./crypt.pl filename username password\n\n”;
} else {
open FILE, “»”, $filename or die $!;
print FILE $username . “:” . crypt($password, $username) . “\n”;
close FILE or die $!;
}
</code>
==== Using Comments as Hashtags for Easier History Search ====
<code bash>
vim -R /var/db/mysql/percona57.err #mysql-error
</code>
==== Getting SIGINFO with Ctrl-T ====
e.g. to get an progress information for a running program like
dd,
ping,
rsync'''.
Delete Line from File
sed -i -e '3d' ~/.ssh/known_hosts
Find and process files older than…
find /opt/tomcat/temp/ -type f -name '*.xlsx' -mtime +14 -exec rm {} \;
Run (Cron) Script only if not already running
4 5 * * * /usr/local/bin/flock -n /tmp/backup.lock /usr/local/scripts/bbackupfoo.sh