Solved

Using grep with Python

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

Author Closing Comment

by:Realtime20
ID: 33755481
Gave a perfect example of what i need to learn and what i can use.
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
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 28

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 28

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Here I am using Python IDLE(GUI) to write a simple program and save it, so that we can just execute it in future. Because when we write any program and exit from Python then program that we have written will be lost. So for not losing our program we…
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
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…

705 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now