Adventures in CS

Bash Cheatsheet

20 May 2019

Here a set of Bash one-liners that I have used in past

Starting ssh-agent in the background:

$ eval "$(ssh-agent -s)"

Adding ssh key to ssh-agent:

$ ssh-add -K <ssh private key>

Run shell script when a file or directory changes:

Install entr using apt install entr

For changes to file:

ls -d *.c | entr sh -c 'make && make test'

For changes to directories:

while true; do find path/ | entr -d echo Changed; sleep 10; done

or

while true; do ls path/* | entr -pd echo Changed; sleep 10; done

Starting a remote ssh command and shifting it to background and getting the PID of the process:

ssh -n me@example.com "nohup myscript.sh >/dev/null 2>&1 &"
SERV_PID=$(ssh me@example.com 'echo $!')

Adding numbers to the beginning of every line:

awk '{printf("%010d %s\n", NR, $0)}' example.txt

Local and remote variable inside an SSH command:

A=3;
ssh host@name "B=3; echo $A; echo \$B;"

Remove the first line of a file:

tail -n +2 "$FILE"

Increment a variable:

var=$((var+1))
((var=var+1))
((var+=1))
((var++))

Splitting string by the first occurrence of a delimiter:

id="$( cut -d ';' -f 1 <<< "$s" )";

Disable password authentication:

Add the following to /etc/ssh/sshd_config

PasswordAuthentication no
ChallengeResponseAuthentication no
UsePAM no

Then restart ssh using sudo systemctl restart ssh

Changing java version:

sudo update-alternatives --config java

Useful CLI tools

TC tool for traffic control:

TC can only limit egress bandwidth (and do a bunch of other things like delays, packet loss and packet corruption). We can use the following command to limit egress bandwidth

tc qdisc add dev <interface> root tbf rate 1mbit latency 1us

You can remove the above limit using:

tc qdisc remove dev <interface> root tbf rate 1mbit latency 1us

IPERF for testing network performance:

On server side use iperf -s

On client side use iperf -c <serverip>

References:

Tweet me @mb_ce if you like this post.

Tweet
comments powered by Disqus