You are not logged in.

#1 2009-07-19 03:11:02

Dart27
Member
From: Florida
Registered: 2008-11-24
Posts: 8

WiFi script.

Well I made a wifi script for myself because netcfg didn't like working and I was tired of my makeshift connect script. So I made this ruby script to help me learn and to have a useful connect script so I thought that others might find it useful.

#!/usr/bin/ruby
# kukri 
$help = <<HELP
 Kukri
     WiFi script by Dart27 <DartTwentySeven@gmail.com>
 usage
 scriptname [interface] 
 -s    --scan                     Scans for access points.
 -c     --connect SSID {Hexkey|s:asciikey}    Connect to an access point    
 -d    --disconnect                Disconnect from current access point
 -h    --help                    Displays this help and exits
 -i    --information                Displays SSID and signal strength

 You can edit the source file to change the default interface and DHCP client.
 Note for wep users, if you are using an ascii key, prepend a "s:" to your key.
     ex: kukri -c accesspoint s:password

 Missing a feature? Contact me.
HELP
INTERFACE = :wlan0 # Edit for default extension
DHCPCLIENT = :dhclient # Edit for default dhcp client
class WifiTools 
    def initialize interface = INTERFACE
        @interface = interface
    end

    def scan
        
        networks = `iwlist #{@interface} scan`.split(/Cell \d*/)
        networks.shift
        networks.each do |results|        
            puts results.match(/SSID:\".*"/).to_s.gsub('"','')
            print "\t", results.match(/Channel:\d+/).to_s.concat("\n")
            print "\t", results.match(/Quality=\d+\/\d+/).to_s.sub("Quality=",'Strenth:').to_s.concat("\n")
            print "\t", results.match(/Encryption .*\s/).to_s.sub(' ',':')
        end
    end
    def connect essid, key = nil
         if `sudo ifconfig wlan0 up` != ''
             abort("Please run the script using sudo or as root")
         end

        unless key 
            puts "Connecting to #{essid}"
            if `sudo iwconfig #{@interface} essid #{essid}` == ''
                puts "Connected to #{essid}"
            else
                abort("Could not connect")
            end
        else
            puts "Connecting to #{essid} with WEP encryption"
            if `sudo iwconfig #{@interface} essid #{essid} key #{key}` == ''
                puts "Connected to #{essid}"
            else
                    abort("Could not connect")
            end
        end

        `sudo #{DHCPCLIENT} -q -n #{@interface}`
        puts "#{DHCPCLIENT.to_s.capitalize} started."
    end

    def disconnect
        "Disconnecting #{@interface}"
        if `sudo iwconfig #{@interface} essid off` != '' 
            "Disconnect failed! Try running as root"
        end
    end
    def information
        "Current Connection"
        info = `iwconfig #{@interface}`
        puts info.match(/SSID:\".*"/)
        puts  info.match(/Quality=\d+\/\d+/).to_s.sub("Quality=",'Strenth:')
        end
end


#wifi = Config.new(ARGV.first) if ARGV.first =~ /\w\d/ else 
case ARGV.first() 
    when /\w\w/
        wifi = WifiTools.new(ARGV.shift)
    else
    wifi = WifiTools.new()
end

case ARGV.first()
when "--scan", "-s"
    wifi.scan
when "--connect", "-c"
        wifi.connect ARGV[1], ARGV[2]
when "--disconect", "-d"
    wifi.disconnect
when "--information", "-i"
    wifi.information
when "--help", "-h"
    print $help
else
    print $help
end

dwm 4 lyfe

Offline

#2 2009-07-19 18:41:11

Daenyth
Forum Fellow
From: Boston, MA
Registered: 2008-02-24
Posts: 1,244

Re: WiFi script.

Why not work on making netcfg and wifi-select better instead of duplicating effort?

Offline

#3 2009-07-19 20:29:47

Dart27
Member
From: Florida
Registered: 2008-11-24
Posts: 8

Re: WiFi script.

Because this isn't a project and netcfg didn't work for me. So I just made something that will work without a lot of hassle.


dwm 4 lyfe

Offline

Board footer

Powered by FluxBB