Python – Parse URLs from files in directory to check if links are up/down before executing yum on Centos.

This script parses the files in the yum repo directory and checks if all URLS are up before proceeding to execute yum. It creates two lists to do a sanity check. One list stores the number of URLs found and the second list stores the number of links that have been found to be working. If those two numbers do not match the execution of YUM is aborted. Please note, this script will work only with custom made yum repos.

import os
import subprocess
import re
import urllib2
import os.path


def check_Software():
    path = '/usr/bin/gcc'
    if os.path.isfile(path):
        pass
    else:
        try:
            subprocess.call(["yum", "install", "-y", "gcc"])
            if not os.patch.isfile(path):
                print "Sending email"

        except OSError as er:
            print er

def yum_link_Search():
    #These two lists are being used as control. The status list is used to append succesful HTTP connections
    #for each of the URLs captured from the files in the YUM directory. The latter is stored in the files_list list.
    #The number (amount of elements) is being compared. A success condition is when each site returns an alive code of
    #200. This tells us that all sites are up and running. View lines between 52 and 60
    status_list = []
    files_list = []
    src_dict = ("/etc/yum.repos.d/") #Specify base directory
    pattern = re.compile ('http\S+') #CPatter to search for

    for yum_files in os.listdir(src_dict): # obtain list of files in directory
        files = os.path.join(src_dict, yum_files) #join the full path with the names of the files.
        files_list.append(yum_files)
        strng = open(files) #We need to open the files
        for lines in strng.readlines(): #We then need to read the files
            if re.search(pattern, lines): #If we find the pattern we are looking for
                print re.split(r'=', lines)[1] #We split using as a delimeter the = sign.
                try:

                    status_link = urllib2.urlopen(re.split(r'=', lines)[1]).getcode()
                    #if  status_link == 200: #Check if the site is up
                    status_list.append(status_link)#View notes starting on line 29 for these conditionals
                    print status_list
                    print files
                    if len(files_list) > len(status_list): #View notes starting on line 29 for these conditionals
                        print "Not all Links are up - ABORTING"
                        exit();
                    if len(files_list) < len(status_list):
                        print "Not all Links are up - ABORTING"
                        exit();
                    elif len(files_list) == len(status_list):
                        print "Will run yum installer" #here we can call other functions since everything seems to be working fine.
                        #call the yum function here
                except StandardError as er:#Here we are capturing errors of other codes.
                    print "Site down"
                    print status_link



yum_link_Search()
Share Button

Python – Searching for text in multiple files within a directory OR one file in a directory

This script searches the yum directory for all the repo files and searches for the URLs each repo goes to.

Search for text in multiple files

src_dict = ("/etc/yum.repos.d/") #Specify base directory
pattern = re.compile ('http\S+') #CPatter to search for

for yum_files in os.listdir(src_dict): # obtain list of files in directory
    files = os.path.join(src_dict, yum_files) #join the full path with the names of the files.
    strng = open(files) #We need to open the files
    for lines in strng.readlines(): #We then need to read the files
        if re.search(pattern, lines): #If we find the pattern we are looking for
            print re.split(r'=', lines)[1] #We split using as a delimeter the = sign.
            #INSERT WHATEVER CODE YOU WANT

Search for text in one file in a directory

drc = '/home/gmastrokostas/PycharmProjects/learning'
f = open( os.path.join(drc, file) )
for lines in f.readlines():
    searpatt = re.search(pattern, lines)
    if re.search(pattern,lines):
        print (searpatt.group())
Share Button

Python: Check IPs for DNS entries and see if host is UP or DOWN. Export info to a CSV file

Checks IPs if they have a DNS entry. If they do it checks if the host is up. If an IP does not have a DNS entry it reports it. It does not report if an IP with out a DNS is down or not

import socket
import subprocess
import  netifaces
import csv
 
def checkPING(IP):
    try:
        ping = subprocess.check_output(['ping', '-c1', ip])
        return "Host is UP"
    except:
        return "Host is DOWN"
 
def checkDNS(IP):
    try:
        dns = socket.gethostbyaddr(ip)
        return dns[0]
    except:
        return "No DNS entry found"
 
 
