aboutsummaryrefslogtreecommitdiff
path: root/usr
diff options
context:
space:
mode:
authorB. Stack <bgstack15@gmail.com>2016-11-04 14:42:01 -0400
committerB. Stack <bgstack15@gmail.com>2016-11-04 14:42:01 -0400
commit4428f400272c08039696de3a483156b8df3c8177 (patch)
treee6979f7eb7c8e48f39615114465009eee66d9410 /usr
downloadwifi-4428f400272c08039696de3a483156b8df3c8177.tar.gz
wifi-4428f400272c08039696de3a483156b8df3c8177.tar.bz2
wifi-4428f400272c08039696de3a483156b8df3c8177.zip
Initial commit
Diffstat (limited to 'usr')
l---------usr/bin/wifi1
-rw-r--r--usr/wifi/docs/nmcli.md13
-rw-r--r--usr/wifi/docs/wifi.spec52
-rw-r--r--usr/wifi/docs/wnic.md4
-rwxr-xr-xusr/wifi/inc/localize_git.sh2
-rwxr-xr-xusr/wifi/inc/scrub.py149
-rw-r--r--usr/wifi/inc/scrub.txt14
-rw-r--r--usr/wifi/networks/campus.wifi14
-rw-r--r--usr/wifi/networks/sample.wifi14
-rw-r--r--usr/wifi/packaging.txt36
-rwxr-xr-xusr/wifi/wifi.py123
11 files changed, 422 insertions, 0 deletions
diff --git a/usr/bin/wifi b/usr/bin/wifi
new file mode 120000
index 0000000..ec89aac
--- /dev/null
+++ b/usr/bin/wifi
@@ -0,0 +1 @@
+../wifi/wifi.py \ No newline at end of file
diff --git a/usr/wifi/docs/nmcli.md b/usr/wifi/docs/nmcli.md
new file mode 100644
index 0000000..09dc75c
--- /dev/null
+++ b/usr/wifi/docs/nmcli.md
@@ -0,0 +1,13 @@
+nmcli con add type wifi ifname "${wnic}" con-name EXAMPLE-Campus ssid EXAMPLE-Campus
+nmcli con edit id EXAMPLE-Campus
+nmcli> set ipv4.method auto
+nmcli> set 802-1x.eap peap
+nmcli> set 802-1x.phase2-auth mschapv2
+nmcli> set 802-1x.identity bgstack15
+nmcli> set 802-1x.password REDACTED
+nmcli> set 802-11-wireless-security.key-mgmt wpa-eap
+nmcli> set 802-11-wireless-security.wep-tx-keyidx 0
+nmcli> set 802-11-wireless-security.auth-alg open
+nmcli> save
+nmcli> activate
+
diff --git a/usr/wifi/docs/wifi.spec b/usr/wifi/docs/wifi.spec
new file mode 100644
index 0000000..f69bafe
--- /dev/null
+++ b/usr/wifi/docs/wifi.spec
@@ -0,0 +1,52 @@
+Name: wifi
+Version: 0.1
+#Release: 1%{?dist}
+Release: 1
+Summary: Wifi Configurator for bgstack15
+
+Group: Applications/Internet
+License: CC BY-SA 4.0
+URL: https://bgstack15.wordpress.com/
+Source0: wifi.tgz
+
+BuildRequires: python3-devel
+Requires: python34
+Packager: Bgstack15 <bgstack15@gmail.com>
+Buildarch: noarch
+
+%define _unpackaged_files_terminate_build 0
+%description
+Wifi provides a small python script that reads config files for specific networks. Adapt to your needs.
+
+%prep
+%setup
+
+
+%build
+
+%install
+rsync -a . %{buildroot}/
+
+%clean
+rm -rf %{buildroot}
+
+%post
+
+%preun
+
+%files
+/README.md
+%config /usr/wifi/networks/sample.wifi
+%config /usr/wifi/networks/campus.wifi
+%doc %attr(444, -, -) /usr/wifi/packaging.txt
+/usr/wifi/docs/wifi.spec.2
+/usr/wifi/docs/wifi.spec.1
+/usr/wifi/docs/nmcli.md
+/usr/wifi/docs/wifi.spec
+/usr/wifi/docs/wnic.md
+/usr/wifi/wifi.py
+%verify(link) /usr/bin/wifi
+
+%changelog
+* Thu Nov 3 2016 B Stack <bgstack15@gmail.com>
+- Wrote initial rpm package
diff --git a/usr/wifi/docs/wnic.md b/usr/wifi/docs/wnic.md
new file mode 100644
index 0000000..f7518bd
--- /dev/null
+++ b/usr/wifi/docs/wnic.md
@@ -0,0 +1,4 @@
+# Reference
+# this works in shell
+wnic=$( iw dev | grep -iE "Interface" | awk '{print $2}' )
+
diff --git a/usr/wifi/inc/localize_git.sh b/usr/wifi/inc/localize_git.sh
new file mode 100755
index 0000000..18334df
--- /dev/null
+++ b/usr/wifi/inc/localize_git.sh
@@ -0,0 +1,2 @@
+#!/bin/sh
+\cp -pRf /home/work/wifi.clean/.git /home/bgstack15-local/rpmbuild/SOURCES/wifi-0.1-1/
diff --git a/usr/wifi/inc/scrub.py b/usr/wifi/inc/scrub.py
new file mode 100755
index 0000000..52652fd
--- /dev/null
+++ b/usr/wifi/inc/scrub.py
@@ -0,0 +1,149 @@
+#!/bin/env python3
+# Filename: scrub.py
+# Location: Various
+# Author: bgstack15@gmail.com
+# Startdate: 2016-09-28
+# Title: Script that Simultaneously Copies and Scrubs a Directory
+# Purpose: Prepare projects for publication by removing private information like usernames and hostnames
+# Package: Various
+# History:
+# 2016-10-03 working on batch rename files
+# 2016-10-20 added not ".tgz" in source.name
+# 2016-10-27 Fixed error when trying chmod on a symlink
+# 2016-10-31 Handle symlinks by duplicating the link and stopping there.
+# Also separated directory, filename renaming tasks
+# Usage:
+# Store this file with any package that gets published. Adjust scrub.txt in local directory.
+# # First line: source directory Second line: target directory. WILL BE OVERWRITTEN!
+# /etc/ansible
+# /home/bjones/ansible.clean
+# # Rest of the lines are "OLD WORD" "NEW WORD"
+# bjones bgstack15
+# rsmith rmstack15
+# Reference:
+# http://stackoverflow.com/questions/79968/split-a-string-by-spaces-preserving-quoted-substrings-in-python/524796#524796
+# http://stackoverflow.com/questions/6706953/python-using-subprocess-to-call-sed#6707003
+# http://stackoverflow.com/questions/6584871/remove-last-character-if-its-a-backslash/6584893#6584893
+# http://stackoverflow.com/questions/2212643/python-recursive-folder-read/2212728#2212728
+# parallel lists: http://stackoverflow.com/questions/1663807/how-can-i-iterate-through-two-lists-in-parallel-in-python
+# file renames http://stackoverflow.com/questions/225735/batch-renaming-of-files-in-a-directory/7917798#7917798
+# Improve:
+# Add option to specify scrub file
+# Add exclude option to scrub file, such as .git and so on
+# Accept CLI options like source, destination, even exclusions?
+# Add flag for performing file renames as well, or file renames only
+import re, shlex, os, sys, shutil
+from pathlib import Path
+
+# scrubpy version
+scrubpyversion = "2016-10-31a"
+
+# Define functions
+
+def removeComments(string):
+ #string = re.sub(re.compile("/\*.*?\*/",re.DOTALL ) ,"", string)
+ #string = re.sub(re.compile("//.*?\n" ) ,"" ,string)
+ pattern = r"(\".*?\"|\'.*?\')|(/\*.*?\*/|(//|#)[^\r\n]*$)"
+ regex = re.compile(pattern, re.MULTILINE|re.DOTALL)
+ def _replacer(match):
+ if match.group(2) is not None:
+ return ""
+ else:
+ return match.group(1)
+ return regex.sub(_replacer, string)
+
+def isValidFile(_thisstring):
+ # return true if not png, tgz, or other non-text file
+ _isValidFile=True
+ if re.compile('.*\.(tgz|png|gif|jpg|pyc|pyo|git|swp)').match(_thisstring):
+ _isValidFile=False
+ #print( _thisstring + ": " + str(_isValidFile) )
+ return _isValidFile
+
+# Main code
+stringfile = open('scrub.txt','r')
+count=0
+thisdir=""
+newdir=""
+oldstrings=[]
+newstrings=[]
+
+while True:
+ x = stringfile.readline().rstrip()
+ count += 1
+ if not x: break
+ x = removeComments(x)
+ #print("x=" + x)
+ y = shlex.split (x)
+ if len(y) >= 1:
+ if thisdir == "":
+ thisdir = y[0]
+ elif newdir == "":
+ newdir = y[0]
+ if len(y) >= 2:
+ #print("y[0]=" + y[0] + "\t and y[1]=" + y[1])
+ oldstrings.append(y[0])
+ newstrings.append(y[1])
+
+# After the file is done
+stringfile.close()
+#newdir = thisdir.rstrip('\/') + ".scrubbed/"
+
+if False:
+ print("\nthisdir=" + thisdir)
+ print("newdir=" + newdir + '\n')
+ print("oldstrings are:")
+ print(oldstrings)
+ print("newstrings are:")
+ print(newstrings)
+
+# Clean scrubbed directory
+try:
+ shutil.rmtree(newdir)
+except:
+ foo=1
+
+shutil.copytree(thisdir,newdir,symlinks=True)
+
+# Execute substitutions
+for rootfolder, subdirs, files in os.walk(thisdir):
+ for filename in files:
+ sourcepath = os.path.join(rootfolder, filename)
+ destdir = rootfolder.replace(thisdir.rstrip('\/'),newdir.rstrip('\/'))
+ destfile = os.path.join(destdir, filename)
+ if os.path.islink( sourcepath ):
+ _symlinktarget = os.readlink( sourcepath )
+ try:
+ os.symlink( _symlinktarget, destfile )
+ except Exception as e:
+ pass
+ else:
+ with open( sourcepath, "r" ) as source:
+ #if not ".swp" in source.name and not ".git" in source.name and not ".tgz" in source.name:
+ if isValidFile( source.name ):
+ #print("sourcefile=" + source.name)
+ #print("destfile=" + destfile + '\n')
+ with open( destfile, "w") as target:
+ data = source.read()
+ for oldword, newword in zip(oldstrings, newstrings):
+ data = data.replace(oldword,newword)
+ changed = data
+ target.write(changed)
+
+# Execute directory renames
+for rootfolder, subdirs, files in os.walk(newdir):
+ for subdir in subdirs:
+ oldpath = os.path.join(rootfolder, subdir)
+ for oldword, newword in zip(oldstrings, newstrings):
+ if oldword in oldpath:
+ os.rename(oldpath, oldpath.replace(oldword,newword))
+
+# Execute file renames
+# Used "file renames" reference, as well as the structure of directory traversal used earlier, which was from a different source.
+for rootfolder, subdirs, files in os.walk(newdir):
+ for filename in files:
+ oldpath = os.path.join(rootfolder, filename)
+ for oldword, newword in zip(oldstrings, newstrings):
+ if oldword in oldpath:
+ #print("oldword=" + oldword + "\toldpath=" + oldpath)
+ os.rename(oldpath, oldpath.replace(oldword,newword))
diff --git a/usr/wifi/inc/scrub.txt b/usr/wifi/inc/scrub.txt
new file mode 100644
index 0000000..ed09117
--- /dev/null
+++ b/usr/wifi/inc/scrub.txt
@@ -0,0 +1,14 @@
+# First line: source directory Second line: target directory. WILL BE OVERWRITTEN!
+/home/bgstack15-local/rpmbuild/SOURCES/wifi-0.1-1
+/home/work/wifi.clean
+# rest of the lines are "OLD WORD" "NEW WORD"
+BGSTACK15 BGSTACK15
+Bgstack15 Bgstack15
+bgstack15 bgstack15
+Alice Alice
+User1 User1
+example example
+EXAMPLE EXAMPLE
+".com" ".com"
+"203.0." "203.0."
+one one
diff --git a/usr/wifi/networks/campus.wifi b/usr/wifi/networks/campus.wifi
new file mode 100644
index 0000000..bfa3fc5
--- /dev/null
+++ b/usr/wifi/networks/campus.wifi
@@ -0,0 +1,14 @@
+# File: campus.wifi
+# Purpose: Information to set up nmcli to connect to the EXAMPLE wireless network
+type wifi
+ifname $WNIC
+con-name EXAMPLE-Campus
+ssid EXAMPLE-Campus
+ipv4.method auto
+802-1x.eap peap # comment after peap
+802-1x.phase2-auth mschapv2
+802-1x.identity file:///etc/mount/.bgstack15.example.com
+802-1x.password file:///etc/mount/.bgstack15.example.com
+802-11-wireless-security.key-mgmt wpa-eap
+802-11-wireless-security.wep-tx-keyidx 0
+802-11-wireless-security.auth-alg open
diff --git a/usr/wifi/networks/sample.wifi b/usr/wifi/networks/sample.wifi
new file mode 100644
index 0000000..933776e
--- /dev/null
+++ b/usr/wifi/networks/sample.wifi
@@ -0,0 +1,14 @@
+# File: Example-Campus.wifi
+# Purpose: Information to set up nmcli to connect to the EXAMPLE wireless network
+type wifi
+ifname $WNIC
+con-name Example-Campus
+ssid Example-Campus
+ipv4.method auto
+802-1x.eap peap
+802-1x.phase2-auth mschapv2
+802-1x.identity file:///etc/mount/.bgstack15.example.com
+802-1x.password file:///etc/mount/.bgstack15.example.com
+802-11-wireless-security.key-mgmt wpa-eap
+802-11-wireless-security.wep-tx-keyidx 0
+802-11-wireless-security.auth-alg open
diff --git a/usr/wifi/packaging.txt b/usr/wifi/packaging.txt
new file mode 100644
index 0000000..c574282
--- /dev/null
+++ b/usr/wifi/packaging.txt
@@ -0,0 +1,36 @@
+File: usr/wifi/packaging.txt
+Package: wifi
+Author: bgstack15
+Startdate: 2016-11-03
+Title: Packaging instructions for wifi
+Purpose: To make it easier to build the package
+History:
+Usage:
+Reference: bgscripts packaging.txt
+ GrapefruiTgirl from http://www.linuxquestions.org/questions/linux-newbie-8/sed-awk-remove-section-from-file-840513/
+ https://fedoraproject.org/wiki/Packaging:Python?rd=Packaging/Python#Byte_Compiled_Files
+Improve:
+Document: Below this line
+
+### PACKAGING for rhel/centos/fedora
+package=wifi
+version=0.1-1
+shortversion=0.1
+rpmbuilddir=~/rpmbuild/
+packagespecfile="${package}-${version}/usr/${package}/docs/${package}.spec"
+#awk '{ if ($0 ~ /\%changelog|\%files/){output="off"; next}; if($0 ~ /\%/){output="on"; print;next;} if(output == "on"){print;};}' "${rpmbuilddir}/SOURCES/${packagespecfile}" > "${rpmbuilddir}/SOURCES/${packagespecfile}.swp" #remove files and changelog
+sed -n -e '1,/^\%files/p;' "${rpmbuilddir}/SOURCES/${packagespecfile}" > "${rpmbuilddir}/SOURCES/${packagespecfile}.swp" #remove files and changelog
+cd "${rpmbuilddir}/SOURCES/${package}-${version}/usr/bin"
+ln -sf ../wifi/wifi.py wifi
+cd "${rpmbuilddir}/SOURCES/${package}-${version}"
+find * ! -type d ! -regex '.*?.swp' ! -regex '.*?DEBIAN.*?' | sed -e 's/^/\//;s/\(.*\.txt\)/%doc %attr(444, -, -) \1/;' -e 's/\(.*bin.*\)/%verify(link) \1/;' -e 's/\(.*\.wifi\)/%config \1/;' >> "${rpmbuilddir}/SOURCES/${packagespecfile}.swp"
+printf "\n" >> "${rpmbuilddir}/SOURCES/${packagespecfile}.swp"
+sed -n -e '/^\%changelog/,$p' "${rpmbuilddir}/SOURCES/${packagespecfile}" >> "${rpmbuilddir}/SOURCES/${packagespecfile}.swp"
+mv -f "${rpmbuilddir}/SOURCES/${packagespecfile}.swp" "${rpmbuilddir}/SOURCES/${packagespecfile}"
+rm -rf "${rpmbuilddir}/SOURCES/${package}-${shortversion}"; /bin/cp -prf "${rpmbuilddir}/SOURCES/${package}-${version}" "${rpmbuilddir}/SOURCES/${package}-${shortversion}"
+rm -rf "${rpmbuilddir}/SOURCES/${package}-${shortversion}/DEBIAN"
+cd "${rpmbuilddir}/SOURCES"
+rm -rf "${package}.tgz"; tar -zc --exclude='.git' -f "${package}.tgz" "${package}-${shortversion}"
+/bin/cp -pf "${rpmbuilddir}/SOURCES/${packagespecfile}" "${rpmbuilddir}/SPECS"
+cd "${rpmbuilddir}/RPMS/noarch"
+rpmbuild -bb "${rpmbuilddir}/SPECS/${package}.spec"
diff --git a/usr/wifi/wifi.py b/usr/wifi/wifi.py
new file mode 100755
index 0000000..8a3bba0
--- /dev/null
+++ b/usr/wifi/wifi.py
@@ -0,0 +1,123 @@
+#!/usr/bin/python3
+# File: wifi.py
+# Author: bgstack15
+# Startdate: 2016-11-03
+# Title: Script that Connects to Wifi Using a Config File
+# Package: wifi
+# Purpose: To store wireless network settings and also use them
+# History:
+# Usage:
+# Reference:
+# get command output http://stackoverflow.com/questions/4760215/running-shell-command-from-python-and-capturing-the-output/13135985#13135985
+# also http://stackoverflow.com/questions/606191/convert-bytes-to-a-python-string#606199
+# man nmcli
+# Improve:
+# specify connection name or filename
+# provide --verbose flag
+
+import re, subprocess
+
+wifipyversion = "2016-11-03b"
+
+# DEFINE FUNCTIONS
+def _run_command(command):
+ # reference: http://stackoverflow.com/questions/4760215/running-shell-command-from-python-and-capturing-the-output/13135985#13135985
+ p = subprocess.Popen(command,
+ stdout=subprocess.PIPE,
+ stderr=subprocess.STDOUT)
+ return iter(p.stdout.readline, b'')
+
+def run_command(command):
+ rc_out = []
+ for line in _run_command(command.split()):
+ rc_out.append(line.decode("utf-8").rstrip())
+ return rc_out
+
+def getwnic():
+ getwnic = ""
+ for line in run_command('iw dev'):
+ if "Interface" in line:
+ getwnic = line.split()[1]
+ return getwnic
+
+def getvaluefromfile(filename, searchstring):
+ # looks for "searchstring=" and then returns whatever is after that on that line, in filename
+ thisvalue = ""
+ filename = re.sub("^file://", "", filename)
+ try:
+ for line in open(filename, 'r'):
+ line = line.strip()
+ if re.compile(searchstring + "=").match(line):
+ thisvalue = re.sub("^" + searchstring + "=", "", line)
+
+ except Exception as e:
+ print("Fatal error opening file: " + filename)
+ quit()
+
+ return thisvalue
+
+# SAMPLE VARIABLES
+configfile = "/home/bgstack15-local/software/wifi/campus.wifi"
+conname = "" # gets con-name
+nmcli_con_add = [] # gets type, ifname, con-name, ssid
+nmcli_con_mod = [] # gets everything else
+
+# PARSE CONFIG FILE
+for line in open(configfile):
+ usedline=0
+ line = re.sub('#.*', '', line).rstrip()
+ if line != "":
+ #print(line)
+ words = line.split()
+ if "type" in words[0] or "ifname" in words[0] or "con-name" in words[0] \
+ or "ssid" in words[0]:
+ if "$WNIC" in words[1]:
+ words[1] = str(getwnic())
+ nmcli_con_add.append(words[0] + " " + words[1])
+ usedline=1
+ if "con-name" in words[0]:
+ conname = words[1]
+ else:
+ # everything else goes to nmcli_con_mod
+ if "identity" in words[0]:
+ if "file:///" in words[1]:
+ nmcli_con_mod.append(words[0] + " " + getvaluefromfile(words[1],"username"))
+ usedline = 1
+ elif "password" in words[0]:
+ if "file:///" in words[1]:
+ nmcli_con_mod.append(words[0] + " " + getvaluefromfile(words[1],"password"))
+ usedline = 1
+ if usedline != 1:
+ nmcli_con_mod.append(words[0] + " " + words[1])
+
+# DEBUG
+if False:
+ print("nmcli_con_add: " + str(nmcli_con_add))
+ print("conname: " + conname)
+ print("nmcli_con_mod: " + str(nmcli_con_mod))
+
+# DELETE EXISTING CONNAME if exists
+nmcondel = "nmcli con del " + conname
+print(nmcondel)
+run_command(nmcondel)
+
+# EXECUTE NMCLI CON ADD
+nmconadd = "nmcli con add"
+for thisstring in nmcli_con_add:
+ nmconadd = nmconadd + " " + thisstring
+print(nmconadd)
+run_command(nmconadd)
+
+# EXECUTE NMCLI CON MODIFY
+nmconmod = "nmcli con modify " + conname
+nmconmodprint = "nmcli con modify " + conname
+for thisstring in nmcli_con_mod:
+ nmconmod = nmconmod + " " + thisstring
+ nmconmodprint = nmconmodprint + " " + re.sub("(password ).*","\\1REDACTED", thisstring)
+print(nmconmodprint)
+run_command(nmconmod)
+
+# EXECUTE NMCLI CON UP
+nmconup = "nmcli con up " + conname
+print(nmconup)
+run_command(nmconup)
bgstack15