Commandline CLI wisdom

SSH Authentifizierung über Public-Keys

Create public and private keys using ssh-key-gen on local-host

ssh-keygen -b 8192 -C "oliver@ripley"

Automatically installs your public key to the remote host (this is included in the openssh package)

ssh-copy-id 'user@remotehost'

Login to remote-host without entering the password

ssh remote-host

SSH hintereinander verschachteln

ssh -t user@host1 "ssh user@host2" verschachtelt ssh hintereinander
ssh -l <username> -L <zu tunnelnder Port>:<IP des Server auf den ich nur über Umwege komme>:<zu tunnelnder Port> <IP des Rechners, zu dem ich Zugang habe>
ssh -l oliver -L 2323:192.168.1.2:4312 192.168.1.1

ssh -t redbrain@ara.uberspace.de "ssh root@172.23.30.70"

ssh -L localhost:22:redbrain@ara.uberspace.de:22 root@172.16.42.41
sudo -E ssh graute@graute-think -l graute -L 127.0.0.2:80:192.168.4.101:80 -L 127.0.0.2:22:192.168.4.101:22 -L 127.0.0.2:8081:192.168.4.101:8081 -L 127.0.0.2:9418:192.168.4.101:9418

SSH Tunneling 1

This will get you a shell on machineb. Leave this alone; minimize the terminal window. Now, whenever you make an ssh connection to localhost, you will actually be connected to machinec through machineb.

ssh -L localhost:22:machinec:22 machineb
sudo ssh -L localhost:22:root@10.10.1.100:22 root@172.23.24.175
sudo ssh -L localhost:4711:root@10.10.1.100:4811 root@172.23.24.175

SSH Tunneling 2

Tunnel von Support PC über ZwischenStation zum Ziel

supportpc$ ssh -L 54321:localhost:54321 root@172.16.42.41
zwischen$ ssh -L 54321:localhost:8080 redbrain@ara.uberspace.de
supportpc$ ssh -L 54322:ara.uberspace.de:22 root@172.16.42.41
supportpc$ ssh redbrain@localhost -p 54322

Source:

git pull ssh://redbrain@localhost:54322/home/redbrain/Projekt_Git_Repos/Scripte.git/
git push ssh://redbrain@localhost:54322/home/redbrain/Projekt_Git_Repos/Scripte.git/

sshpass command

sshpass enables non-interactive ssh password authentication. It allows you to establish an SSH connection by specifying a password as part of the command

sshpass -p password ssh -o stricthostkeychecking=no user@host command_to_run

Specifying the password as part of a command is not good security practice. A better approach is to export the password as the SSHPASS environment variable, and then use sshpass with the -e parameter:

export SSHPASS="password"
sshpass -e ssh -X -o stricthostkeychecking=no user@remotehost "command_to_run"

ssh X Forwarding

ssh -X user@remotehost

SSH X Forwarding über Tunnel von Support PC (Laptop) über ZwischenStation (RasperryPI) zum Zielhost (DesktopPC)

Erste Passwort ist das von der Zwischenstation, das zweite ist das vom Zielhost

supportpc$ export SSHPASS="PASSWORD"
supportpc$ sshpass -e ssh -X -o stricthostkeychecking=no pi@zwischenStation "export SSHPASS="PASSWORD" && sshpass -e ssh -X -o stricthostkeychecking=no username@zielhost "xclock""

ssh X Forwarding nach windows mit putty

Unter windows den Xserver Xming installieren In putty unter SSH->X11 Enable X11 Forwarding einstellen. Unter Linux /etc/ssh/sshd_config auf “X11Forwarding yes” überpüfen. mit ssh auf Linux PC einloggen.

Dann Programm starten das X11 benutzt:

xclock &

check tunnel

netstat -ntapu | grep ":54321"
netstat -ntapu | grep "LISTEN"

nested ssh tunnel

vi /etc/ssh/sshd_config

ssh logging

create in your .bashrc a function:

myssh () { ssh $1 2>&1 | tee -a ~myusername/logdir/$1.log; }

alias is with

alias ssh=myssh

Compare a remote file with a local file

ssh user@host cat /path/to/remotefile | diff /path/to/localfile -

Socks5 proxy with SSH to bypass content filters

ssh -D 1337 -q -C -N user@host