ip_list = []
dns_list = []
status_list = []
 
csvfile= open('file.csv', 'w')
for loop_ip in range (62):
    ip = '10.0.0.%d' % loop_ip
    print ip, checkDNS(ip), checkPING(ip)
    #data = ip+" ",checkDNS(ip)+" ",checkPING(ip)
    #data_list.append(data)
    ip_list.append(ip)
    dns_list.append(checkDNS(ip))
    status_list.append(checkPING(ip))
writer = csv.writer(csvfile, dialect='excel')
writer.writerows(zip(ip_list, dns_list, status_list))
 
 
'''
#These lines will print each element of the list on a row
writer.writerow(ip_list)
writer.writerow(dns_list)
writer.writerow(status_list)
'''
 
'''
#Also possible threading implementation
for threads_1 in range(1):
    threads_ping.append(threading.Thread(target=executeCheck()))
    threads_ping[-1].start


for threads_2 in range(num_dns):
    threads_dns = append(threading.Thread(target=checkDNS))
    threads_dns.start()

for threads in threads_ping:
    threads.join()

for threads in threads_dns:
    threads.join()
'''
 
OUTPUT:
10.0.0.5 No DNS entry found Host is UP
10.0.0.6 No DNS entry found Host is DOWN
10.0.0.7 No DNS entry found Host is DOWN
Share Button

Python – Fabric: Connect to a remote server and execute few simple commands.

Fabric is a Python module used to connect to remote servers with SSH and execute various commands. The script below provides the IP for the server to connect to along with the username and password. Then a function is defined with the commands we wish to execute. We execute the script by typing the following command. It provides the name of the file and it also calls the function we defined.

fab -f testin.py uptime
#!/usr/bin/python
from fabric.api import *

env.use_ssh_config = False
env.hosts = ["10.0.0.20"]
env.user = "root"
#env.key_filename = "/root/.ssh/id_rsa"
env.password = "Password!"
env.port = 22


def uptime():
        run("uptime")
        run ("ls -ltr")
        
Share Button

Python – Paramiko module

Paramiko is a Python module that is used to remotely connect via SSH to remote servers and execute commands on that server. The script below connects to a remote server with the username and password provided in order to transfer a file. It also changes the permissions of the file transferred.

import sys
import paramiko
sys.stderr = open('/dev/null')  
import psutil

ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())

ssh.connect('10.0.0.10', username='root', password='JamesBond')
sftp = ssh.open_sftp()
sftp.put('/home/Batman/Documents/python/scripts/testing.py', '/root/testing.py')
stdin, stdout, stderr = ssh.exec_command("chmod +x /root/testing.py")
stdin, stdout, stderr = ssh.exec_command("python /root/testing.py")

print stdout.channel.recv(1024)

ssh.close()
Share Button

Python – Get hostname and IP from remote hosts.

The script resolved IPs to hostnames and matches the IP with the hostname. The script is meant for inventory purposes.

#!/usr/bin/python
from netaddr import IPNetwork
import socket
import netifaces as nif



for loop_ip in ['10.0.0.2', '10.0.0.6']:

    try:
        #Will get the hostname and IP address.
            dns = socket.gethostbyaddr(loop_ip)
            hostnm = dns[0]
            ipadd  = (", ".join(dns[2]))
            print hostnm.ljust(10), ipadd.rjust(20)
        

    except socket.error, msg:
            print msg
Ooutput
win8.sfentona.lol             10.0.0.2
ad1.sfentona.lol             10.0.0.6
Share Button

Python regex examples

Write text from file temp1.txt to named.txt (use the ‘a’ option if you want to append instead)

 
#!/usr/bin/python
import os
import re
with open("named.txt", "wt") as fout:
    with open("templ.txt", "rt") as fin:
        for line in fin:
            fout.write(line)

Search for a string in a file and display the Result

 
import re
text="XXX"
file ='templ.txt'
for line in open(file):
    if text in line:
        print line

Search for a string by using compile in a file and display the Result

 
import re
text = "XXX"
pattern = re.compile(text)
file = 'templ.txt'
of = open(file)
for line in of.readlines():
    if re.search(pattern,  line):
        print line

