Category Archives: UNIX

Posts regarding UNIX and Linux systems

DNS records creator

Back around here. Quick post of a small script to create A and PTR dns records.

Input file. First column is the IP and second column is the fqdn. We call this file hosts.txt

10.124.12.34 athletic.abc.com
192.158.21.32 deportivo.abc.com
92.32.43.12 drac1.abc.com

Script below:

#!/bin/bash

while read line; do
        IP=`echo $line | awk '{print $1}'`
        HOST=`echo $line | awk '{print $2}'`
        PTR=`echo "${IP}" | awk -F\. '{print $4"."$3"."$2"."$1".in-addr.arpa."}'`
        echo "${HOST}. IN A ${IP}"
        echo "${PTR} IN PTR ${HOST}."
done < hosts.txt

Execution:

me@server:/tmp$ bash dnsconverter.sh
athletic.abc.com. IN A 10.124.12.34
34.12.124.10.in-addr.arpa. IN PTR athletic.abc.com.
deportivo.abc.com. IN A 192.158.21.32
32.21.158.192.in-addr.arpa. IN PTR deportivo.abc.com.
drac1.abc.com. IN A 92.32.43.12
12.43.32.92.in-addr.arpa. IN PTR drac1.abc.com.
me@server:/tmp$

Project Euler problem 30

Below is the solution to problem 30 from Project Euler. It’s written in Python, a programming language I’m trying to learn.

#!/usr/bin/env python 
# Project Euler problem 30

narc_sum = 0
for number in range(100, 200000):
        # Transform the number into a string
        string = str(number)
        # Transform the string into a list
        lst = list(string)
        sum = 0 
        for i in range(len(lst)):
                # Add the members of the list to the fifth power
                sum = sum + (int(lst[i])**5)
        if (number == sum):
                # Add all numbers and store in narc_sum value
                narc_sum = narc_sum + number
                print "Sum is ",narc_sum

Notify-send command

So the other day I was looking around and discovered the notify-send command. It basically sends notifications to the desktop. Pretty useful now adays.

[11:49:13] xavi@thinkpad: ~ $ sudo dpkg -S `which notify-send`
libnotify-bin: /usr/bin/notify-send
[11:49:15] xavi@thinkpad: ~ $

It is installed with libnotify-bin via aptitude.

[11:50:35] xavi@thinkpad: ~ $ sudo aptitude install libnotify-bin

So, once installed lets create a practical script. On my job I need to have a permanente VPN connection. Once in a while the VPN connection drops and we have to connect again. Lets create a simple script that notifies us when the connection drops.

#!/bin/bash

# Check tunnel interface
while "true"; do
        /sbin/ifconfig tun0 &> /dev/null
        while [ $? -ne 0 ]; do
                notify-send -t 5000 "VPN" "VPN is down"
        done
        sleep 300
done

We save and copy the script, give it execution permissions with chmod and send it to background.

[11:55:47] xavi@thinkpad: ~ $ chmod +x /tmp/check-vpn.sh; /tmp/check-vpn.sh &
[1] 15613
[12:00:43] xavi@thinkpad: ~ $

Here is a screenshot of it working.

vpn notify-send

Enjoy.