Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Using grep with Python

Posted on 2010-09-24
8
Medium Priority
?
1,588 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 500 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

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…
We are witnesses that everyone is saying that our children shouldn't "play" with a technology because it is dangerous. This article is going to prove that they are wrong.
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

610 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