Python for Network Engineers

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

Using Linux grep command

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

grep is a utility that allows you to print lines that match certain patterns $ ifconfig eth0 eth0 Link encap:Ethernet HWaddr fc:aa:14:01:cd:41 inet addr:192.168.1.73 Bcast:192.168.1.255 Mask:255.255.255.0 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:0 errors:0 dropped:0 overruns:0 frame:0 TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 RX bytes:0 (0.0 B) TX bytes:0 (0.

Using Linux Awk

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

awk is a utility that automatically splits lines on white space or tabs into columns and also features a full programming language $ ls -la total 4 drwxrwxr-x 2 geo geo 159 Jan 3 12:13 . drwxr-xr-x. 7 geo geo 4096 Jan 2 14:50 .. -rw-rw-r-- 1 geo geo 0 Jan 2 11:07 file1.

Using rsync to backup your files

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

What is the best utility to backup your files to another drive or to a remote server? the answer is rsync. Before you start complaining that rsync is not available in windows, I have to mention that windows 10 runs natively ubuntu if you enable the windows subsystem for linux. for information on how to enable it check this https://docs.