tell your browser to use that SOCKS5 proxy

generate huge network load

for p in {6000..6015} ; do ssh -f -L $(($p + 1)):localhost:$p -p $p localhost sleep 1h ; printf "%s <== %s [%s]\n" $p $(($p + 1 )) "$(date)" ; done

check the Network load

ssh -D 2000 -p 6002 localhost
ifconfig lo| grep "RX"

Tunnel von Support PC über ZwischenStation zum Ziel

supportpc$ ssh -L 54321:localhost:54321 root@172.23.24.175
zwischen$ ssh -L 54321:localhost:8080 root@10.10.1.111
supportpc$ ssh -L 54322:10.10.1.111:22 root@172.23.24.175
supportpc$ ssh root@localhost -p 54322

In one command, ssh to an internal host on your private network through a gateway host.

ssh -t you@homeaddr 'ssh you@192.168.1.5'

Make local webserver available via remoteserver:8080. Req. GatewayPorts yes on sshd

ssh -R *:8080:localhost:80 remoteserver

Encapsulate UDP packets in a TCP stream so it can be tunneled through an SSH tunnel, etc

socat -v UDP-LISTEN:8161,fork TCP:localhost:8161

Screen

screen - Prozesse im Hintergrund starten

screen

create a new screen with the name foo

screen -S foo

return to screen with name foo

screen -r 'foo'

laufende screens anzeigen

screen -ls

zum nächsen screen springen

Ctrl-A” and “n“.

zum vorherigen screen springen

Ctrl-A” and “p“.

Creating Logfile

“Ctrl-A” and “H“

Screen Password Lock

“Ctrl-A” and “x”

screen Prozesse in den Vordergrund holen

screen -r

screen Prozesse Logging

screen -L

connecting directly to a screen remotly via ssh

ssh -t  pi@raspberry screen -ls
ssh -t  pi@raspberry screen -r 2684.pts-1.raspberrypi

reptyr

reptyr can be used to reattach a program on another tty to the current tty so you can transfer it into screen/tmux.

reptyr <pid>

watch Prozesse beobachten

watch -n 10 process

Ubuntu release update

do-release-upgrade

Ubuntu print distribution-specific information

lsb_release --all

Anlegen des Git Repositories

mkdir newdir
cd newdir
git init
git add .
git commit

clonen eines vorhandenen sauberen repos

git clone --bare /home/oliver/workspace/hacking/ hacking.git

Initialized empty Git repository in /tmp/hacking.git/

aus sauberen repo Arbeitsverzeichnis erstellen

git clone /tmp/hacking.git/ /tmp/hacking

Pushen

git push ssh://git@192.168.4.1/hacking.git

Tags Pushen

git push ssh://git@192.168.4.1/hacking.git Portbindender_Shellcode

Pullen

git pull ssh://git@192.168.4.1/hacking.git
git pull ssh://oliver@192.168.3.5/home/git/repositories/Scripte.git/

Tags Pullen

git pull ssh://git@192.168.4.1/hacking.git Portbindender_Shellcode

Eine neue remote quelle angeben und daraus aktualisieren

git remote add  -f hacking.git ssh://git@192.168.4.1/hacking.git
git remote add origin https://github.com/user/repo.git

Set a new remote

git remote -v

Verify new remote

origin  https://github.com/user/repo.git (fetch)
origin  https://github.com/user/repo.git (push)

Using “git diff –word-diff” on #LaTeX files can be very helpful in tracking changes

git diff --word-diff

Statistiken zu Codeänderungen am Beispiel des Linux Kernels

Anzahl Dateien

find . -type f -not -regex '\./\.git/.*' | wc -l

Zeilen Quelltext

find . -type f -not -regex '\./\.git.*' | xargs cat | wc -l (find . -name *.[hcS] -not -regex '\./\.git.*' | xargs cat | wc -l)

Anzahl Commits

git-log --no-merges --pretty=oneline v2.6.(x-1)..v2.6.(x) | wc -l

Diffstat

git diff --shortstat v2.6.(x-1)..v2.6.(x)

ColorDiff

colordiff is a wrapper for diff and produces the same output as diff but with coloured syntax

apt-get install colordiff
colordiff file1 file2
diff -u file1 file2 | colordiff

Verschlüsselung:

