Solved

Using grep with Python

Posted on 2010-09-24
8
1,464 Views
Last Modified: 2013-11-05
Hello and Thank you in advance.

I am trying to grep "dropped" from the command i run within the python. The command is a simple /sbin/ifconfig.

I have it being written to a file but would like to have it being kept in mem. Currently when i run the script i get the following.

          RX packets:3702045 errors:0 dropped:0 overruns:0 frame:0
          TX packets:3587203 errors:0 dropped:0 overruns:0 carrier:0
          RX packets:310 errors:0 dropped:0 overruns:0 frame:0
          TX packets:310 errors:0 dropped:0 overruns:0 carrier:0

I am trying to make a script to alert me if we drop packets. So if the number equals 0 we are fine if it is anything else besides 0 then it will alert me.

I know the script can be made with perl as well but i want to learn python.

Thanks!
#!/usr/bin/env python

import os
import subprocess

ifconfig = os.popen('/sbin/ifconfig | grep dropped').read()

fname = open("/tmp/ifconfig.tmp",'w')
fname.write(ifconfig)
fname.close()

Open in new window

0
Comment
Question by:Realtime20
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
8 Comments
 
LVL 9

Accepted Solution

by:
zaghaghi earned 125 total points
ID: 33755437
Hi,

I used some string manipulations to get 'dropped' values, and checked them against 0. See the attached code.
import time
import os
import subprocess
import sys

def checkDrops():
    ifconfig = os.popen('/sbin/ifconfig | grep dropped').read()
    lines= ifconfig.split('\n')
    for l in lines:
        keys =l.strip().split()
        if len(keys)>3:
            drops = keys[3]
            ndrops = drops.split(':')[1]
            if int(ndrops) >0:
                return False
    return True

try:
    if sys.argv[1]=='s':
        sleeptime = int(sys.argv[2])
except:
    sleeptime = 20
    
while true:
    if checkDrops():
        print "Ooops,  we have drops" # or you can do something else
    time.sleep(sleeptime)    # wait sleeptime seconds

Open in new window

0
 
LVL 9

Expert Comment

by:zaghaghi
ID: 33755453
Ooops, change
while true:
to
while True:
0
 

Author Comment

by:Realtime20
ID: 33755478
This looks like what i need. I can modify a few things but gives me the best examples of how to use.

Thanks again for the help.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Closing Comment

by:Realtime20
ID: 33755481
Gave a perfect example of what i need to learn and what i can use.
0
 
LVL 9

Expert Comment

by:zaghaghi
ID: 33755485
Oops again, sorry for bad code. Use this one
import time
import os
import subprocess
import sys

def checkDrops():
    ifconfig = os.popen('/sbin/ifconfig | grep dropped').read()
    lines= ifconfig.split('\n')
    for l in lines:
        keys =l.strip().split()
        if len(keys)>3:
            drops = keys[3]
            ndrops = drops.split(':')[1]
            if int(ndrops) >0:
                return False
    return True

sleeptime = 10
try:
    if sys.argv[1]=='s':
        sleeptime = int(sys.argv[2])
except:
    pass
    
print sleeptime
    
while True:
    if not checkDrops():
        print "Ooops,  we have drops" # or you can do something else
    else:
        print "."
    time.sleep(sleeptime)    # wait 60 seconds

Open in new window

0
 
LVL 9

Expert Comment

by:zaghaghi
ID: 33755522
Also you can use raw_input.
if we save the below script as drops.py then we can use this command:

ifconfig | grep dropped | python drops.py
import os
import subprocess

def checkDrops():
    ifconfig = raw_input()
    lines= ifconfig.split('\n')
    for l in lines:
        keys =l.strip().split()
        if len(keys)>3:
            drops = keys[3]
            ndrops = drops.split(':')[1]
            if int(ndrops) >0:
                return False
    return True

if not checkDrops():
    print "Ooops, we have packet drops."
else:
    print "We haven't packet drops."

Open in new window

0
 
LVL 29

Expert Comment

by:pepr
ID: 33767707
There is a chance that you can avoid calling the grep completely.  As the grep does "general regular expression parsing", you can use the re Python module directly to do the same with the lines.  As the code should check only against the value different than zero, there is even no need to convert the value to int.  So, the core could be rewritten this way (the open of data.txt simulates the result of ifconfig).  I suggest also to use the subprocess module for getting the output from the ifconfig.
import re


def checkDrops(f):
    rexDropped = re.compile('dropped:(?P<num>\d+)\s')
    for line in f:
        m = rexDropped.search(line)
        if m:                          # If found
            if m.group('num') != '0':  # ... and the number is not zero
                return True            # ... do terminate immediately.
    return False                       # No drops found.
         
         
# Just for testing.
f  = open('data.txt')
areSomeDrops = checkDrops(f)
f.close()

if areSomeDrops:
    print 'Packet drops happened.'

Open in new window

0
 
LVL 29

Expert Comment

by:pepr
ID: 33767798
Here is the core of the calling the ifconfig and processing it directly in Python.  Warning, I did not checked under Unix, you may observe some problems. Check what happens with the launched ifconfig processes after termination of the Python script, for example.
import re
import subprocess

def checkDrops():
    rexDropped = re.compile('dropped:(?P<num>\d+)\s')
    proc = subprocess.Popen('/sbin/ifconfig', stdout=subprocess.PIPE)
    for line in proc.stdout:
        m = rexDropped.search(line)
        if m:
            print line                # just for debugging to see that line
            if m.group('num') != '0': # non-zero drops
                return True
    return False            
         
         
if checkDrops():
    print 'Packet drops happened.'
    # Not waiting for finishing the ifconfig process...

Open in new window

0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Variable is a place holder or reserved memory locations to store any value. Which means whenever we create a variable, indirectly we are reserving some space in the memory. The interpreter assigns or allocates some space in the memory based on the d…
Strings in Python are the set of characters that, once defined, cannot be changed by any other method like replace. Even if we use the replace method it still does not modify the original string that we use, but just copies the string and then modif…
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question