wifi-wand 2.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: ffa643d3118b6039e5335d2b390ccbaaad5b6985b633ccb6ddc06292547285f2
4
+ data.tar.gz: 95845961ff4f1c0e0a97a88f676f7fedf71b056cb530bc10fc735318ea23cc4b
5
+ SHA512:
6
+ metadata.gz: 8c8e7a084ca01bc32ea3975d3cfc0a42de836f5ccde28f0dbae78a5ee1709398301adba3ddbcfa23cb78f90625ceb58f06c9c831f22fc490e3166c4126a231aa
7
+ data.tar.gz: 6dec19538c4b1ecc0196144776e99e3f237a79a0495df5f06c74904489c051b460cf9707d09020f60bc804a9ee2766043602f99190c1577b8dcc1e3f53c4db26
data/.gitignore ADDED
@@ -0,0 +1,19 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ .idea/
19
+ mac-wifi-error-stack-trace.txt
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in wifi-wand.gemspec
6
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2017 Keith Bennett
2
+
3
+ MIT License
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.
data/README.md ADDED
@@ -0,0 +1,363 @@
1
+ # wifi-wand
2
+
3
+ To install this software, run:
4
+
5
+ `gem install wifi-wand`
6
+
7
+ or, you may need to precede that command with `sudo`:
8
+
9
+ `sudo gem install wifi-wand`
10
+
11
+ The `wifi-wand` gem enables the query and management
12
+ of wifi configuration and environment on a Mac.
13
+ The code encapsulates the Mac OS specific logic in a minimal class
14
+ to more easily add support for other operating systems,
15
+ but as of now, only Mac OS is supported. (Feel free to add an OS!)
16
+
17
+ It can be run in single-command or interactive mode.
18
+ Interactive mode uses the [pry](https://github.com/pry/pry) gem,
19
+ providing an interface familiar to Rubyists and other
20
+ [REPL](https://en.wikipedia.org/wiki/Read%E2%80%93eval%E2%80%93print_loop) users.
21
+
22
+
23
+ ### Usage
24
+
25
+ Available commands can be seen by using the `h` (or `help`) option. Here is its
26
+ output at the time of this writing:
27
+
28
+ ```
29
+ $ wifi-wand -h
30
+
31
+ Command Line Switches: [wifi-wand version 2.4.0]
32
+
33
+ -o[i,j,p,y] - outputs data in inspect, JSON, puts, or YAML format when not in shell mode
34
+ -s - run in shell mode
35
+ -v - verbose mode (prints OS commands and their outputs)
36
+
37
+ Commands:
38
+
39
+ a[vail_nets] - array of names of the available networks
40
+ ci - connected to Internet (not just wifi on)?
41
+ co[nnect] network-name - turns wifi on, connects to network-name
42
+ cy[cle] - turns wifi off, then on, preserving network selection
43
+ d[isconnect] - disconnects from current network, does not turn off wifi
44
+ h[elp] - prints this help
45
+ i[nfo] - a hash of wifi-related information
46
+ l[s_avail_nets] - details about available networks
47
+ n[etwork_name] - name (SSID) of currently connected network
48
+ on - turns wifi on
49
+ of[f] - turns wifi off
50
+ pa[ssword] network-name - password for preferred network-name
51
+ pr[ef_nets] - preferred (not necessarily available) networks
52
+ q[uit] - exits this program (interactive shell mode only) (see also 'x')
53
+ r[m_pref_nets] network-name - removes network-name from the preferred networks list
54
+ (can provide multiple names separated by spaces)
55
+ ro[pen] - open resource ('ipc' (IP Chicken), 'ipw' (What is My IP), 'spe' (Speed Test), 'this' (wifi-wand Home Page))
56
+ t[ill] - returns when the desired Internet connection state is true. Options:
57
+ 1) 'on'/:on, 'off'/:off, 'conn'/:conn, or 'disc'/:disc
58
+ 2) wait interval, in seconds (optional, defaults to 0.5 seconds)
59
+ w[ifion] - is the wifi on?
60
+ x[it] - exits this program (interactive shell mode only) (see also 'q')
61
+
62
+ When in interactive shell mode:
63
+ * use quotes for string parameters such as method names.
64
+ * for pry commands, use prefix `%`.
65
+ ```
66
+
67
+ Internally, it uses several Mac command line utilities. This is not ideal,
68
+ I would have preferred OS system calls, but the current approach enabled me to develop
69
+ this script quickly and simply.
70
+
71
+
72
+ ### Pretty Output
73
+
74
+ For nicely formatted output of the `info` command in non-interactive mode,
75
+ the `awesome_print` gem is used if it is installed;
76
+ otherwise, the somewhat less awesome pretty print (`pp`) is used. Therefore,
77
+ installation of the `awesome_print` gem is recommended.
78
+ This is accomplished by the following command:
79
+
80
+ `gem install awesome_print`
81
+
82
+ You may need to precede this command with `sudo `, especially if you are using the
83
+ version of Ruby that comes packaged with MacOS.
84
+
85
+
86
+ ### JSON, YAML, and Other Output Formats
87
+
88
+ You can specify that output in _noninteractive_ mode be in a certain format.
89
+ Currently, JSON, YAML, inspect, and puts formats are supported.
90
+ See the help for which command line switches to use.
91
+
92
+
93
+ ### Seeing the Underlying OS Commands and Output
94
+
95
+ If you would like to see the Mac OS commands and their output,
96
+ you can do so by specifying "-v" (for _verbose) on the command line.
97
+
98
+ You may notice that some commands are executed more than once. This is to simplify the application logic
99
+ and eliminate the need for the complexity of balancing the speed that a cache offers and the risk
100
+ of stale data.
101
+
102
+
103
+ ### Troubleshooting
104
+
105
+ If you try to run the shell, the script will require the `pry` gem, so that will need to be installed.
106
+ `pry` in turn requires access to a `readline` library. If you encounter an error relating to finding a
107
+ `readline` library, this can probably be fixed by installing the `pry-coolline` gem: `gem install pry-coolline`.
108
+ If you are using the Ruby packaged with Mac OS, or for some other reason require root access to install
109
+ gems, you will need to precede those commands with `sudo`:
110
+
111
+ ```
112
+ sudo gem install pry
113
+ sudo gem install pry-coolline
114
+ ```
115
+
116
+
117
+ ### Using It in Scripts
118
+
119
+ Sometimes calling `wifi-wand` from a script is handy. I have this script that
120
+ connects to a commonly used wifi network, and then speaks a message when it's done:
121
+
122
+ ```
123
+ wifi-wand connect my_network_name my_password && \
124
+ wifi-wand till conn && \
125
+ say -v Kyoko "Connected to my network."
126
+ ```
127
+
128
+ (The Mac OS `say` command supports all kinds of accents that are fun to play around with.
129
+ You can get a list of all of them by running `say -v "?"`)
130
+
131
+
132
+ ### Using the Shell
133
+
134
+ _If the program immediately exits when you try to run the shell, try upgrading `pry` and `pry-byebug`.
135
+ This can be done by running `gem install pry; gem install pry-byebug`._
136
+
137
+ The shell, invoked with the `-s` switch on the command line, provides an interactive
138
+ session. It can be useful when:
139
+
140
+ * you want to issue multiple commands
141
+ * you want to combine commands
142
+ * you want the data in a format not provided by this application
143
+ * you want to incorporate these commands into other Ruby code interactively
144
+ * you want to combine the results of commands with other OS commands
145
+ (you can shell out to run other command line programs by preceding the command with a period (`.`)) .
146
+
147
+
148
+ ### Using Variables in the Shell
149
+
150
+ There are a couple of things (that may be surprising) to keep in mind
151
+ when using the shell. They relate to the fact that local variables
152
+ and method calls use the same notation in Ruby (since use of parentheses
153
+ in a method call is optional):
154
+
155
+ 1) In Ruby, when both a method and a local variable have the same name,
156
+ the local variable will override the method name. Therefore, local variables
157
+ may override this app's commands. For example:
158
+
159
+ ```
160
+ [1] pry(#<WifiWandView>)> n # network_name command
161
+ => ".@ AIS SUPER WiFi"
162
+ [2] pry(#<WifiWandView>)> n = 123 # override it with a local variable
163
+ => 123
164
+ [3] pry(#<WifiWandView>)> n # 'n' no longer calls the method
165
+ => 123
166
+ [4] pry(#<WifiWandView>)> ne # but any other name that `network_name starts with will still call the method
167
+ => ".@ AIS SUPER WiFi"
168
+ [5] pry(#<WifiWandView>)> network_name
169
+ => ".@ AIS SUPER WiFi"
170
+ [6] pry(#<WifiWandView>)> ne_xzy123
171
+ "ne_xyz123" is not a valid command or option. If you intend for this to be a string literal, use quotes.
172
+ ```
173
+
174
+ If you don't want to deal with this, you could use global variables, instance variables,
175
+ or constants, which will _not_ hide the methods:
176
+
177
+ ```
178
+ [7] pry(#<WifiWandView>)> N = 123
179
+ [8] pry(#<WifiWandView>)> @n = 456
180
+ [9] pry(#<WifiWandView>)> $n = 789
181
+ [10] pry(#<WifiWandView>)> puts n, N, @n, $n
182
+ .@ AIS SUPER WiFi
183
+ 123
184
+ 456
185
+ 789
186
+ => nil
187
+ ```
188
+
189
+ 2) If you accidentally refer to a nonexistent variable or method name,
190
+ the result may be mysterious. For example, if I were write the wifi information
191
+ to a file, this would work:
192
+
193
+ ```
194
+ [1] pry(#<WifiWandView>)> File.write('x', info)
195
+ => 431
196
+ ```
197
+
198
+ However, if I forget to quote the filename, the program exits:
199
+
200
+ ```
201
+ [2] pry(#<WifiWandView>)> File.write(x, info)
202
+ ➜ wifi-wand git:(master) ✗ 
203
+ ```
204
+
205
+ What happened? `x` was assumed by Ruby to be a method name.
206
+ `method_missing` was called, and since `x` is the exit
207
+ command, the program exited.
208
+
209
+ Bottom line is, be careful to quote your strings, and you're probably better off using
210
+ constants or instance variables if you want to create variables in your shell.
211
+
212
+
213
+
214
+ ### Examples
215
+
216
+ #### Single Command Invocations
217
+
218
+ ```
219
+ wifi-wand i # prints out wifi info
220
+ wifi-wand a # prints out names of available networks
221
+ wifi-wand lsa # prints available networks detailed information
222
+ wifi-wand pr # prints preferred networks
223
+ wifi-wand cy # cycles the wifi off and on
224
+ wifi-wand co a-network a-password # connects to a network requiring a password
225
+ wifi-wand co a-network # connects to a network _not_ requiring a password
226
+ wifi-wand t on && say "Internet connected" # Play audible message when Internet becomes connected
227
+ ```
228
+
229
+ #### Interactive Shell Commands
230
+
231
+ When in shell mode, commands generally return the target object (e.g. the array of
232
+ available networks) rather than outputting a nicely formatted string.
233
+ This is intentional, so that you can compose expressions and in general
234
+ have maximum flexibility. The result may be that `pry` displays
235
+ that returned value in an ugly way.
236
+
237
+ If you don't need the return value but just want to display the value nicely,
238
+ you can use the `fancy_puts` method to output it nicely. An alias `fp` has been
239
+ provided for your convenience. You're welcome! For example:
240
+
241
+ ```
242
+ [5] pry(#<WifiWand::CommandLineInterface>)> fp pr.first(3)
243
+ [
244
+ [0] " AIS SMART Login",
245
+ [1] " BubblesLive",
246
+ [2] "#HKAirport Free WiFi"
247
+ ]
248
+ ```
249
+
250
+ For best display results, be sure `awesome_print` is `gem install`ed.
251
+ The program will silently use a not-as-nice formatter without it.
252
+ (This silence is intentional, so that users who don't want to install
253
+ `awesome-print` will not be bothered.)
254
+
255
+ If you want to suppress output altogether (e.g. if you are using the value in an
256
+ expression and don't need to see it displayed,
257
+ you can simply append `;nil` to the expression
258
+ and `nil` will be value output to the console. For example:
259
+
260
+ ```
261
+ [10] pry(#<WifiWand::CommandLineInterface>)> available_networks = avail_nets; nil
262
+ => nil
263
+ ```
264
+
265
+ #### Using the Models Without the Command Line Interface
266
+
267
+ The code has been structured so that you can call the models
268
+ from your own Ruby code, bypassing the command line interface.
269
+ Here is an example of how to do that:
270
+
271
+ ```ruby
272
+ require 'wifi-wand'
273
+ model = WifiWand::MacOsModel.new
274
+ puts model.available_network_names.to_yaml # etc...
275
+ ```
276
+
277
+
278
+ **More Examples**
279
+
280
+ (For brevity, semicolons are used here to put multiple commands on one line,
281
+ but these commands could also each be specified on a line of its own.)
282
+
283
+ ```
284
+ # Print out wifi info:
285
+ i
286
+
287
+ # Cycle (off/on) the network then connect to the specified network not requiring a password
288
+ > cycle; connect 'my-network'
289
+
290
+ # Cycle (off/on) the network, then connect to the same network not requiring a password
291
+ > @name = network_name; cycle; connect @name
292
+
293
+ # Cycle (off/on) the network then connect to the specified network using the specified password
294
+ > cycle; connect 'my-network', 'my-password'
295
+
296
+ > @i = i; puts "You are connected on port #{@i[:port]} to #{@i[:network]} on IP address #{@i[:ip_address]}."
297
+ You are connected on port en0 to .@ AIS SUPER WiFi on IP address 172.27.145.225.
298
+
299
+ > puts "There are #{pr.size} preferred networks."
300
+ There are 341 preferred networks.
301
+
302
+ # Delete all preferred networks whose names begin with "TOTTGUEST", the hard way:
303
+ > pr.grep(/^TOTTGUEST/).each { |n| rm(n) }
304
+
305
+ # Delete all preferred networks whose names begin with "TOTTGUEST", the easy way.
306
+ # rm can take multiple network names, but they must be specified as separate parameters; thus the '*'.
307
+ > rm(*pr.grep(/^TOTTGUEST/))
308
+
309
+ # Define a method to wait for the Internet connection to be active.
310
+ # (This functionality is included in the `till` command.)
311
+ # Call it, then output celebration message:
312
+ [17] pry(#<WifiWandView>)> def wait_for_internet; loop do; break if ci; sleep 0.5; end; end
313
+ [18] pry(#<WifiWandView>)> wait_for_internet; puts "Connected!"
314
+ Connected!
315
+
316
+ # Same, but using a lambda instead of a method so we can use a variable name
317
+ # and not need to worry about method name collision:
318
+ @wait_for_internet = -> { loop do; break if ci; sleep 0.5; end }
319
+ @wait_for_internet.() ; puts "Connected!"
320
+ Connected!
321
+ ```
322
+
323
+
324
+ ### Dependent Gems
325
+
326
+ Currently, the only gems used directly by the program are:
327
+
328
+ * `pry`, to provide the interactive shell
329
+ * `awesome_print` (optional), to more nicely format output in non-interactive mode
330
+
331
+ So the user can avoid installing gems altogether as long as they don't need to use the interactive shell,
332
+ and as long as they are comfortable with the less pretty output.
333
+
334
+
335
+ ### Password Lookup Oddity
336
+
337
+ You may find it odd (I did, anyway) that even if you issue the password command
338
+ (`mac_wifi pa a-network-name`) using sudo, you will still be prompted
339
+ with a graphical dialog for both a user id and password. This is no doubt
340
+ for better security, but it's unfortunate in that it makes it impossible to fully automate this task.
341
+
342
+ In particular, it would be nice for the `cycle` command to be able to fetch the current network's
343
+ password, cycle the network, and then reconnect to the original network with it after turning the network on.
344
+ However, since fetching the password without user intervention is not possible, this cannot be automated.
345
+
346
+ If you don't mind storing the network password in plain text somewhere, then you could easily
347
+ automate it (e.g. `wifi-wand cycle && wifi-wand connect a-network a-password`). Also, you might find it handy
348
+ to create a script for your most commonly used networks containing something like this:
349
+
350
+ ```
351
+ wifi-wand connect my-usual-network its-password
352
+ ```
353
+
354
+
355
+ ### License
356
+
357
+ MIT License (see LICENSE.txt)
358
+
359
+ ### Shameless Ad
360
+
361
+ I am available for consulting, development, tutoring, training, troubleshooting, etc.
362
+
363
+ You can contact me via GMail, Twitter, Github, and LinkedIn, as _keithrbennett_.
data/RELEASE_NOTES.md ADDED
@@ -0,0 +1,81 @@
1
+ ## v2.4.0
2
+
3
+ * Project has been renamed from 'mac-wifi' to 'wifi-wand'.
4
+ * Further preparation for addition of support of other OS's.
5
+ * Make resource opening OS-dependent as it should be.
6
+ * Move models to models directory.
7
+ * Refactored OS determination and model creation.
8
+ * Use scutil --dns to get nameserver info, using the union of the scoped and unscoped nameservers.
9
+
10
+
11
+ ## v2.3.0
12
+
13
+ * Add public IP address info to info hash (https://github.com/keithrbennett/macwifi/issues/3).
14
+ * Add nameserver information to info hash (issue at https://github.com/keithrbennett/macwifi/issues/5).
15
+ * Made all info hash keys same data type to be less confusing; made them all String's.
16
+ * Replace 'public-ip-show' with 'ropen', and provide additional targets ipchicken.com,
17
+ speedtest.net, and the Github page for this project
18
+ * Speed up retrieval of network name
19
+ * Remove BaseModel#run_os_command private restriction.
20
+
21
+
22
+ ## v2.2.0
23
+
24
+ * Add pu[blic-wifi-show] command to open https://www.whatismyip.com/ to show public IP address info.
25
+ * Removed 'vpn on' info from info hash; it was often inaccurate.
26
+
27
+
28
+ ## v2.1.0
29
+
30
+ * Support for the single script file install has been dropped. It was requiring too much complexity,
31
+ and was problematic with Ruby implementations lacking GEM_HOME / GEM_PATH environment variables.
32
+ * Code was broken out of the single script file into class files, plus a `version.rb`
33
+ and `mac-wifi.rb` file.
34
+
35
+
36
+ ## v2.0.0
37
+
38
+ * Support output formats in batch mode: JSON, YAML, puts, and inspect modes.
39
+ * Change some command names to include underscores.
40
+ * Shell mode is now (only) a command line switch (-s).
41
+
42
+
43
+ ## v1.4.0
44
+
45
+ * Support for "MAC-WIFI-OPTS" environment variable for configuration dropped.
46
+ * Support for "-v" verbosity command line option added.
47
+ * Work around pry bug whereby shell was not always starting when requested.
48
+ * 99% fix for reporting of available network names containing leading spaces
49
+ (this will not correctly handle the case of network names that are identical
50
+ except for numbers of leading spaces).
51
+ * Improved handling of attempting to list available networks when wifi is off.
52
+
53
+
54
+ ## v1.3.0
55
+
56
+ * Add partial JSON and YAML support.
57
+ * Script moved from bin to exe directory.
58
+ * Provide `fp` fancy print alias for convenience in shell.
59
+ * Command renames: 'lsp' -> 'prefnets', 'rm' -> 'rmprefnets'
60
+ * Add 'availnets' command for list of unique available network names.
61
+
62
+
63
+ ## v1.2.0
64
+
65
+ * Fix: protect against using command strings shorter than minimum length
66
+ (e.g. 'c', when more chars are necessary to disambiguate multiple commands).
67
+ * Improvements in help text and readme.
68
+
69
+
70
+ ## v1.1.0
71
+
72
+ * Sort available networks alphabetically, left justify ssid's.
73
+ * to_s is called on parameters so that symbols can be specified in interactive shell for easier typing
74
+
75
+
76
+ ## v1.0.0
77
+
78
+ * First versioned release.
79
+
80
+
81
+