gpg --export-secret-key KEY_ID | paperkey --output-type raw | dmtxwrite -e 8 -f PDF > secret-key.pdf

Generates key-aa, key-ab, …

gpg --export-secret-key KEY_ID | paperkey --output-type raw | split -b 1500 - key-

Convert each of them to a PNG image

for K in key-*; do
    dmtxwrite -e 8 $K > $K.png
done
dmtxread
cat my-scanned-keys | paperkey --pubring ~/.gnupg/pubring.gpg > secret-key.gpg

Barcode and QR-Codes

$ echo -n 123456 | dmtxwrite > image.png
$ echo -n 123456 | dmtxwrite -o image.png
$ echo -n 123456 > message.txt; dmtxwrite message.txt > image.png
$ dmtxwrite <(echo -n 123456) -o image.png
$ dmtxread -n image.png
$ cat image.png | dmtxread -n
$ dmtxread -n -N1 image1.png image2.png image3.png
$ dmtxread -n -N1 -m500 image.png
$ echo 'Hello, world!' | dmtxwrite | dmtxread
Hello, world!
$ dmtxread image1.png | dmtxwrite -o image2.png

Generate QR-Code for a WiFi hotspot

qrencode -s 7 -o qr-wifi.png "WIFI:S:$(zenity --entry --text="Network name (SSID)" --title="Create WiFi QR");T:WPA;P:$(zenity --password --title="Wifi Password");;"

Generate a Hello World QR-Code

qrencode -o qrcode.png 'Hello World!'
qrencode -o lpm.png http://www.linuxpromagazine.com

Isos Mounten

sudo mount -o loop -t iso9660 Desktop/Ubuntu\ 11.04/Ubuntu\ DVD/natty-dvd-i386.iso /media/cdrom

Mount a CD-ROM disc from its ISO image file.

dd if=/dev/cdrom of=image.iso ; mkdir CDroot ; mount -o loop image.iso CDroot ; cd CDroot

dd with progress bar and statistics

sudo dd if=/dev/sde1 bs=4096 | pv -s 2G | sudo dd bs=4096 of=~/USB_BLACK_BACKUP.IMG

Dateien syncronisieren mit rsync

using rsync in a screen session is always helpfull

rsync -av <quelle> <ziel>
rsync -av --delete -e ssh root@server.example.com:/home/ /mnt/server-mirror/home/

Adjust all rsync processes on the system to lower IO priority

pgrep rsync | xargs ionice -c3 -p

move a lot of files over ssh

copy files to a ssh server with gzip compression

rsync -az /home/user/test user@sshServer:/tmp/
rsync -avze ssh /home/benutzer benutzer@example.com:/backups

rsync cmd as substitute for scp

rsync -r -P -e ssh --stats <quelle> <ziel>

wobei die einzelnen Parameter diese Bedeutungen haben:

-r = copy files recursive
-P = progressbar and contunie after a abort
-e ssh = using ssh as transport protocol
--stats = statistic after the sync is finished
rsync -avzhu --progress <quelle> <ziel>

move a lot of files over ssh alternative

Useful to move many files (thousands or millions files) over ssh. Faster than scp because this way you save a lot of tcp connection establishments (syn/ack packets).

tar -cf - /home/user/test | gzip -c | ssh user@sshServer 'cd /tmp; tar xfz -'

Copy with progress

rsync --progress file1 file2
rsync  -avP /dir1 /dir2

Limit the bandwith

Do an rsync and limit the bandwidth used to about 200 KBytes/sec. Useful on shared or slow links.

rsync --bwlimit=200 src dest

Apparently the fastest way to delete millions of small files

rsync -a -delete empty/ foo/

Cryptsetup

cryptsetup luksHeaderBackup --header-backup-file <file> <device>
luksHeaderRestore

Mount

mount //172.23.1.25/Technik /media/Technik/ -t cifs -o username=ogr,workgroup=Neuhaus

Spilit mkv File

mkvmerge Tron\ Legacy\ 2010\ 1080p\ 143.mkv --split 4096M -o Tron Legacy 2010 1080p 143_1.mkv

md5sum

md5sum durch Verzeichnisse rekursiv nutzen

find . -type f -print0 | xargs -0 md5sum >> checksummen.md5

resize2fs

ext2 Dateisystem vergrößern

resize2fs - ext2/ext3 file system resizer

