bobbyb1980 Posted April 20, 2012 Share Posted April 20, 2012 (edited) Hey guys. I plan on starting to learn C. I've been doing Python for a number of months now and it has been very rewarding for me. Do you guys think that C is harder/easier than Python, or are they just different? The Python book I have makes a lot of references to C. Should I start with C or C++ or does it make a difference? How long should it take to become an intermediate C programmer, 6 months, a year? Lately I've been writing some rinky dink programs, I still have a looot to learn but I made a few modules to deauth people from AP's and try to detect rouge AP's and make a DNS entry on bind9 where u just type the host and IP and run the module and it automatically writes the zone files without having to go through the obscure process of adding zones. I'd be using it for stuff like that, mainly various pen testing applications. Thanks. Edited April 20, 2012 by bobbyb1980 Quote Link to comment Share on other sites More sharing options...
Sitwon Posted April 21, 2012 Share Posted April 21, 2012 Hey guys. I plan on starting to learn C. I've been doing Python for a number of months now and it has been very rewarding for me. Excellent. If you're comfortable with Python then C is a good next step. Do you guys think that C is harder/easier than Python, or are they just different? The Python book I have makes a lot of references to C. In some ways it is more challenging, in other ways it is simpler. I'd prefer to say that they're just a bit different. Certainly don't be afraid of C.Python was inspired by and written in C and in many ways it still says very close to those roots. In my opinion, it has one of the most convenient FFIs (Foreign Function Interface) for accessing native-compiled C libraries. This heritage and easy interoperability make Python and C a very good pair. Should I start with C or C++ or does it make a difference?Definitely start with C. It does make a difference. C is a relatively small and simple language, where as C++ is a very complex and sometimes convoluted language. In my experience it is definitely far easier to teach/learn C than C++. How long should it take to become an intermediate C programmer, 6 months, a year?Depending on the student 6 months is entirely realistic (especially with prior experience with another programming language). There are a few core concepts which students typically struggle with when learning C or C++ for the first time: pointers, memory management, and recursion. Since recursion is the same in C as it is in Python, I would say learn it in Python (if you haven't already). Pointers and memory management, however, you can't really learn in Python.Pointers and memory management can be difficult to explain in text. When I teach these topics I like to use diagrams step through the program with the student(s) using an interactive debugger to demonstrate what is happening. If you're taking a live class or don't have a tutor available who can help explain it to you, then you might find it useful to look up good video lectures on these topics. (And by "good" I mean professional lectures from MIT, Stanford, UC Berkeley, or my favorite, UNSW. Definitely NOT TheNewBoston's tutorials.) Here are the links to YouTube videos of professor Richard Buckland of the University of New South Whales giving his lectures on COMP1917 (their version of Introduction to Computer Science) and the follow up COMP1927 (Data structures and Algorithms). These are excellent lectures in which he teaches the fundamentals of Computer Science and programming with C. Lately I've been writing some rinky dink programs, I still have a looot to learn but I made a few modules to deauth people from AP's and try to detect rouge AP's and make a DNS entry on bind9 where u just type the host and IP and run the module and it automatically writes the zone files without having to go through the obscure process of adding zones. I'd be using it for stuff like that, mainly various pen testing applications. Thanks. That sounds great! The best way to learn any language is by using it to solve actual problems. Quote Link to comment Share on other sites More sharing options...
bobbyb1980 Posted April 22, 2012 Author Share Posted April 22, 2012 Wow, thanks for all that info Sitwon! I have to say, info regarding this stuff does not seem very easy to come by. Currently I'm reading Python by Mark Lutz and it's a good read and I try to stay progressing everyday. Can you recommend any books on C? What about Java? I know that Java is a pretty commonly used programming language, would you consider Java similar to python in the way C is similar to python? Would you recommend learning C or Java after python? Quote Link to comment Share on other sites More sharing options...
Sitwon Posted April 23, 2012 Share Posted April 23, 2012 (edited) Wow, thanks for all that info Sitwon! I have to say, info regarding this stuff does not seem very easy to come by. Currently I'm reading Python by Mark Lutz and it's a good read and I try to stay progressing everyday. Can you recommend any books on C? The canonical book on C is "The C Programming Language" by Brian Kernighan and Dennis Ritchie. http://www.amazon.com/Programming-Language-2nd-Brian-Kernighan/dp/0131103628/ref=sr_1_1?ie=UTF8&s=books&qid=1281115882&sr=8-1 Another good book is "Programming in C": http://www.cs.cf.ac.uk/Dave/C/ What about Java? I know that Java is a pretty commonly used programming language, would you consider Java similar to python in the way C is similar to python?Yes, Java is similar to Python in some of the same ways that C is similar to Python.Would you recommend learning C or Java after python? If you asking which language will teach you more about the fundamentals of computer science then I would say C. If you're specifically interested in learning about the object-oriented paradigm then I would say Java.Personally, from the perspective of studying Computer Science, I would counsel learning C first. Mastering pointers is essential to understanding languages like Java in a more fundamental way. Java, by design, obfuscates it's implementation. You can find more resources here: http://learnproglang.couch.it/ Edited April 23, 2012 by Sitwon Quote Link to comment Share on other sites More sharing options...
bobbyb1980 Posted April 23, 2012 Author Share Posted April 23, 2012 Really, thanks for your help Sitwon, not easy finding info on this stuff. I hope I can trouble you for another question or two : P I assume by the amount of info you know about Python that you're familiar with it. I've found that for a lot of what I'm doing, mainly wifi stuff, I use subprocess a lot and I parse a lot of data (from output files). For example, I'll use subprocess and Popen to run airodump-ng, then I'll iterate over the output file looking for MAC's or IP's or what have you and take the findings and append them to a list or dictionary for later use. Could you recommend and module packages/concepts that I should be learning to assist me in these types of operations? This is also a problem I've been having for a few weeks now. I want to parse output from airodump-ng but I'd like it to run "in memory", ie no output file, this just seems like a more "Pythonic" way of doing it since in theory I could read and iterate over simple files in bash. I've gotten airodump-ng to pipe it's output to stdout I believe it was, but when I try to iterate over the output (using both for and while, the output is in the form of a human readable tupple) of airodump-ng it always finds nothing. Can you recommend any modules or concepts that I should study to get around this? Once again thanks for your help. Quote Link to comment Share on other sites More sharing options...
Sitwon Posted April 23, 2012 Share Posted April 23, 2012 Really, thanks for your help Sitwon, not easy finding info on this stuff. I hope I can trouble you for another question or two : P I assume by the amount of info you know about Python that you're familiar with it. I've found that for a lot of what I'm doing, mainly wifi stuff, I use subprocess a lot and I parse a lot of data (from output files). For example, I'll use subprocess and Popen to run airodump-ng, then I'll iterate over the output file looking for MAC's or IP's or what have you and take the findings and append them to a list or dictionary for later use. Could you recommend and module packages/concepts that I should be learning to assist me in these types of operations? Let's see, that's a pretty broad problem set. First of all, as a best practice, you should encapsulate external dependencies behind a class or a module. This gives you the freedom to develop an alternate class/module for accessing that external dependency without having to rewrite the rest of your application. (Eg, you might start out invoking it on the command line and parsing the output, but later decide to make direct calls to the DLL.) If you get in the habit of encapsulating external dependencies this way then you'll find it easier to replace or upgrade them, and you'll also find it easier to re-use them in other programs. Second, learn Regular Expressions (aka, regex or the 're' module). Regex is a language for describing and matching patterns in text. Whenever you're working with strings or text files and trying to extract specific bits of information a regex pattern is usually the best strategy. Learning regex also comes in handy in other languages and utilities (grep, sed, awk, perl, Java, Ruby, Scala, etc). This is also a problem I've been having for a few weeks now. I want to parse output from airodump-ng but I'd like it to run "in memory", ie no output file, this just seems like a more "Pythonic" way of doing it since in theory I could read and iterate over simple files in bash. I've gotten airodump-ng to pipe it's output to stdout I believe it was, but when I try to iterate over the output (using both for and while, the output is in the form of a human readable tupple) of airodump-ng it always finds nothing. Can you recommend any modules or concepts that I should study to get around this? Once again thanks for your help. Some applications, particularly those like airodump-ng which re-write parts of the screen, they will output differently or not at all if their standard output stream is redirected to a non-TTY device (such as a file or a pipe). If you want to avoid writing the file out to the disk you could use a named-pipe instead. A named-pipe, or fifo, looks like a file but is actually just a buffer which queues lines of text. One application can enqueue strings into it and another application can dequeue strings out of it, but the strings are never written to the disk (and once they've been read they are "forgotten"). You can try it on the command-line with the 'mkfifo' command, or you can do it in python with os.mkfifo. Create a named-pipe and tell airodump-ng to write it's logs to that "file", then read them in and parse them from the named-pipe. This is not necessarily the Pythonic solution, but it's the Unix solution. Named-pipes will work regardless of the language you use because they look and act like files. Quote Link to comment Share on other sites More sharing options...
bobbyb1980 Posted April 28, 2012 Author Share Posted April 28, 2012 Sitwon! Thanks for the informative response, it's nice to read answers to these questions! About classes, I'm still learning the concept of classes, I actually just started the introduction to classes part of my book yesterday. To my understanding they are data structures of functions and used for program organization. I'm still not quite there yet to the point where I can incorporate the concept of classes into my programs (well, not programs, better said random groups of functions!) but I hope to be soon. In regards to regex, yes, per your advice I have changed and it is much more versatile than using find() and 10 times more versatile than using line[x:x] == 'searchstring' to match patterns. On the negative side it's not the easiest module to use and takes a little getting used to : ( I've figured out how to make it ID mac addresses but I'm still working on making it identify IP addresses. Dude, mannny thanks for the fifo pipe advice. I haven't had time to read in depth into this concept but this weekend I am going to and will definitely share my findings. From what I have seen, there are no python programs out there yet that use this technique of parsing output and I hope to be the first! I have a question for you on the ethics of coding Sitwon. My book constantly talks about writing code so it can be easily read and reused by others. I assume it's "ok" for me to use other's code? I mean... some things are a little over my head, but if I copy paste them from another program, I can usually catch on to what it's doing and figure out a way to rewrite it so it looks like I did it and didn't just copy and paste, but from what my book is telling me it's ok to just copy paste code from other open source programs? Coming from a capitalist environment, this concept isn't heavily accepted and I want to make sure it's ok hah : P Quote Link to comment Share on other sites More sharing options...
Sitwon Posted April 28, 2012 Share Posted April 28, 2012 (edited) Sitwon! Thanks for the informative response, it's nice to read answers to these questions! About classes, I'm still learning the concept of classes, I actually just started the introduction to classes part of my book yesterday. To my understanding they are data structures of functions and used for program organization. I'm still not quite there yet to the point where I can incorporate the concept of classes into my programs (well, not programs, better said random groups of functions!) but I hope to be soon. Yes, classes are a collection of functions (called members) and data upon which those members operate. It's like you're creating a new data type, but giving it a bunch of semantic meaning and helper functions specific to that data type.In regards to regex, yes, per your advice I have changed and it is much more versatile than using find() and 10 times more versatile than using line[x:x] == 'searchstring' to match patterns. On the negative side it's not the easiest module to use and takes a little getting used to : ( I've figured out how to make it ID mac addresses but I'm still working on making it identify IP addresses.You can find a LOT of examples for common patterns like IP addresses, email address, phone numbers, etc. online. Most of the time you can just cut&paste the one you need (although I still recommend learning the language so you understand what the pattern you're using actually does).Dude, mannny thanks for the fifo pipe advice. I haven't had time to read in depth into this concept but this weekend I am going to and will definitely share my findings. From what I have seen, there are no python programs out there yet that use this technique of parsing output and I hope to be the first! I'm sure that there must be other python scripts out there that use named pipes, but I think it's probably a less popular technique because it only works on Unix-like systems. Python coders like to write cross-platform code and named pipes aren't portable to Windows systems. I have a question for you on the ethics of coding Sitwon. My book constantly talks about writing code so it can be easily read and reused by others. I assume it's "ok" for me to use other's code? I mean... some things are a little over my head, but if I copy paste them from another program, I can usually catch on to what it's doing and figure out a way to rewrite it so it looks like I did it and didn't just copy and paste, but from what my book is telling me it's ok to just copy paste code from other open source programs? Coming from a capitalist environment, this concept isn't heavily accepted and I want to make sure it's ok hah : P Well this can be something of a complicated issue, and there's some subjectivity involved. To simplify it as much as I can, think about it like you would any other language. Imagine you're writing a college paper and your school has a strict policy on academic dishonesty. As a student you're not typically expected to be an expert on the subject matter so it's perfectly acceptable for you to do research and consult the works and opinions of other people with experience in whatever subject matter you're writing about. The same way you would cite those other authors in your paper you should cite the authors of source code that you have substantially copied. Now the subjectivity comes in determining what is a "substantial" copy and what isn't. Some bits of code might be novel and worth attributing to the original author, while others might just be idiomatic patterns of the language which are would be considered obvious to anyone fluent in the language. That judgement will just take experience. If in doubt, cite the author of the code you are borrowing.In general, these rules apply to all code regardless of what license they are covered by. If your "borrowing" falls within Fair Use then it doesn't matter which license the code was released under. (This has not been heavily tested in court, but it has been upheld in a few notable cases such as SCO v. IBM.) The distinction comes in when you are copying more significant functionality or even the entire program and redistributing it largely intact. In those cases, merely citing the author is not enough, you must also comply with the terms of whatever license is attached to that source code which grants you permission to make copies in cases that fall outside the scope of Fair Use (or not, if it happens to be proprietary code). The important things to understand are: A.) Open Source software is not necessarily at odds with Capitalism. The two can and do work together all the time. B.) Intellectual property laws are complex, convoluted, and subjective (which is why we have lawyers and judges). C.) The court assumes that as a computer programmer you are equally capable of understanding the legalese in a license agreement as a trained lawyer (Blizzard v. BNet), you can't use the IANAL excuse. Edited April 28, 2012 by Sitwon Quote Link to comment Share on other sites More sharing options...
bobbyb1980 Posted April 28, 2012 Author Share Posted April 28, 2012 (edited) Thanks for another great explanation Sitwon! I definitely owe you a drink! Up until now I haven't really been copying that much code but the thought has crossed my mine. I do however read a lot of other scripts then I can use different methods of doing what they do to accomplish the same task (ie wifite-ng used find to look for mac addys, I use regex. It iterates airodump-ng from an output file whereas I'll iterate it from a named pipe, etc. Hopefully that's alright. It is surprising to know that Python coders like to code cross platform. I would have figured that most are linux people with a firm anti-Windows stance like most in the security community : P I have a question I was hoping you'd be kind enough to answer. I have been reading a lot about regular expressions (Google has some GREAT python documentation!) and I have figured how to find IP's, but it's returning more than I am bargaining for. I'll show you and example and please let me know if I'm doing this whole process right and if the comments of my understandings are correct. The goal of this code is to locate IP addys within the output of ifconfig. def getIP(): proc = subprocess.Popen(['ifconfig'], stdout=subprocess.PIPE) #pipes ifconfig output to stdout stdout_value = proc.communicate()[0] #communicates stdout to generator object iterable = stdout_value.split('\n') #removes \n's from generator object and makes iterable (?Not really sure if it makes gen obj iterable but python wont iterate properly without stripping it) get_ip = r'\d+.\d+.\d+.\d+' # Intended to match 4 consecutive numerical groupings of any size separated by periods for x in iterable: # begins iteration a = re.compile(get_ip).search(x) #compiles search string and searches iterated objects if a: print(x[a.start():a.end()]) #if the search string is matched print what was found ip_list.append(a) The code works, it will identify what I need it to, however I also get some extra garbage that I can't seem to get rid of. Here is the output of the file. 127.0.0.1 192.168.161.1 172.16.178.1 192.168.1.2 1562839 1347534 1344840896 I can't seem to shake those numbers at the bottom. Any idea what is happening here? It also won't find the same set of characters twice, ie it will locate 192.168.1.1 but it will not locate 192.168.1.255. Edited April 28, 2012 by bobbyb1980 Quote Link to comment Share on other sites More sharing options...
Sitwon Posted April 28, 2012 Share Posted April 28, 2012 (edited) In regular expressions the period ('.') matches any character. If you want to match a literal period character you need to escape it with a backslash. get_ip = r'\d+\.\d+\.\d+\.\d+' Also, you don't need to compile the expression every time you search, you can compile it once and then use it for multiple searches. get_ip = re.compile(r'\d+\.\d+\.\d+\.\d+') ... a = get_ip.search(x) Edited April 28, 2012 by Sitwon Quote Link to comment Share on other sites More sharing options...
bobbyb1980 Posted April 30, 2012 Author Share Posted April 30, 2012 Thanks a lot for the help Sitwon! Got the named pipe concept down and playing around with it to get airodump-ng writing to it : ) Quote Link to comment Share on other sites More sharing options...
bobbyb1980 Posted April 30, 2012 Author Share Posted April 30, 2012 Also, you don't need to compile the expression every time you search, you can compile it once and then use it for multiple searches. get_ip = re.compile(r'\d+\.\d+\.\d+\.\d+') ... a = get_ip.search(x) Sitwon, I am trying this, but the following is the only method I can get to return results. searchmac_string = '([a-fA-F0-9]{2}[:|\-]){5}[a-fA-F0-9]{2}' for x in iterable: a = re.compile(searchmac_string).search(x) if a: print(x[a.start():a.end()]) When I change this, and place the variable "a" outside of the for loop the iteration does not complete properly. Would that be cause it uses a variable outside the scope of for? Quote Link to comment Share on other sites More sharing options...
Sitwon Posted April 30, 2012 Share Posted April 30, 2012 Sitwon, I am trying this, but the following is the only method I can get to return results. searchmac_string = '([a-fA-F0-9]{2}[:|\-]){5}[a-fA-F0-9]{2}' for x in iterable: a = re.compile(searchmac_string).search(x) if a: print(x[a.start():a.end()]) When I change this, and place the variable "a" outside of the for loop the iteration does not complete properly. Would that be cause it uses a variable outside the scope of for? Try like this: searchmac_string = re.compile('([a-fA-F0-9]{2}[:|\-]){5}[a-fA-F0-9]{2}') for x in iterable: a = searchmac_string.search(x) if a: print(x[a.start():a.end()]) Quote Link to comment Share on other sites More sharing options...
bobbyb1980 Posted May 1, 2012 Author Share Posted May 1, 2012 (edited) Thanks Sitwon! Once again, that works! I'm having another little hiccup I was hoping you'd be able to assist with. I have airodump-ng writing to a fifo but the problem is, is when I run my code from the terminal it writes the airodump-ng output to the terminal. IE- as soon as the 'airodump-ng mon0' command is run it's like the code beneath it doesn't exist and airodump-ng takes over. I've tried playing around with stdout but that doesn't seem to be the issue. The goal of this code is to execute airodump-ng, let it read the airwaves for 3 seconds, then extract all mac's from it's output and put them into a list for further usage. Any idea what's happening here? The code seems to work with anything that isn't airodump-ng : ( Should I be researching xterm? mac_list = [] r, w = os.pipe() proc = subprocess.call(['airodump-ng', 'mon0'], stdout=os.fdopen(w, 'w'), ) time.sleep(3) client_out = os.fdopen(r, 'r').read() iterable = client_out.split('\n') searchmac_string = re.compile('([a-fA-F0-9]{2}[:|\-]){5}[a-fA-F0-9]{2}') for x in iterable: a = searchmac_string.search(x) if a: print(x[a.start():a.end()]) mac_list.append(a) Edited May 1, 2012 by bobbyb1980 Quote Link to comment Share on other sites More sharing options...
Sitwon Posted May 1, 2012 Share Posted May 1, 2012 It's possible that airodump-ng is writing to stderr instead of stdout. Or that it's figuring out which TTY devices the shell is connected to and writing directly to that instead of using stdout/stderr. Also, you need to use subprocess.Popen() instead of subprocess.call(). call() will block (pause your program) until the subprocess terminates so that it can return the exit code and store it in 'proc'. Instead you want to use Popen() to run it in the background and keep a reference to it in proc. I would try this: r, w = os.pipe() w = os.fdopen(w, 'w') proc = subprocess.Popen(['airodump-ng', 'mon0'], stdout=w, stderr=w, ) time.sleep(3) client_out = os.fdopen(r, 'r').read() proc.terminate() If it still doesn't work, try adding shell=True to the Popen() call. If it still doesn't work you might need to re-think your strategy. Also, if airodump-ng works the way I think it does, you might not get the output you're expecting from that pipe, so you might have to come up with a different way to gather that information anyways. Quote Link to comment Share on other sites More sharing options...
Jason Cooper Posted May 1, 2012 Share Posted May 1, 2012 I haven't got my machine with airodump-ng to hand, but it might be easier for you to parse to the output files it can generate rather than the screen output. Just use something like airodump-ng wlan0 -w OutputData and then while it is running tail the file using the command tail -f OutputData.txt If you see things appearing in the file as time goes on then you could just open the file in your script and read from that. Quote Link to comment Share on other sites More sharing options...
bobbyb1980 Posted May 1, 2012 Author Share Posted May 1, 2012 Thank you for the response Jason, however I was trying to avoid using that method. Sitwon, I tried what you said and every variant of and the closest I can get is a 'no interface specified' error from airodump-ng. When playing with stdout and stderr and when the code is run from a bash shell it just hangs infinitely. Results are a little different when run from the interpreter but regardless still not working. The error I am getting is from popen and airodump-ng is not seeing the "mon0" on the end of the file, all the actual bash shell is seeing is 'airodump-ng'. Looks like a problem with blank spaces or the way that python is passing the code to the bash shell. I'm going to paste the code below, to verify that the pipes were redirecting properly I ran this code and then iterated the output and python can see airodump-ng's output in this form but now the problem looks like it may be with bash. I've also tried all the other combos I know of formatting the Popen expression and they all yield the same results. Any ideas what that could be? r, w = os.pipe() proc = subprocess.Popen(['mon0'], stdout=os.fdopen(w, 'w'), stderr=subprocess.PIPE, shell=False, executable='airodump-ng' ) time.sleep(2.5) client_out = os.fdopen(r, 'r').read() for x in client_out: print(x) The output of this code will display a "no interface specified" error... Quote Link to comment Share on other sites More sharing options...
Sitwon Posted May 1, 2012 Share Posted May 1, 2012 Thank you for the response Jason, however I was trying to avoid using that method. Sitwon, I tried what you said and every variant of and the closest I can get is a 'no interface specified' error from airodump-ng. When playing with stdout and stderr and when the code is run from a bash shell it just hangs infinitely. Results are a little different when run from the interpreter but regardless still not working. The error I am getting is from popen and airodump-ng is not seeing the "mon0" on the end of the file, all the actual bash shell is seeing is 'airodump-ng'. Looks like a problem with blank spaces or the way that python is passing the code to the bash shell. I'm going to paste the code below, to verify that the pipes were redirecting properly I ran this code and then iterated the output and python can see airodump-ng's output in this form but now the problem looks like it may be with bash. I've also tried all the other combos I know of formatting the Popen expression and they all yield the same results. Any ideas what that could be? r, w = os.pipe() proc = subprocess.Popen(['mon0'], stdout=os.fdopen(w, 'w'), stderr=subprocess.PIPE, shell=False, executable='airodump-ng' ) time.sleep(2.5) client_out = os.fdopen(r, 'r').read() for x in client_out: print(x) The output of this code will display a "no interface specified" error... The call to Popen should look like this: proc = subprocess.Popen(['airodump-ng', 'mon0'], stdout=os.fdopen(w, 'w'), stderr=subprocess.PIPE, shell=False, executable='airodump-ng' ) Even though you specified the executable with the named parameter, you still need to put it in the list of arguments to become argv[0] and put 'mon0' in argv[1]. argv[0] needs to be the name of the executable that was called. Some applications (such as bash or busybox) will behave differently depending on which name they were called with (as passed to them in argv[0]). Quote Link to comment Share on other sites More sharing options...
bobbyb1980 Posted May 1, 2012 Author Share Posted May 1, 2012 Thanks Sitwon, that did solve the problem, I'm not getting the interface not specified error anymore but now it's just hanging like the other ones. Looks like my options are to either parse the csv file or find another program similar to airodump-ng to read output from as this method won't work. Quote Link to comment Share on other sites More sharing options...
Jason Cooper Posted May 2, 2012 Share Posted May 2, 2012 As I have my machine with airodump-ng with me today I figured I would have a quick look at how it outputs it's results. It prints the results to stderr as sitwon suggested, it also has some ANSI codes in the output to clear the screen and reposition the cursor to the top left of the screen each time it updates the result. If you really want to process the output rather than its dump files then try #!/usr/bin/python import os import subprocess import time import sys proc=subprocess.Popen(['airodump-ng','mon0'], stderr=subprocess.STDOUT, executable='/usr/sbin/airodump-ng') time.sleep(2.5) client_out=proc.communicate() for x in client_out: print(x) sys.stdout.flush() The key bits here are redirecting stderr to stdout and using the communicate method of the subprocess to get your output. Quote Link to comment Share on other sites More sharing options...
bobbyb1980 Posted May 2, 2012 Author Share Posted May 2, 2012 Hey Jason, thank you for your help! Unfortunately I tried that and results are all the same, as soon as Popen runs airodump-ng, the command just takes over the terminal and it's like the code beneath the Popen line doesn't exist. I'll try to throw in a terminate process line but I'm sceptical about that also. I was thinking that if it does indeed write to stderr, I could try to pipe stderr to the input of another program and see if it can be read like that. Quote Link to comment Share on other sites More sharing options...
Jason Cooper Posted May 2, 2012 Share Posted May 2, 2012 Having written the script in Perl I convinced myself that it wasn't anything to do with airodump-ng and decided it must be something in python causing the problem. I have tried specifying subprocess.PIPE for both stdout and stderr which caused it to block on communicate. I then tried ditching the recommended communicate method opting for pulling from the stderr stream directly, which seemed to produce better results. I have produced you some new python code that is closer to what you want #!/usr/bin/python import os import subprocess import time import sys import re proc=subprocess.Popen(['airodump-ng', 'mon0'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, executable='/usr/sbin/airodump-ng') time.sleep(2.5) macsRe=re.compile("([\da-f]{2}:[\da-f]{2}:[\da-f]{2}:[\da-f]{2}:[\da-f]{2}:[\da-f]{2})", re.IGNORECASE) for x in proc.stderr: macs=re.findall(macsRs,x) for mac in macs: print (mac); sys.stdout.flush() and for those who want to see the Perl code for the same, here it is #!/usr/bin/perl use stict; open AIRODUMP, "/usr/sbin/airodump-ng mon0 2>&1 |" or die "unable to run airodump-ng\n"; while(<AIRODUMP>){ my @macs=m/([\da-f]{2}:[\da-f]{2}:[\da-f]{2}:[\da-f]{2}:[\da-f]{2}:[\da-f]{2})/gsi; foreach my $mac (@macs){ print "$mac\n"; } } Note, both scripts will run as long as the airodump-ng process runs. Quote Link to comment Share on other sites More sharing options...
bobbyb1980 Posted May 2, 2012 Author Share Posted May 2, 2012 Jason! Thanks bud, that works awesome! Who would have thought it was just writing proc.stderr, heh. I still haven't perfected this, but I read on google's python tutorials that when using regular expressions it's best to iterate and then put an if statement after, so I altered your code a bit. When I have more time this evening I also plan on throwing in another if statement so duplicates are ignored and also a few lines to match up AP names with MAC addys. But thanks again Jason, I owe you a beer! import os import subprocess import time import sys import re mac_list = [] proc=subprocess.Popen(['airodump-ng', 'mon0'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, executable='/usr/sbin/airodump-ng') time.sleep(2.5) searchmac_string = re.compile(r'([a-fA-F0-9]{2}[:|\-]){5}[a-fA-F0-9]{2}') for x in proc.stderr: a = searchmac_string.search(x) if a: print(x[a.start():a.end()]) mac_list.append(a) sys.stdout.flush() Quote Link to comment Share on other sites More sharing options...
bobbyb1980 Posted May 2, 2012 Author Share Posted May 2, 2012 This is indeed some sexy code : ) : ) Thanks again Jason! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.