Python for Network Engineers

Bash scripting 101

By George El. • January 2019 • Reading time: 6 minutes

In this post I describe basic features of the bash shell Variables Variables by convention use uppercase characters and are case sensitive. There are builtin variables like HOME, USER, PWD and user defined variables. To print a variable you precede it with $ or put it in ${} $ VAR1=1 $ echo "$VAR1" 1 $ VAR2="Hello" $ echo "$VAR2" Hello $ echo $USER geo555 $>echo ${USER} geo555 you may have noticed that I enclose variables in quotes when printing them.

How to Sort IP Addresses in Bash in One Line

By George El. • January 2019 • Reading time: 1 minute

Lets assume we have the following file with IPs and we want to sort them. Obviosuly if we sort them like strings, we won’t get what we want. We want to sort on the first octet, then the second, then the third, then the fourth. more IPs.txt 120.

Find the Total Size of All Files With Certain Extension with one Line

By George El. • January 2019 • Reading time: 2 minutes

find all files with a certain extension in current directory and add the file sizes to calculate total file size in Mbytes find . -iname "*.txt" | xargs ls -l | awk '{print $5 }' | paste -sd+ | bc | awk '{print $1/1024/1024 }' the above example finds all files ending in txt, starting from the current directory.

Bash expansion

By George El. • January 2019 • Reading time: 4 minutes

In this post I am going to talk about expansion in bash. But first we need to talk about how bash interprets each line in a script. Bash does five things. Words that are not variable assignments or redirections are expanded First word becomes command remaining words become arguments Redirections are performed Variables are expanded and assigned does not include brace and process substitution assignments are valid only for this command if there is no command assignemnt becomes permanent Alias expansion is applied if the command was not quoted The identified function, builtin or external program is executed with the arguments There are several types of expansion and are done in the following order and from left to right:

Using Linux Find Command

By George El. • November 2018 • Reading time: 3 minutes

The find command allows to find files based on various criteria, some of which are: maxdepth : the number of directories depth type f : files type d: directories name : the name of the files like “*.txt” iname : same as above but ignore case size +10M : find files overs 10Mbytes size -5k: find files less than 5Kbytes size +1G : find files larger than 1gig mtime -7 : shows files modified last 7 days mtime -30 : shows files modified last 30 days ok prompt the user for permission Examples copy all files from /home/geo/temp to /tmp (you can also use xargs)

Using Linux Sed Command

By George El. • November 2018 • Reading time: 2 minutes

In this post I provide examples of using the sed command $ cat test1.txt | sed -r '' 1 text1 2 text2 3 text3 4 text4 5 text5 6 text6 7 text7 8 text8 9 text9 10 text10 $ cat test1.txt | sed -r '2,4d' 1 text1 5 text5 6 text6 7 text7 8 text8 9 text9 10 text10 $ cat test1.

Using Linux Join Command

By George El. • November 2018 • Reading time: 2 minutes

In this post I demontrate the linux join command $ cat 5.txt 1;orange1 2;orange2 3;orange3 4;orange4 5;orange5 6;orange6 7;orange7 8;orange8 9;orange9 10;orange10 $ cat 6.txt 1;apple1 2;apple2 3;apple3 4;apple4 5;apple5 6;apple6 7;apple7 8;apple8 9;apple9 10;apple10 $ join -t ';' -j 1 -o 1.1,1.2,2.1,2.2 5.txt 6.txt 1;orange1;1;apple1 2;orange2;2;apple2 3;orange3;3;apple3 4;orange4;4;apple4 5;orange5;5;apple5 6;orange6;6;apple6 7;orange7;7;apple7 8;orange8;8;apple8 9;orange9;9;apple9 10;orange10;10;apple10 deleted lines 4 and 5 $ cat 6.

Using Linux Sort Command

By George El. • November 2018 • Reading time: 2 minutes

Sort is a linux command that allows us to sort files. Lets see some examples $ cat /etc/passwd root:x:0:0:root:/root:/bin/bash daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin bin:x:2:2:bin:/bin:/usr/sbin/nologin sys:x:3:3:sys:/dev:/usr/sbin/nologin sync:x:4:65534:sync:/bin:/bin/sync games:x:5:60:games:/usr/games:/usr/sbin/nologin man:x:6:12:man:/var/cache/man:/usr/sbin/nologin lp:x:7:7:lp:/var/spool/lpd:/usr/sbin/nologin mail:x:8:8:mail:/var/mail:/usr/sbin/nologin news:x:9:9:news:/var/spool/news:/usr/sbin/nologin uucp:x:10:10:uucp:/var/spool/uucp:/usr/sbin/nologin proxy:x:13:13:proxy:/bin:/usr/sbin/nologin www-data:x:33:33:www-data:/var/www:/usr/sbin/nologin backup:x:34:34:backup:/var/backups:/usr/sbin/nologin list:x:38:38:Mailing List Manager:/var/list:/usr/sbin/nologin irc:x:39:39:ircd:/var/run/ircd:/usr/sbin/nologin gnats:x:41:41:Gnats Bug-Reporting System (admin):/var/lib/gnats:/usr/sbin/nologin nobody:x:65534:65534:nobody:/nonexistent:/usr/sbin/nologin systemd-timesync:x:100:102:systemd Time Synchronization,,,:/run/systemd:/bin/false systemd-network:x:101:103:systemd Network Management,,,:/run/systemd/netif:/bin/false systemd-resolve:x:102:104:systemd Resolver,,,:/run/systemd/resolve:/bin/false systemd-bus-proxy:x:103:105:systemd Bus Proxy,,,:/run/systemd:/bin/false syslog:x:104:108::/home/syslog:/bin/false _apt:x:105:65534::/nonexistent:/bin/false lxd:x:106:65534::/var/lib/lxd/:/bin/false messagebus:x:107:111::/var/run/dbus:/bin/false uuidd:x:108:112::/run/uuidd:/bin/false dnsmasq:x:109:65534:dnsmasq,,,:/var/lib/misc:/bin/false sshd:x:110:65534::/var/run/sshd:/usr/sbin/nologin pollinate:x:111:1::/var/cache/pollinate:/bin/false geo555:x:1000:1000:,,,:/home/geo555:/bin/bash mysql:x:112:116:MySQL Server,,,:/nonexistent:/bin/false lets sort this without any options

Using Linux Xargs

By George El. • October 2018 • Reading time: 4 minutes

xargs is very useful utility because it takes standard input and passes it to the next function. In most cases it can replace a for loop lets say I wanna zip each txt file1-9 individually $ls -1 *.txt | xargs -n1 gzip file1.txt.gz file2.txt.gz file3.txt.gz file4.txt.gz file5.txt.gz file6.txt.gz file7.txt.gz file8.txt.gz file9.

Using Sed

By George El. • October 2018 • Reading time: 5 minutes

sed is a very useful utility and certainly a whole book can be written for sed, Here are some basic examples $ ls -1 file1.txt file2.txt file3.txt file4.txt file5.txt file6.txt file7.txt file8.txt file9.txt delete last line $ ls -1 | sed '$d' file1.txt file2.txt file3.txt file4.txt file5.txt file6.txt file7.txt file8.txt delete first line