Setting UP PPP Connection via 2 Serial Devices

sudo pppd nodetach 10.0.3.1:10.0.3.2 pty "pppd notty"
cat /dev/pts/2
echo Test > /dev/pts/4

start with an empty set of ppp options

mv /etc/ppp/options /etc/ppp/options.bak
touch /etc/ppp/options

run this first at one command prompt

pppd /dev/ptyp0 nodetach local 10.0.3.1:10.0.3.2

run this at another command prompt

pppd /dev/ttyp0 nodetach local
tail -f /var/log/messages
tcpdump -i lo -n "imcp"
tcpdump -i ppp0 -n "icmp"
tcpdump -i ppp1 -n "icmp"
ping 10.0.3.1

Connecting two pppd commands together

Connecting two pppd commands together, on different hosts, over an existing network, by embedding an ssh command to start the remote one inside the pty argument of the one started on the local host. This creates an encrypted network tunnel through the existing network, aka, a VPN.

start with an empty set of ppp options

mv /etc/ppp/options /etc/ppp/options.bak
touch /etc/ppp/options

Can setting up an encrypted VPN really be this simple?

pppd nodetach 10.0.3.1:10.0.3.2 pty "ssh -l root remotehost pppd notty"

Tar and Split

tar -czvf - home/ | split -b 700m -
cat xa* > home.tar.gz
tar -xzvf home.tar.gz

mount a directory with sshfs

sshfs USERNAME@192.168.1.1:/home/USERNAME ~/srv/
sshfs -h
fusermount -u ~/srv/

what process have opened a given file

fuser displays the PIDs of processes using the specified files

fuser myfile

Send SIGTERM to every process that has myfile opened

fuser -k myfile

Filesystem Operations

Ordner größe anzeigen

du -a -c -h directory/

größte Ordner und Dateien im Verzeichnis sortiert anzeigen

du -sh * | sort -h -r

Disk Usage Sorted by Size

du -h|sort -hr|less

Show directory size and sort by human readable amount (MB, GB, etc.). Requires fairly recent version of GNU sort.

du -sh */ | sort -h

List the 20 largest files or folders under the current working directory.

du -ma | sort -nr | head -n 20

Show the 10 largest directories at top level along with total usage. All in megabytes.

du -cms .[^.]*/ */ | sort -rn | head

Show all file lager then 100 Megabyte

ls -lahS $(find / -type f -size +100M)

space usage of directories only

du -sh */

List the size (in human readable form) of all sub folders from the current location

du -h --max-depth=1
du -sh */ #space usage of directories only

Find out which of your directories(below the current directory) occupy at least 1GB of space.

du -h . | grep "^[0-9\.]\+G"

du with colored bar graph

du -x --max-depth=1|sort -rn|awk -F / -v c=$COLUMNS 'NR==1{t=$1} NR>1{r=int($1/t*c+.5); b="\033[1;31m"; for (i=0; i<r; i++) b=b"#"; printf " %5.2f%% %s\033[0m %s\n", $1/t*100, b, $2}'|tac
df -Pl -t ext3 -t ext4 | tail -n+2 | awk '{ sum+=$3 } END { print sum/2**20 }'

Show the total space used on all your local disk partitions.

df -lP |awk '{sum += $3} END {printf "%d GiB\n", sum/2**20}'

Show ext2, 3 & 4 FS w/ human readable output. Sort percent,space left.

df -text{2..4} -hP |column -t |tail -n+2 |sort -k5nr -k4n

größte Ordner und Dateien im Verzeichnis sortiert anzeigen

du -sh * | sort -h -r

Memory Operations

Show the total memory used by Chrome processes (Probably a lot)

Speicherverbrauch von Prozess Chrome anzeigen

ps aux | awk '/chrome/ {sum += $6} END { printf "%dMB\n", sum/1024 }'

Show % reports of CPU statistics for every active task in the server at two second intervals.

pidstat 2 5

Display top RAM using processes.

ps aux |tail -n+2 |sort -nrk4 |head -$(($(tput lines)-1)) |cut -c 1-$(tput cols)

On Linux, print out a list of the process IDs that are in the zombie state.

ps aux | awk '{if ($8=="Z") { print $2 }}'

Kill all #zombies on the system.

ps ax -o state -o ppid  | awk '$1=="Z"{print $2}' | xargs kill -9

