Calculate IP


Address: 192.168.0.1 11000000.10101000.00000000 .00000001
Netmask: 255.255.255.0 = 24 11111111.11111111.11111111 .00000000
Wildcard: 0.0.0.255 00000000.00000000.00000000 .11111111
=>
Network: 192.168.0.0/24 11000000.10101000.00000000 .00000000 (Class C)
Broadcast: 192.168.0.255 11000000.10101000.00000000 .11111111
HostMin: 192.168.0.1 11000000.10101000.00000000 .00000001
HostMax: 192.168.0.254 11000000.10101000.00000000 .11111110
Hosts/Net: 254 (Private Internet)

References :

http://jodies.de/ipcalc

Cisco Router IP Traffic Export


R1>enable
R1#configure terminal
R1(config)# ip traffic-export profile MyProfile
R1(conf-rite)# interface f0/0
R1(conf-rite)# mac-address 0001.0203.0405
R1(conf-rite)# bidirectional
R1(conf-rite)# exit

R1(config)# interface s0/0
R1(config-if)# ip traffic-export apply MyProfile 

R1# show ip traffic-export

References :

http://www.cisco.com/c/en/us/td/docs/ios/12_4t/12_4t11/ht_rawip.html http://packetlife.net/blog/2009/oct/26/capturing-serial-traffic-ethernet-rite/
http://keepingitclassless.net/2010/10/raw-ip-traffic-export-rite-on-cisco-ios/

Join bytes in Python

def run_server():
    HOST = ''
    PORT = 11000
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.bind((HOST, PORT))
    s.listen(10)

    while True:
        conn, addr = s.accept()
        print("{0} - ".format(get_counter()) + 'Connection from : {0}'.format(addr))

        data_recv=conn.recv(1024)
        data=bytearray()

        conn.settimeout(1)

        while len(data_recv)>0:
            data.extend(data_recv)
            try:
                data_recv=conn.recv(1024)
            except socket.timeout:
                break

        data=str(data)
        if data=="1":
            message=get_message1()
            conn.sendall(message)
        conn.close()

References :

http://stackoverflow.com/questions/9099145/where-are-python-bytearrays-used

Bytes stream in Python

    @staticmethod
    def from_bin(dataB):
        reader=io.BytesIO(dataB)
        length1= reader.read(4)
        length2=reader.read(4)
        length3=reader.read(4)
        macs=reader.read(length1)
        uid=reader.read(length2)
        signature=reader.read(length3)
        reader.close()

        message=Message(macs,uid,signature)
        return message

References :
https://docs.python.org/3/library/io.html#io.BytesIO http://chimera.labs.oreilly.com/books/1230000000393/ch05.html#_solution_78
https://docs.python.org/2/library/io.html

Serialize memory objects to binary in Python

try:
    import cPickle as pickle
except:
    import pickle
import pprint

data1 = [ { 'a':'A', 'b':2, 'c':3.0 } ]
print 'BEFORE:',
pprint.pprint(data1)

data1_string = pickle.dumps(data1)

data2 = pickle.loads(data1_string)
print 'AFTER:',
pprint.pprint(data2)

print 'SAME?:', (data1 is data2)
print 'EQUAL?:', (data1 == data2)

References:

https://docs.python.org/3/library/pickle.html
https://docs.python.org/2/library/pickle.html
https://pymotw.com/2/pickle/

Broadcast address

IP

The broadcast address for an IPv4 host can be obtained by performing a bitwise OR operation between the bit complement of the subnet mask and the host’s IP address. In other words, take the host’s IP address, and set to ‘1’ any bit positions which hold a ‘0’ in the subnet mask.

Example: For broadcasting a packet to an entire IPv4 subnet using the private IP address space 172.16.0.0/12, which has the subnet mask 255.240.0.0, the broadcast address is 172.16.0.0 | 0.15.255.255 = 172.31.255.255.

255.240.0.0=11111111.11110000.00000000.00000000
172.16.0.0=10101100.00010000.00000000.00000000
10101100.00011111.11111111.11111111=172.31.255.255

Ethernet

Broadcast is possible also on the underlying Data Link Layer in Ethernet networks. Frames are addressed to reach every computer on a given LAN segment if they are addressed to MAC address FF:FF:FF:FF:FF:FF. Ethernet frames that contain IP broadcast packages are usually sent to this address.

References :

https://en.wikipedia.org/wiki/Broadcast_address

colorama – Python Package

Description:
Cross-platform colored terminal text

Available formatting constants are:

Fore: BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE, RESET.
Back: BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE, RESET.
Style: DIM, NORMAL, BRIGHT, RESET_ALL

Example :

from colorama import Fore, Back, Style
print(Fore.RED + 'some red text')
print(Back.GREEN + 'and with a green background')
print(Style.DIM + 'and in dim text')
print(Style.RESET_ALL)
print('back to normal now')

 

References :
https://pypi.python.org/pypi/colorama
http://stackoverflow.com/questions/287871/print-in-terminal-with-colors-using-python

Native file system watcher for Linux

for using with Jetbrains product like pycharm and idea :

The current limit can be verified by executing:

cat /proc/sys/fs/inotify/max_user_watches

It can be raised by adding following line to the /etc/sysctl.conf file:

fs.inotify.max_user_watches = 524288

or

echo fs.inotify.max_user_watches=524288 | sudo tee -a /etc/sysctl.conf

… and issuing this command to apply the change:

sudo sysctl -p

Check if there is a file in /etc/sysctl.d with your parameter. These files override the /etc/sysctl.conf file

References
https://github.com/guard/listen/wiki/Increasing-the-amount-of-inotify-watchers
https://serverfault.com/questions/355520/after-reboot-debian-box-ignore-sysctl-conf-values