Append the contents of a file to another file

import re
import os
fo = open("templ.txt","r")
fi = open("named.txt","a")
for line in fo:
    fi.write(line)
    #print line
fi.close()
fo.close()

Isolating text field from a variable

text = '11:47:55.045 -T- MFS_TEST10 Received FIX Message 3  8=FIX.4.2|9=146|35=D|34=3|49=MFS_TEST10|52=20120726-15:47:55|56=CCG|115=YYYC|11=AAA 0981/07262012|54=1|38=100000|55=ACL|40=1|47=A|60=20120726-15:47:55|21=1|207=N|10=044|'
print re.split(r'\s', text)[2]
Output
MFS_TEST10

Isolating text fields from a file.

 import re
#!/usr/bin/python
import re
import os
import fnmatch
fo = open("templ.txt", "r")
for line in fo:
        print line
        print re.split(r'\s', line, re.I|re.M)[1]
Provided the text with in the file was "Road 123 Str NY USA 11214"
Output 123

Searching for specific type files.

 

filenames = ['httpd.conf', 'samba.conf', 'header.h', 'socket.net']

[loop for loop in filenames if loop.endswith(('.h'))]
for loop in filenames:
    if loop.endswith('.h') or loop.startswith('httpd'):
        print loop
Output
httpd.conf
header.h

Searching through a List

 
#Great use with os.dirlist
addresses = [
    '5412 N CLARK ST',
    '1060 W ADDISON ST',
    '1039 W GRANVILLE AVE',
    '2122 N CLARK ST',
    '4802 N BROADWAY',
]

for loop in addresses:
    if fnmatch.fnmatchcase(loop, '* ST'):
        print loop
Output
5412 N CLARK ST
1060 W ADDISON ST
 2122 N CLARK ST

Print specific fields from a string

 
text = 'Today is 11/27/2012. PyCon starts 3/13/2013.'
pattern = re.compile('Today\s\w+\s\d+\W\d+\W\d+', re.IGNORECASE)
searpatt = re.search(pattern, text)
print (searpatt.group())

Output

Today is 11/27/2012

Replace specific fields of text

print text
#!/usr/bin/python
import re

text = 'Today is 11/27/2012. PyCon starts 3/13/2013.'
newt = "Yesterday"
pattern = re.compile('Today\s\w+\s\d+\W\d+\W\d+', re.IGNORECASE)
searpatt = re.search(pattern, text)
print re.sub(pattern, newt, text)

Output

Yesterday. PyCon starts 3/13/2013.

Print a specific element of the split you created

#!/usr/bin/python
import re

text = 'Today is 11/27/2012. PyCon starts 3/13/2013.'
newt = "Yesterday"
pattern = re.compile('Today\s\w+\s\d+\W\d+\W\d+', re.IGNORECASE)
searpatt = re.search(pattern, text)
print text
print re.split(pattern, text )[1]

PyCon starts 3/13/2013.

Replace strings in a file

#!/usr/bin/python
import os
import re
text_ip = "#listen_addresses = 'localhost'"
        subtext_ip = "listen_addresses = '10.0.0.27'"
        text_port = "#port = 5432"
        subtext_port = "port = 5432"

        s = open("/home/postgres/postgres94/postgresql.conf").read()
        s = s.replace(text_ip, subtext_ip)
        s = s.replace(text_port, subtext_port)
        f = open("/home/postgres/postgres94/postgresql.conf", 'w')
        f.write(s)
        f.close()
Share Button

A note to visitors

This web site is used for me to store information/skills I acquire in order to retain it.  These instructions are not meant for productions environments. They serve as a mere blueprint on how to do certain things.

Thank you.

Share Button

Etherchannel between two CISCO switches.

This how to shows how to setup Etherchannel links between two switches. A trunk link was first established between the two switches and then the Etherchannel links were created using the PAgP protocol. You can download the PacketTrace file from here.

snapshot1

The following commands will create a trunk between the two switches. You can run the command on only one switch.

 
switchport fastEthernet 0/1
switchport mode trunk
switchport trunk allowerd vlan 1-99,150 #in case you have VLANS

