Solved

Using grep with Python

Posted on 2010-09-24
8
1,410 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

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!

Question has a verified solution.

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

Introduction On September 29, 2012, the Python 3.3.0 was released; nothing extremely unexpected,  yet another, better version of Python. But, if you work in Microsoft Windows, you should notice that the Python Launcher for Windows was introduced wi…
Article by: Swadhin
Introduction of Lists in Python: There are six built-in types of sequences. Lists and tuples are the most common one. In this article we will see how to use Lists in python and how we can utilize it while doing our own program. In general we can al…
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…

730 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