python - Output manipulation from subprocess -
I want to manipulate the output with a subprocess call and only print specific details. I was originally going down from the Pipes in any sub-publication: Code> def nmap_ips (ip_list) ): In ip_list for ip: cmd1 = subprocess.Popen (["nmap", "-sC", "--open", "-Pn", "-p80,443", ip], stderr = subprocess.STDOUT , Stdout = subprocess.PIPE) cmd2 = subprocess.Popen (["grep", "title", "-a", "1"], stdin = cmd1.stdout) cmd1.stdout.close () Output = cmd2.communicate () [0] stdout Reading lines: # IM is not actually receiving output from this function, I'm guessing IP's IPILIP: P = Subprances for Popen (IP) (IP_list): regs = ["title", "common name"] combined = "|". ["Nmap", "-sc", "- Open", "-PN", "-P 80,443", IP], stadder = subprances STDUUT, stadout = subpros. PPE) in the line (P.Stdout.readline, b ''): ml = re.match (combined, line) if ml: print ml.group () The example of the data I am trying to manipulate Note: If you want to save these lines in one variable, then the I do not have to make sure about the exact syntax for the same If you do not need the SSL certificate web server then you need PIPE route and sent the output to the second
subprocess of
grep but it looks very limited , Which means that I could only print some lines using the
-A or
-b
grep arguments (some of which I Did not want). So now I'm taking the
stdout.readline route. My question is, is there a better way of what I am trying to do? Does regular expression and
again match the best option?
2014-10-10 at 23:47 p.m. NMAP 6.40 (http://nmap.org ) To start BTHMemHub.home (192.168.1.254) the NMAP scan report is hosted (0.0054 latency). Port State Service 80 / TCP Open http | Http-title: Home Hub Manager - Please reset your password. _Requested resource was http: //BTHomeHub.home/html/home/a_firstlogin_configuration.html 443 / tcp open https | Ssl-cert: Subject: Common Name = bthomehub.home / Countryname = UK | Not valid before: 2011-01-15T16: 52: 23 + 00: 00 | _10 Not valid: 2024-01-17T16: 52: 23 + 00: 00. _ssl- Date: 2014-10-10 T 22: 47: 18+ 00:00; Your view is OK
grep 'title \ <14>
double search (b'title | commonName', line) both work .
Re.match () Match starts at the beginning of its input string
re.search () finds matches anywhere in the string .
grep process is unnecessary. This is only enough to start
nmap . Popen, PIPE, STDOUT def nmap_something (ip_list): match = re.compile (b "title | commonName") from subprocess import .search p = popen (["nmap", "-" SC "," --open "," -Pn "," -p80,443 "] + ip_list, stdout = PIPE, stderr = STDOUT, bufsize = 1) Results = List (filter (match, P. standout)) .stdout.close () p.wait () Return Results
nmap Ips, but it supports it i.e. you start multiple processes to scan multiple apps Is not required.
nmap , you can use a pure Python solution such as
Comments
Post a Comment