An easier way of selecting the processes owned by someuser.

ps wu -U someuser

You started a program, but now want it to notify you when its done.

<ctrl-z> bg ; wait %1 ; echo "done" | mail -s "done" you@example.com

See the first 10 lines of foo.sh byte by byte. Allows you to see “invisible characters” messing things up

head foo.sh | hexdump -c

Canonical hex+ASCII display.

hexdump -C hexdump.dmp

Outputs the hex value of each letter.

echo hello | hexdump -v -e '/1 "%02X\n"'

Give a character by character dump of that suspicious looking URL you just copied from e-mail.

( xsel -b || pbpaste ) | hexdump -c

add timestamps to log or program output. perl -n forms a while loop.

tail -f logfile | perl -ne 'print localtime . " $_";'

Notify when more than 1 unique user logs in.

while true; do [ $(sleep 1m && users | wc -w) -gt 1 ] && echo warning ; done

Primes using Perl regex

for i in {1..997};do printf "%0${i}d"|grep -P "^0?$|^(00+?)\1+$" >/dev/null ||echo $i;done

Try finding the URL given a fuzzy screenshot.

for u in example/bc{3,8}449{0,8,9}2-ab7a-1{3,8}03; do curl -sI $u|grep -q 404||echo $u; done

Convert a video into a series of pictures for analysis.

mkdir frames && cd frames && mplayer -ao null -vo png ../video.flv

Grab a video from an embeded flash player.

rtmpdump -r rtmp://videoserv.url:1935/app/somevideo.mp4 --flv video.flv

is nice if you want to manage the video window better than what an embeded browser video window will allow.

rtmpdump -v --live -r rtmp://streamhost/live/ |tee streamcopy.flv |mplayer -xy 2 - # stream directly at 2x size and save a copy.

Try to find the rtmp URL for an embeded video on a website.

sudo tcpdump -s1514 -A -w - | strings | egrep -A5 "(rtmp://|Playing)"

plot random numbers in terminal

for i in {1..50};do echo "$i: $(($RANDOM%30))"; done |gnuplot -e "set terminal dumb $COLUMNS $LINES; plot '-' with lines"

Show Linux distro release information or die trying

lsb_release -a || cat /etc/redhat-release

Count the number of web server processes running.

ps auxw | grep "[h]ttpd" | wc -l

Find out if any duplicate image files exist in the current directory.

shasum *.jpg | awk {'print $1'} | sort | uniq -c | grep -v " 1 "

Make thumbnails of images IMG_3000.JPG - IMG_3499.JPG

for i in IMG_3[0-4]*.JPG ; do convert -quality 60 -geometry 300 $i thumbs/$i ; done

Split a file called largefile into 1 gigabyte pieces called split-xaa, split-xab, split-xac …

split -b 1G verylargefile split

Join the splits back together.

cat split-xaa split-xab split-xac > rejoinedlargefile

restore a mysql dump with progressbar and ETA.

pv bigdump.sql.gz | gunzip | mysql

Boot Zeitpunkt des Systems berechnen

date -d @$(( $(date +%s) - $(cut -f1 -d. /proc/uptime) ))
date -d @$(cat /proc/stat | grep btime | awk '{ print $2 }')

Convert Unix Timestamp in Human readable Date

date -u -d @1234567890
Fr 13. Feb 23:31:30 UTC 2009

Learn a command on each new shell open.

echo 'man $(ls /usr/bin | shuf -n 1)| sed -n "/^NAME/ { n;p;q }"' >> ~/.bashrc

Move photos with EXIF data to directories by year created/taken. Be verbose.

exiftool -v '-Directory<DateTimeOriginal' -d %Y .

A simple stopwatch that works more universally. Use Ctrl-D to stop. Check elapsed or real time in output.

time cat

Get list of top URLs from all logs combined

zcat access_log*.gz |cat - access_log |awk '{print $7}' |sed 's/\?.*//' |sort|uniq -c|sort -nr

group subdomains by domain.

cat longdomainlist.txt | rev | sort | rev

Monitor your wireless card signal strength on the screen

watch -n1 cat /proc/net/wireless

Remove any encryption or protection from PDF file

qpdf --decrypt protectpdf.pdf output.pdf

