Jump to content

Python Question


bobbyb1980

Recommended Posts

Hey guys. I am currently trying to write a program in Python that analizes a ifconfig output and gives further options for changing MAC, changing IP, loading the contents into airmon-ng, etc.

Currently what I do is import os and use that module to run "ifconfig > ifconfig.txt". Once I have ifconfig.txt confirmed I open that file then use the "re" module to compile it and search it for string of IP or MAC addys or whatever. This technique is effective but I believe there are more... pythonic ways of doing this as this way requires 2 output files, one for the original ifconfig.txt command and another for the results of the search. Or at least my book leads me to believe that.

I have been heavily researching the "subprocess" module. I have successfully been able to import subprocess and use subprocess to execute "ifconfig" (by piping it to STDOUT) and then I can call process.communicate() to view the output in a human friendly version. The problem is that the output appears as a tuple but is not iterable, ie I can't search it for MAC addy's or IP's. I have tried doing "list(process.communicate())" and assigning it a variable but that also generates errors.

I've looked at the code for similar python programs and there are people who have sucessfully done this and I imagine it's pretty common but I am not familiar with the concepts/code being use to do. Several other programs use the "cmd" module to run the command but the code I have seen appears obfuscated (to me at least) past that.

Will I be forever writing to files for output or is their a cleaner way to do this? Thanks fellas.

Link to comment
Share on other sites

Here's what I have so far. This is my first program so no laughing.

I'm only going to post the code that I've written that searches files for MAC's and IP's as the other parts aren't relevant to this question. If you see a way to do this by piping outputs and searching them from there please let me know!

import os
import re
###########
##FUNCTIONS
###########
print('=' * 80)
print('-' * 80)
print("THIS IS A SIMPLE MODULE TO SEARCH A FILE FOR A MAC OR IP")
print("AND THEN PUTS THOSE MACS IN A TXT FILE FOR FURTHER ANALYSIS")
print("TO CONTINUE, SELECT 1 TO ID MAC OR 2 TO ID IP")
print('-' * 80)
print('=' * 80)
choice = int(raw_input('1/2?'))
#Choice for menu
#
os.system('ifconfig > ifconfig_1.txt')
#Run ifconfig and output to said file
#
macfind = open('ifconfig_1.txt', 'r')
#File Object
#
output_1 = file('output.txt', 'w')
list_macfind = list(macfind)
#Convert file to iterable
#
searchmac_string = '([a-fA-F0-9]{2}[:|\-]){5}[a-fA-F0-9]{2}'
searchip_string = r"(%s)" % ("\.".join(['(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)']*4))
#Search string for mac and ip
#
if choice == 1:
    for x in list_macfind:
        a = re.compile(searchmac_string).search(x) # declare variable to if
        if a:
            print >> output_1, x[a.start():a.end()] # put addys in txt for further analysis
            print x[a.start():a.end()] # display macs found
            os.system('rm ifconfig_1.txt') # delete file
            deauth_file = open('output.txt', 'r') # opens previous output file
if choice == 2:
    ips = []
    for y in list_macfind:
        b = re.compile(searchip_string).search(y)
        if b:
            ips.append(B)
            print y[b.start():b.end()]
            os.system('rm ifconfig_1.txt')

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...