The following commands will need to be run on both switches.

 
interface  range  fastEthernet  0/1-4
channel-protocol  pagp
channel-group 1 mode  desirable

The following modes are available.

1
2
3
4
5
6
channel-group 1 mode ?
active Enable LACP unconditionally
auto Enable PAgP only if a PAgP device is detected
desirable Enable PAgP unconditionally
on Enable Etherchannel only
passive Enable LACP only if a LACP device is detected
Share Button

EIGRP with VLANs

This how to is a continuation of the how to found here.  The Packet Tracer of this how to can be found here.

In this how to, the the EIGRP routing protocol is being deployed. In addition the VLANs will be able to communicate with two new networks introduced, 10.0.0.0/8 and 192.168.5.192/27. Overall most of the the networks used in these examples are not efficiently broken down with VSLM. The focus for now is to understand and learn routing and routing protocols.

In this example we will use EIGRP with the VLANs we already created which will be able to communicate with the two new networks as mentioned above.

EIGRP

 

Make sure Router 0 can handle the traffic from the VLANs. We managed to do that by running the following commands in the previous how to. You can verify the router is aware of the VLANs by pinging the VLAN router interfaces 192.168.50.100 and so forth. Below are the commands that we used to make Router 0 aware of the VLANS.

On Router 0 run the following only if you cannot ping from one of the workstations the network address for each VLAN:

For VLAN 50

 
interface fastEthernet 0/0.50
encapsulation dot1Q 50
ip address 192.168.50.100 255.255.255.0

For VLAN 60

 
interface fastEthernet 0/0.60
encapsulation dot1Q 60
ip address 192.168.60.100 255.255.255.0

For VLAN 99

 
interface fastEthernet 0/0.99
encapsulation dot1Q 99
ip address 192.168.99.100 255.255.255.0

For VLAN 150

 
interface fastEthernet 0/0.150
encapsulation dot1Q 150
ip address 192.168.150.100 255.255.255.0

 

Configure the FastEthernet and Serial connections of Router 0

The FastEthernet 0/0 is not assigned an IP address simply because it handles traffic from all VLANs.

For Interface FastEthernet 0/1 execute

interface FastEthernet 0/1
ip address 10.0.0.1 255.0.0.0
no shutdown

For Interface Serial 0/2/0 execute

interface Serial 0/2/0
ip address 192.168.5.225 255.255.255.252
no shutdown

For Interface Serial 0/2/1 execute

interface Serial 0/2/1
clock rate 64000
ip address 192.168.5.234 255.255.255.252
no shutdown

Setup EIGRP on Router 0

router eigrp 1
network 10.0.0.0  0.255.255.255
network 192.168.5.224  0.0.0.3
network 192.168.5.232  0.0.0.3

 

Configure the FastEthernet and Serial connections of Router 1

For Interface FastEthernet 0/0 execute

interface FastEthernet 0/0
ip address 192.168.5.1 255.255.255.128
no shutdown

For Interface Serial 0/2/0 execute

interface Serial 0/2/0
ip address 192.168.5.229 255.255.255.252
no shutdown

For Interface Serial 0/2/1 execute

ip address 192.168.5.226 255.255.255.252
clock rate 64000
no shutdown

Setup EIGRP on Router 1

router eigrp 1
network 192.168.5.0   0.0.0.127 
network 192.168.5.228 0.0.0.3   
network 192.168.5.224 0.0.0.3

 

Configure the FastEthernet and Serial connections of Router 2

For Interface FastEthernet 0/0 execute

interface FastEthernet 0/0
ip address 192.168.5.193 255.255.255.224
no shutdown

For Interface Serial 0/2/0 execute

interface Serial 0/2/0
ip address 192.168.5.233 255.255.255.252
no shutdown

For Interface Serial 0/2/1 execute

interface Serial 0/2/1
clock rate 64000
ip address 192.168.5.230 255.255.255.252
no shutdown

Setup EIGRP on Router 1

router eigrp 1
network 192.168.5.192  0.0.0.31 
network 192.168.5.232  0.0.0.3  
network 192.168.5.228  0.0.0.3 
Share Button