Copy a file using “ionice -c 3” to give it idle priority to reduce load on the system.

ionice -c 3 cp vm1.img vm1-clone.img

Find file duplicates in ‘dir’ recursively based on size and mdsum and log to dupes.txt

fdupes -r dir > dupes.txt

Turn a Unix epoch time back into a human readable date. This is a not widely known feature of the GNU date command.

date -d @728737200

“Attempt” to recover an accidentally removed file.

fgrep --binary-files=text -C 2000 "string in file" /dev/sda > recovereddata.out

Search for names and build a frequency count for each name.

egrep -o "(Donnie|Frank|Roberta|Grandma)" story.txt |sort|uniq -c|sort -r

Use the */ trick to get only the directories, then use ${dir%/} to remove the trailing / you get

for dir in */ ; do echo "${dir%/}" ; done

Use perl regex (negative look-behind/look-ahead assertions) to get URLs.

grep -P -o '(?<=href=")http:\S+(?=")' *.html

Write 1MB (1048576) of random numbers to a file.

strings /dev/urandom | tr -c -d '0-9' | dd of=randomnumbers.txt bs=1 count=1M

cause cpuload with md5sum

md5sum -c /dev/urandom

rig is this cool little program that generates random identities.

sudo apt-get install rig

Time for some Mathematics, this command output all the possible factors of a given number.

factor

shred a Harddisk before selling

This makes 30 passes by overwriting the entire hard disk with -z zeros:

sudo shred -vfz -n 30 /dev/sdx

Creating random passwords which contains no special characters, is 10 characters long and displays 4

cat /dev/urandom| tr -dc 'a-zA-Z0-9' | fold -w 10| head -n 4

Generate a random password 30 characters long

strings /dev/urandom | tr -cd '[:alnum:]' | fold -w 30 | head -n 1

apg - generates several random passwords

apg

pwgen - generate pronounceable passwords

pwgen

Howto Recover a Linux Root Password

  1. Boot using a recovery CD or DVD.
  2. mount the drive
mount /mnt /dev/sda2
  1. Replace the existing root password with an empty one
sed /^root/s/.*:root:/root::0:0:root:/ /mnt/etc/passwd
  1. reboot
reboot

Grep All Email Addresses from a Text File Using Regular Expressions

