Some shell scripts I've wrote

2016-08-03 Thread Walter Alejandro Iglesias
Hello,

Not what you asked for, but taking in care some people here complain
about not having a "desktop wireless connection app" as they got used by
the popular OSs, I'll share (shamelessly) what I improvised to solve my
specific needs with the aim to encourage others to write their own
solutions.

In my case, since I prefer to use ethernet cables and static IP
addresses for all machines in my home LAN, I wrote the following shell
scripts to connect my laptop in those occasions I'm out, in a bar or a
restaurant.  They are also intended to be useful individually; if at
some place I have an ethernet cable available, I directly run the second
one (dhcp-connect.sh) to establish a provisional dhcp connection, then
(optionally) when I shutdown the machine before living the place, the
third one (reset-LAN.sh) restores the LAN version of /etc/hosts and
/etc/resolv.conf so I don't need to bother about reseting them manually
when I'm back home.

I'm new to openbsd, it surely offers simpler ways to accomplish the same
tasks that I still ignore (advices welcome).


=
#!/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` "
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 ] &&

Re: Some shell scripts I've wrote

2016-08-03 Thread Walter Alejandro Iglesias
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` "
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

Re: Some shell scripts I've wrote

2016-08-03 Thread Steven Dee
Cool. Maybe a good complement for .

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` "
> 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 

Re: Some shell scripts I've wrote

2016-08-03 Thread Scott Bonds
I count myself among those who have taken a stab at automating wifi 
roaming in userland:


https://github.com/bonds/winot

To be clear, winot is far, far from production ready, its more of an 
excuse for me to play with Haskell at this point. But I've started 
adding to the Further Reading and Alternatives sections of the README 
when I see a thread on this topic, so maybe that could be useful to a 
wider audience.


On 08/03, Steven Dee wrote:

Cool. Maybe a good complement for .

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` "
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 >>