├── README.md ├── hello_world.txt ├── wireless └── wireless.cfg /README.md: -------------------------------------------------------------------------------- 1 | wireless 2 | ======== 3 | 4 | Very simple OpenBSD console wireless connection manager. 5 | 6 | Store your usually used network configs inside a simple config file 7 | and save yourself typing by connecting to your wlan by issuing 8 | ./wireless network-name inside your console. Wireless will issue 9 | the right ifconfig commands to setup your connection and then get 10 | an IP address via DHCP. 11 | 12 | Installation: 13 | cp wireless /usr/local/bin/ 14 | cp wireless.cfg /etc/ 15 | 16 | Usage: 17 | lists all wireless networks configured inside your config 18 | ./wireless 19 | 20 | connects to network-name as long as it was found inside your config and is in range 21 | ./wireless network-name 22 | 23 | Want to see more on how it works? http://www.bsdguides.org/2012/a-simple-console-wireless-network-manager-for-openbsd/ 24 | -------------------------------------------------------------------------------- /hello_world.txt: -------------------------------------------------------------------------------- 1 | Just a test 2 | -------------------------------------------------------------------------------- /wireless: -------------------------------------------------------------------------------- 1 | #!/usr/bin/perl 2 | 3 | # Copyright 2012 - BSDGuides.org 4 | # 5 | # Permission is hereby granted, free of charge, to any person obtaining 6 | # a copy of this software and associated documentation files (the 7 | # "Software"), to deal in the Software without restriction, including 8 | # without limitation the rights to use, copy, modify, merge, publish, 9 | # distribute, sublicense, and/or sell copies of the Software, and to 10 | # permit persons to whom the Software is furnished to do so, subject to 11 | # the following conditions: 12 | # 13 | # The above copyright notice and this permission notice shall be 14 | # included in all copies or substantial portions of the Software. 15 | # 16 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 19 | # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 20 | # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 21 | # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 22 | # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 23 | 24 | use strict; 25 | use warnings; 26 | 27 | my $config_file = "/etc/wireless.cfg"; 28 | sub configure_wlan; 29 | sub dhclient; 30 | sub ifdown; 31 | 32 | die("You need root privileges to run $0\n") unless $> eq 0; 33 | die("Could not open $config_file\n") unless -e -r $config_file; 34 | die("$0 wants to run under OpenBSD sorry\n") unless $^O eq "openbsd"; 35 | 36 | system('clear'); 37 | 38 | my %configuration; 39 | my $current_config; 40 | my $current_wlan; 41 | my @allowed_config_keys = ("interface","wired_interface","wireless_interface","nwid","wpakey","name","type"); 42 | 43 | open FILE,"<",$config_file or die("Could not open $config_file\n"); 44 | 45 | while(){ 46 | chomp; 47 | next if($_ =~ m/^\s*$/); 48 | next if($_ =~ m/^#/); 49 | 50 | if($_ eq "[config]"){ 51 | $current_config = "config"; 52 | next; 53 | } 54 | if($_ eq "[network]"){ 55 | $current_config = "network"; 56 | $current_wlan += 1; 57 | next; 58 | } 59 | 60 | die("Config error: We need a format of key=value on Line $.\n") unless $_ =~ m/^\w+=.+$/; 61 | 62 | my ($key,$value) = split("=",$_); 63 | die("Config error: Unknown key: $key on Line $.\n") unless grep $_ eq $key, @allowed_config_keys; 64 | 65 | if($current_config eq "config"){ 66 | $configuration{$current_config}{$key} = $value; 67 | } elsif($current_config eq "network"){ 68 | $configuration{$current_config}{$current_wlan}{$key} = $value; 69 | } 70 | } 71 | close(FILE); 72 | 73 | my %config = %{$configuration{'config'}}; 74 | my %networks = %{$configuration{'network'}}; 75 | 76 | my $wireless_interface = $config{'wireless_interface'} || $config{'interface'} 77 | or die("Could not find name of wireless interface"); 78 | my $wired_interface = $config{'wired_interface'}; 79 | 80 | print "Listing available networks:\n"; 81 | print "-" x 50 . "\n\n"; 82 | 83 | printf("%-20s %-20s %-10s\n","Network Name","SSID","Type"); 84 | foreach my $index (sort (keys %networks)){ 85 | my %network = %{$networks{$index}}; 86 | printf("%-20s %-20s %-10s\n",$network{"name"},($network{"nwid"} || "-"),$network{"type"}); 87 | } 88 | 89 | print "\n"; 90 | print "-" x 50 . "\n"; 91 | print "Use ./wireless YOUR_NETWORK_NAME to connect\n\n"; 92 | 93 | if(@ARGV){ 94 | my $command = shift(@ARGV); 95 | my %connect; 96 | foreach my $index (keys %networks){ 97 | my %network = %{$networks{$index}}; 98 | if(uc($network{"name"}) eq uc($command)){ 99 | %connect = %network; 100 | } else { 101 | next; 102 | } 103 | } 104 | if(scalar(keys %connect) > 0){ 105 | if ($connect{'type'} eq 'wired') { 106 | die ("Could not find name of wired interface") unless $wired_interface; 107 | ifdown($wireless_interface); 108 | dhclient($wired_interface); 109 | } elsif ($connect{'type'} eq 'airplane') { 110 | ifdown($wired_interface); 111 | ifdown($wireless_interface); 112 | } else { 113 | ifdown($wired_interface) if $wired_interface; 114 | configure_wlan($wireless_interface,$connect{'nwid'},$connect{'wpakey'},$connect{'type'}); 115 | dhclient($wireless_interface); 116 | } 117 | } else { 118 | print "Network $command not found\n"; 119 | } 120 | } 121 | 122 | sub configure_wlan { 123 | my ($interface, $nwid, $wpakey,$type) = @_; 124 | print "Configuring SSID $nwid on $interface\n"; 125 | if(lc($type) eq 'open'){ 126 | system("ifconfig $interface nwid \"$nwid\" -wpakey"); 127 | } elsif(lc($type) eq 'wpa'){ 128 | system("ifconfig $interface nwid \"$nwid\" wpakey \"$wpakey\""); 129 | } else { 130 | die("Unknown Type: $type\n"); 131 | } 132 | } 133 | 134 | sub dhclient { 135 | my $interface = shift; 136 | print "Getting an IP address on $interface via DHCP\n"; 137 | system("dhclient $interface"); 138 | } 139 | 140 | sub ifdown { 141 | my $interface = shift; 142 | print "Bringing $interface down\n"; 143 | system("ifconfig $interface down"); 144 | } 145 | -------------------------------------------------------------------------------- /wireless.cfg: -------------------------------------------------------------------------------- 1 | [config] 2 | wired_interface=em0 3 | wireless_interface=athn0 4 | 5 | [network] 6 | name=Wired 7 | type=wired 8 | 9 | [network] 10 | name=Airplane 11 | type=airplane 12 | 13 | [network] 14 | name=Network1 15 | nwid=Network_SSID1 16 | wpakey=WPA_PASSPHRASE 17 | type=wpa 18 | 19 | [network] 20 | name=Network2 21 | nwid=Network_SSID2 22 | wpakey=WPA_PASSPHRASE 23 | type=open 24 | --------------------------------------------------------------------------------