Python for Network Engineers

Lambda Expressions in Python

By George El. • March 2019 • Reading time: 3 minutes

In this post I will explain lambda expressions in python. Simply put, lambda expressions is another way to create a function in python. However this function has some restrictions. It does not have a name (that’s why they are also called anonymous), they can not have assignments they do not have a ‘return’ keyword and they can only be one line.

How to Sort IP Addresses With Python

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

Python provides a a sort and sorted function. Sort changes the original list, while sorted creates a copy of the list, without changing the original list. So the sort version is faster, but it works only on lists, while sorted works on any iterable. Now the problem is that if I try to sort IPs like strings, they won’t be sorted properly.

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 255.1.1.1 1.1.1.1 120.10.1.5 120.

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