grep -E -o "\b[a-zA-Z0-9.-]+@[a-zA-Z0-9.-]+\.[a-zA-Z0-9.-]+\b" filename.txt
grep -E -o -h "\b[a-zA-Z0-9.-]+@[a-zA-Z0-9.-]+\.[a-zA-Z0-9.-]+\b" -R /home/*
grep -E -o -h "\b[a-zA-Z0-9.-]+@[a-zA-Z0-9.-]+\.[a-zA-Z0-9.-]+\b" -R /home/* |sort|uniq -c|sort -nr
grep -E -o -h "\b[a-zA-Z0-9.-]+@[a-zA-Z0-9.-]+\.[a-zA-Z0-9.-]+\b" -R /home/* |sort|uniq -c|sort -nr | head -n 20

only domain name of email

grep -E -o -h "@[a-zA-Z0-9.-]+\.[a-zA-Z0-9.-]+\b" -R /home/oliver/*

Get list of top Emails domains

grep -E -o -h "@[a-zA-Z0-9.-]+\.[a-zA-Z0-9.-]+\b" -R /home/* |sort|uniq -c|sort -nr
grep -E -o -h "@[a-zA-Z0-9.-]+\.[a-zA-Z0-9.-]+\b" -R /home/* |sort|uniq -c|sort -nr | head -n 20
egrep -o "(mailto|ftp|http(s)?://){1}[^'\"]+" *

Pandoc a universal document converter

pandoc -s -S README -o example29.docx

Syntax highlighting of delimited code blocks:

pandoc code.text -s --highlight-style pygments -o example18a.html

SSL: Verifying that a Certificate matches a Private Key

$ (openssl x509 -noout -modulus -in server.pem | openssl md5 ;\
   openssl rsa -noout -modulus -in server.key | openssl md5) | uniq

Get sunrise and sunset times

This will get the sunrise and sunset times of a specific location. To be able to determine $l you need to first go to http://weather.yahoo.com/ and look up your location. The last numbers in the URL will be the $l

$ l=656958;curl -s http://weather.yahooapis.com/forecastrss?w=$l&u=c|grep astronomy| awk -F\" '{print $2 "\n" $4;}'
l=656958;curl -s curl http://weather.yahooapis.com/forecastrss?w=656958\&u=c | grep "condition" | awk -F\" '{print $5 $6;}'

Check all bash scripts in current dir for syntax errors

find . -name '*.sh' -exec bash -n {} \;

Job Control

You’re running a script, command, whatever.. You don’t expect it to take long, now 5pm has rolled around and you’re ready to go home… Wait, it’s still running… You forgot to nohup it before running it… Suspend it, send it to the background, then disown it… The ouput wont go anywhere, but at least the command will still run..

^Z $bg $disown

suspend and reattach a process to screen

longcmd <ctrl-z> bg; disown; screen; reptyr $(pid longcmd)

Silently Execute a Shell Script that runs in the background and won’t die on HUP/logout

This command runs your shell script in the background with no output of any kind, and it will remain running even after you logout.

nohup /bin/sh myscript.sh 1>&2 &>/dev/null 1>&2 &>/dev/null&

Mount a temporary ram partition

Makes a partition in ram which is useful if you need a temporary working space as read/write access is fast. Be aware that anything saved in this partition will be gone after your computer is turned off.

mount -t tmpfs tmpfs /mnt -o size=1024m

Show the last reboots in a List with Timestamps

last reboot

Simple Webserver with python

Serve current directory tree at http://$HOSTNAME:8000/

python -m SimpleHTTPServer

long complex bash command

Rapidly invoke an editor to write a long, complex, or tricky command

ctrl-x e

upower - Monitoring activity from the power daemon

upower --monitor-detail

create a Box around a given string

a function to create a box of ‘=’ characters around a given string. Call it with box test

box() { t="$1xxxx";c=${2:-=}; echo ${t//?/$c}; echo "$c $1 $c"; echo ${t//?/$c}; }
box test
box test X

Convert Softwarecode to nice html and print it in a pdf

code2html code.cpp > code.html

show printers

lpstat -p -d

print code in a pdf file

code2html code.cpp | lpr -P printer
lpr -P printer filename

How can I get octal file permissions from command line?

stat -c "%a %n" *

Files:

644 - The owner may read and write a file, while all others may only read the file. A common setting for data files that everybody may read, but only the owner may change.

664 - The owner and the group  may read and write a file, while all others only read the file

755 - The file's owner may read, write, and execute the file. All others may read and execute the file. This setting is common for programs that are used by all users.

Directorys:

777 - No restrictions on permissions. Anybody may list files, create new files in the directory and delete files in the directory. Generally not a good setting.

755 - The directory owner has full access. All others may list the directory, but cannot create files nor delete them. This setting is common for directories that you wish to share with other users.

700 - The directory owner has full access. Nobody else has any rights. This setting is useful for directories that only the owner may use and must be kept private from others.

To change all the directories to 755 (-rwxr-xr-x):

find /media/data -type d -exec chmod 755 {} \;

To change all the files to 644 (-rw-r–r–):

find /media/data/ -type f -exec chmod 644 {} \;

Appending timestamp stamp along with log file lines

cat input.log | sed -e "s/^/$(date -R) /" >> output.log

pipe the script’s output through a loop that prefixes the current date and time:

./script.sh | while IFS= read -r line; do echo "$(date) $line"; done >>/var/log/logfile

Create a 1 Gigabyte random File and sow the progress

SIZE=1; dd if=/dev/zero bs=1M count=$((SIZE*1024)) | pv -pters $((SIZE*1024*1024*1024)) | openssl enc -aes-256-ctr -pass pass:"$(dd if=/dev/urandom bs=128 count=1 2>/dev/null | base64)" -nosalt > randomfile

Show syscalls for parent and all child threads with execution time

strace -T -f -p <PID>
strace -tt -T -f <COMMAND>

Strace on the fly debugging

strace -c -p mypid

Show library calls

ltrace ./example

Change dynamic Library Path

LD_LIBRARY_PATH=$LD_LIBRARY_PATH: ./myprog
export LD_LIBRARY_PATH=/path/to/lib1:/path/to/lib2
./myprog

Split a large file, without wasting disk space

FILE=file_name; CHUNK=$((64*1024*1024)); SIZE=$(stat -c "%s" $FILE); for ((i=0; i < $SIZE; i+=$CHUNK)); do losetup --find --show --offset=$i --sizelimit=$CHUNK $FILE; done

Grep IP Address from ifconfig

/sbin/ifconfig eth0 | grep 'inet addr:' | cut -d: -f2 | awk '{ print $1}'

Rip a Youtube video for archiving, our from any other site

youtube-dl -tci --write-info-json "https://www.youtube.com/watch?v=dQw4w9WgXcQ"

Check your harddisk with smartmontools

sudo smartctl  -a /dev/sda

opens a file or URL in the user’s preferred application

xdg-open file.ps

Change caps lock to backspace

setxkbmap -option caps:backspace

Get HTTP status code with curl AND print response on new line

curl -s -o /dev/null -w "%{http_code}\n" localhost

Get HTTP Header of a Website

curl -I http://www.example.com

Watch who requests what page from lighttpd logs

tail -f access.log | awk '{print $1 , $11}'

count all the lines of code in a directory recursively

find . -name '*.cpp' | xargs wc -l

Clean apt-get and gpg cache and keys

sudo gpg --refresh-keys; sudo apt-key update; sudo rm -rf /var/lib/apt/{lists,lists.old}; sudo mkdir -p /var/lib/apt/lists/partial; sudo apt-get clean all; sudo apt-get update

Secure netcat chat - SSH

ssh hostname nc -l 9876

Reload all sysctl variables without reboot

sysctl --system

Generate map of your hardware

lstopo -p -v --whole-system --whole-io output.svg

Show your local ipv4 IP

ip -o -4 a s | awk -F'[ /]+' '$2!~/lo/{print $4}'

Monitor Harddisk with Smartmontools

sudo apt-get install smartmontools
sudo smartctl -a /dev/sda
sudo smartctl -a /dev/sda | grep Load_Cycle_Count

SSDs und neue Festplattenmodelle überprüfen

Datenbank für Festplattenmodelle updaten

sudo update-smart-drivedb

Abfrage des SSD Status

sudo smartctl -a /dev/sda
sudo smartctl -H /dev/sda

Verzeichnisse sortiert durchblättern

ls | sort | more

Get your external IP address

curl ifconfig.me/ip

List old kernel packages, that is other than the active one

dpkg -l 'linux-*' | sed '/^ii/!d;/'"$(uname -r | sed "s/\(.*\)-\([^0-9]\+\)/\1/")"'/d;s/^[^ ]* [^ ]* \([^ ]*\).*/\1/;/[0-9]/!d'

