Python Forum

Full Version: How can I parse a text file?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
The code i posted should run also with Python2.7, but not tested. I do not care about this.
Unpacking star assignment was introduced with 3.4 or 3.5 and is very nice.

first_element, *rest = long_list
first_element, *middle, last_element = long_list
I always imply that the user is using Python 3.x. I do not support legacy Python.

The set/dict/list comprehension is not easy to understand. Written as for-loop:
access_points_dict = {}
for address, essid, channel in zip(addresses, essids, channels):
   access_points_dict[essid] = {'address': address, 'channel': channel}
I have just coded my program that is way. It's working.

#!/usr/bin/python3.5    

import os, sys, re
from subprocess import check_output, call, PIPE
import subprocess

os.system('sudo airmon-ng start wlan0')

def scannig():
   var = subprocess.check_output(['sudo', 'iwlist', 'scanning'], universal_newlines=True)
   output = open('scanning.txt', 'w')
   print(var, file=output)
   output.close()

scannig()

address = []
channel = []
essid = []
f = open('scanning.txt', 'r').readlines()
for x in f:
   if "Address" in x:
       address.append(x)
   elif "Channel" in x:
       channel.append(x)
   elif "ESSID" in x:    
       essid.append(x)

address = ''.join(address)
channel = ''.join(channel)
essid = ''.join(essid)

address = address.split()
channel = channel.split()
essid = essid.split()
#------------------------------------
address_formatted = [x for x in address if len(x) > 4 and not x.startswith("Address")]
channel_formatted = [x for x in channel if x.startswith("Channel")]
essid_formatted = []

for x in essid:
   essid_formatted.append(x.replace('ESSID:', ''))

new_essid_formatted = []

for x in essid_formatted:
   new_essid_formatted.append(x.replace('"', ''))

print('---' * 10)
access_point_list = list(zip(channel_formatted, address_formatted, new_essid_formatted))
for goal in enumerate(access_point_list):
   print('target-->', goal)
print('---' * 10)

target = int(input("Choose a target for attaced: "))

if target == target:
   a = ''.join(re.findall('(\d+)', access_point_list[target][0]))
   b = access_point_list[target][1]
   start = subprocess.check_output(['sudo', 'airodump-ng', '--channel', a, '-w', 'wep', '-i', '--bssid', b, 'mon0']) 
   print(start.decode('utf-8').strip())
(May-31-2017, 06:22 PM)Mike Ru Wrote: [ -> ]f = open('scanning.txt', 'r').readlines()
You should close your files when you're done with them. Or use a with statement, so the file can close itself when it goes out of scope.
(May-31-2017, 07:27 PM)nilamo Wrote: [ -> ]You should close your files when you're done with them. Or use a with statement, so the file can close itself when it goes out of scope
Thank you for your advise!
Pages: 1 2