You are not logged in.

#1 2007-10-21 13:59:57

fiod
Member
Registered: 2007-04-02
Posts: 205

[SOLVED] Script for auto-connection

Hey,

I want to write a script that does the following:
When I switch my laptop on, checks (using the "iwlist wlan0 scan" command) which wireless networks are in range.
Then, if my home's essid is found, launch the file that connects to that network.

I know quite a few programming languages (JAVA, C, C++, etc.), but no scripting languages what so ever..

I would like a recommendation of a good scripting language to use, and maybe a few pointers as well..

Thanks in advance
Fiod

Last edited by fiod (2007-10-22 18:16:52)

Offline

#2 2007-10-21 15:48:09

tuxom
Member
From: France
Registered: 2007-03-04
Posts: 34

Re: [SOLVED] Script for auto-connection

I started to write a script wich list the available networks in python

maybe that can help you for starting

It is really fun to write python but when you come from java or C, you have to learn a new way of programming !!!
Python is really easy to learn.


#!/usr/bin/env python

import os

class AccessPoint:
  adress = ""
  essid = ""
  quality = 0
  mode = ""
  frequency = ""
  channel = 0
  encryptionKey = ""
  encryptionType = "No Information available"
  def __init__(self, address):
    self.address = address
  def __cmp__(a, b):
    return cmp(a.quality, b.quality)

tmpFile = "/tmp/wifiap"
interface = "wlan0"

if (os.path.exists(tmpFile) == False):
  os.system ("iwlist %s scan > %s" % (interface,tmpFile))

data = open(tmpFile).readlines() # open the file

accessPoints = []
 
for line in data:
  if "Address" in line: # create a new network dict
    address = line[line.find("Address") + 8:]
    accessPoints.append(AccessPoint(address.strip()))
  elif "ESSID" in line:
    accessPoints[-1].essid = line[line.find("ESSID") + len("ESSID") + 1:].strip()
  elif "Mode" in line:
    accessPoints[-1].mode = line[line.find("Mode") + len("Mode") + 1:].strip()
  elif "Frequency" in line:
    accessPoints[-1].frequency = line[line.find("Frequency") + len("Frequency") + 1:].strip()
  elif "Encryption key" in line:
    accessPoints[-1].encryptionKey = line[line.find("Encryption key") + len("Encryption key") + 1:].strip()
  elif "IE" in line:
    accessPoints[-1].encryptionType = line[line.find("IE") + len("IE") + 1:].strip()
  elif "Quality" in line:
    accessPoints[-1].quality = line[line.find("Quality") + len("Quality") + 1:line.find("Signal")].strip()
  elif "Channel" in line:
    accessPoints[-1].channel = line[line.find("Channel") + len("Channel") + 1:].strip()

accessPoints.sort()
accessPoints.reverse()
print "Listing found networks:"
print '%-18s %-20s %-8s %-7s %-4s (%s)' % ("ADDRESS", "ESSID", "QUALITY", "CHANNEL", "ENCRYPTION KEY" , "ENCRYPTION TYPE")
for accessPoint in accessPoints:
  print '%-18s %-20s %-8s %-7s %-4s (%s)' % (accessPoint.address, accessPoint.essid, accessPoint.quality, accessPoint.channel, accessPoint.encryptionKey , accessPoint.encryptionType)

Offline

#3 2007-10-21 16:09:42

fiod
Member
Registered: 2007-04-02
Posts: 205

Re: [SOLVED] Script for auto-connection

Thanks a lot!

Will try to understand and use it..

Offline

#4 2007-10-21 16:31:56

fiod
Member
Registered: 2007-04-02
Posts: 205

Re: [SOLVED] Script for auto-connection

Do you happen to know how to execute a file from python?

Meaning, if I found the wanted ESSID, and want to launch a bash script that connects to the network (say, /root/home-connection),
how do I launch it from within a python script?

Thanks
fiod

Offline

#5 2007-10-21 16:45:29

buttons
Member
From: NJ, USA
Registered: 2007-08-04
Posts: 620

Re: [SOLVED] Script for auto-connection

import os

os.system('your command here')

Cthulhu For President!

Offline

Board footer

Powered by FluxBB