Cool. Maybe a good complement for <https://github.com/mrdomino/autonet>.

On Wed, Aug 3, 2016 at 11:30 AM Walter Alejandro Iglesias <
roque...@gmail.com> wrote:

> Sorry!
>
> I have an entry in vimrc for my mail that replaces '>>' for '> >'.  That
> screwed the code, it was a bad idea.  Here the corrected code:
>
> =========================================================================
> #!/bin/sh
> # ~/bin/wifi.sh - occasional wireless connection in OpenBSD
>
> [ "`whoami`" != "root" ] && { echo "You must be root"; exit 1; }
>
> # PUT YOUR NORMAL USER HERE
> user=morlock
>
> # IMPORTANT: if you don't use dhcp in your home LAN save a copy of your
> # LAN version of /etc/resolv.conf and /etc/hosts to this directory.
> backdir=/home/$user/.wifi
>
> [ ! -d $backdir ] && mkdir $backdir
> rec=$backdir/stored
> [ ! -e $rec ] && {
>         touch $rec
>         chmod 600 $rec
>         chown $user:$user $rec
> }
> tmp=/tmp/wifi-`date +%H%M%S`
>
> # FUNCTIONS
> cancel()
> {
>         ifconfig $int -inet -inet6 -nwid -bssid -wpakey -nwkey
>         ifconfig $int down
>         [ -f $tmp ] && rm $tmp
>         [ -f $stored_tmp ] && rm $stored_tmp
>         exit 1
> }
>
> get_password()
> {
>         if grep -i $bssid $rec; then
>                 echo -n "Use the above \"$nwid\" stored password? [Y/n] "
>                 read answer
>                 if [ "$answer" != "n" ]; then
>                         password=`grep -i $bssid $rec | awk '{ print $2 }'`
>                 else
>                         printf "$nwid $enc $message: "
>                         read password
>                 fi
>         else
>                 printf "$nwid $enc $message: "
>                 read password
>         fi
> }
>
> # SELECT WIRELESS INTERFACE
> interfaces="`ifconfig wlan | awk -F: '/^[^\t]/ { print $1 }' | xargs`"
> if [ ! "$interfaces" ]; then
>         echo "No wireless interfaces found." 1>&2
>         exit 1
> elif [ `echo "$interfaces" | wc -w | xargs` -gt 1 ]; then
>         echo $interfaces
>         int=none
>         until echo $interfaces | grep -q $int; do
>                 echo -n "Interface? "
>                 read int
>         done
> else
>         int=$interfaces
> fi
>
> trap cancel INT
> ifconfig $int up
> ifconfig $int -inet -inet6 -nwid -bssid -wpakey -nwkey
>
> # SCAN AND CHOOSE AN ACCESS POINT
> echo 'Scanning on '$int'...'
> ifconfig $int scan | awk -F'\t' '/\tnwid/ { print $3 }' | nl -s') ' > $tmp
> if [ `awk 'END { print NR }' $tmp` -eq 0 ]; then
>         echo "No access points found."
>         cancel
> elif [ `awk 'END { print NR }' $tmp` -gt 1 ]; then
>         sed 's/\(.*\) nwid \(.*\) chan .*/\1 \2/' $tmp
>         ap=0
>         until egrep -q "^ *$ap\) nwid" $tmp ; do
>                 echo -n "number? "
>                 read ap
>         done
> else
>         ap=`awk -F\) '{ print $1 }' $tmp | sed 's/ *//'`
> fi
>
> # GET AP DATA
> bssid=`egrep '^ +'$ap')' $tmp | egrep -o '(..:){5}..' | tr "[a-f]" "[A-F]"`
> nwid=`grep -i $bssid $tmp | sed 's/.* nwid \(.*\) chan .*/\1/' | sed
> 's/"//g'`
> enc=`grep -i $bssid $tmp | awk -F, '{ print $NF }'`
>
> case $enc in
> wep)
>         key=nwkey
>         message="key (for HEX prefix 0x)"
>         get_password
>         ;;
> wpa*)
>         key=wpakey
>         message="passphrase"
>         get_password
>         ;;
> *)
>         key='-wpakey -nwkey'
>         password=''
>         ;;
> esac
>
> # SET UP INTERFACE
> ifconfig $int nwid "$nwid" $key $password || cancel
>
> # CONNECTION ATTEMPT
> /home/$user/bin/dhcp-connect.sh $int || cancel
>
> # STORE PASSWORD
> [ "$password" != "" ] && {
>         sed -i "/$bssid/d" $rec
>         echo -e "$bssid\t$password" >> $rec
> }
>
> # End of wifi.sh
>
>
> =======================================================================
> #!/bin/sh
> # ~/bin/dhcp-connect.sh
> # Connect using dhcp and set hostname (OpenBSD version)
>
> [ "`whoami`" != "root" ] && { echo "You must be root"; exit 1; }
>
> # PUT YOUR NORMAL USER HERE
> user=morlock
>
> # IMPORTANT: if you don't use dhcp in your home LAN save a copy of your
> # LAN version of /etc/resolv.conf and /etc/hosts to this directory.
> backdir=/home/$user/.wifi
>
> int=$1
> [ "$int" ] || {
>         echo "Usage: `basename $0` <interface>"
>         exit 1
> }
>
> clean_start()
> {
>         for i in `ps xw | grep dhclient | grep $int | \
>                 awk '{ print $1 }'`
>         do
>                 [ $i ] && kill $i
>         done
> }
> cancel()
> {
>         clean_start
>         [ -f $backdir/hosts ] && /home/$user/bin/reset-LAN.sh
>         exit 1
> }
> reset_LAN_at_shutdown()
> {
>         [ ! -e /etc/rc.shutdown ] && {
>                 echo "# /etc/rc.shutdown" > /etc/rc.shutdown
>                 chmod 600 /etc/rc.shutdown
>         }
>         grep -q "# Reset LAN" /etc/rc.shutdown 2>/dev/null || {
>                 echo >>/etc/rc.shutdown
>                 echo '# Reset LAN' >>/etc/rc.shutdown
>                 echo -n "[ -x /home/$user/bin/reset-LAN.sh ] && " \
>                         >>/etc/rc.shutdown
>                 echo "/home/$user/bin/reset-LAN.sh" >>/etc/rc.shutdown
>         }
> }
> dhclientConf()
> {
>         grep -q "send host-name \"`hostname`\"" \
>                 /etc/dhclient.conf 2>/dev/null ||
>                         echo "send host-name \"`hostname`\";" \
>                                 >>/etc/dhclient.conf
> }
>
> clean_start
> trap cancel INT
>
> # Comment this if you think you don't need it
> dhclientConf
>
> # Attempt a connection
> dhclient $int
>
> ip=`ifconfig $int | awk '/inet/ { print $2 }'`
> if [ "$ip" ]; then
>         [ -e $backdir/hosts ] && { # Set hosts file (Optional)
>                 echo "# /etc/hosts"                             >/etc/hosts
>                 echo "# (by $0)"
> >>/etc/hosts
>                 echo -e "127.0.0.1\tlocalhost"
> >>/etc/hosts
>                 echo -e "::1\tlocalhost"
> >>/etc/hosts
>                 echo -e "$ip\t`hostname`\t`hostname -s`"
> >>/etc/hosts
>                 reset_LAN_at_shutdown
>                 /etc/rc.d/smtpd restart
>                 grep -q ntpd /etc/rc.conf.local && /etc/rc.d/ntpd restart
>         }
> else
>         echo "`basename $0`: could't establish the connection."
>         cancel
> fi
>
> # End of dhcp-connect.sh
>
>
>
> =============================================================================
> #!/bin/sh
> # ~/bin/reset-LAN.sh - Reset your home LAN (OpenBSD version)
>
> [ "`whoami`" != "root" ] && { echo "You must be root" 1>&2; exit 1; }
>
> # PUT YOUR NORMAL USER HERE
> user=morlock
>
> # IMPORTANT: if you don't use dhcp in your home LAN save a copy of your
> # LAN version of /etc/resolv.conf and /etc/hosts to this directory.
> backdir=/home/$user/.wifi
>
> diff -q $backdir/resolv.conf /etc/resolv.conf || {
>         cp $backdir/resolv.conf /etc || exit 1
>         echo "`basename $0`: restored LAN version of /etc/resolv.conf"
> }
> diff -q $backdir/hosts /etc/hosts || {
>         cp $backdir/hosts /etc || exit 1
>         echo "`basename $0`: restored LAN version of /etc/hosts"
> }
>
> # End of reset-LAN.sh
>
>
>
>         ***
>
>
> Finally, to add a menu entry to the window manager:
>
> echo 'permit nopass <your_user> cmd /home/<your_user>/bin/wifi.sh' \
>         >>/etc/doas.conf
>
> In ~/.cwmrc:
>
> command wifi-connect "xterm -title wifi-connect \
>         -e \"doas /home/<your_user>/bin/wifi.sh; echo 'quiting...'; sleep
> 4\""
>
> In a ~/.fvwmrc menu entry:
>
> + "WiFi connect" Exec exec xterm -title "WIFI Connect" \
>         -e "doas /home/<your_user>/bin/wifi.sh; echo 'quiting...'; sleep 4"

Reply via email to