stat - display file or file system status

man stat
man 2 stat
stat filename
stat /dev/ttyS0
stat -f /dev/sda1

Renaming files in bulk

rename -v 's/DSC/birthday-party/' *.jpg
DSC_001.jpg renamed as birthday-party-001.jpg
DSC_002.jpg renamed as birthday-party-002.jpg
DSC_003.jpg renamed as birthday-party-003.jpg

replace all spaces in the file names

rename -v 's/ //g' *

Use screenkey to display keystrokes

screenkey

Strings

This commands looks for ASCII strings embedded in binary files

strings -f /usr/lib/lib*

tput - find out how many colors a terminal supports with

tput colors

show only file name without the entire directory path

ls whateveryouwant | xargs -n 1 basename

Show last changed file

ls -Art * | tail -n 1

Recursive and grouping and piping into vim

ls -XR | vim -

Start a Job at specific time

echo "bash /path/to/yourscript" | at 16:30

Add user to dialout group

sudo usermod -a -G dialout $USER

Watch CPU MHz of all cores

watch "cat /proc/cpuinfo | grep \"cpu MHz\""

Adds Timestamps to a live traced log

tail -f file.txt | ts

System Information on the commandline

inxi -Fxz

Faketime - manipulate system time for a given command

faketime 'today 23:15' date

Speedtest mit iperf3

iperf3 -c speedtest.wtnet.de -p 5200 -P 10 -4

Server sendet Client empfängt:

iperf3 -c speedtest.wtnet.de -p 5200 -P 10 -4 -R

Links:

Last generation on 2024-10-06 00:20:11.


© 2024 Oliver Graute ⋅ hosted on a Raspberry PI ⋅ EmailMastodonTwitterGithubStackoverflowFlickr