Page 1 of 1
netpro
Posted: Fri Apr 18, 2014 4:35 pm
by machinebacon
Problem: you have a netbook or laptop and need to switch network profiles (for example, wifi at home and at work/school) and you are a lazy ass who does not want to enter the passwords all the fucking time.
Solution: netpro - it lets you save and switch profiles. All interface profiles are saved in /etc/network/ and not readable as normal user.
Code: Select all
#!/bin/bash
ROOT_UID=0
if [ "$UID" -ne "$ROOT_UID" ]
then
echo "You must be root to run this script."
exit 87
fi
saveprofile()
{
echo "Enter name for profile: "
read proname
echo
echo "Saved as /etc/network/net.$proname"
cp /etc/network/interfaces /etc/network/net.$proname
}
loadprofile()
{
ls /etc/network/net.*
echo "Enter net. profile to load without prefix: "
read proname
echo
cp /etc/network/interfaces /etc/network/interfaces.backup
echo "Current profile saved as /etc/network/interfaces.backup"
cp /etc/network/net.$proname /etc/network/interfaces
# The next lines are optional, they automatically restart the active interface.
# snip ------------------------------------
iface=$(cat /etc/network/interfaces |grep iface|cut -d" " -f2|sed 's/lo//g')
echo "$proname loaded! Restarting $iface"
ifdown $iface
ifup $iface
# ------------------------------------ snap
}
editprofile()
{
nano /etc/network/interfaces
}
echo "[s]ave current network profile as ..."
echo "[l]oad profile from list"
echo "[e]dit current interfaces file"
echo "[q]uit"
read -n1 -s sel
if [ $sel = 's' ]; then
saveprofile
elif [ $sel = 'l' ]; then
loadprofile
elif [ $sel = 'q' ]; then
exit 0
elif [ $sel = 'e' ]; then
editprofile
fi
for the even more lazy+
Re: netpro
Posted: Fri Apr 18, 2014 4:36 pm
by machinebacon
Oh right, please test if it works for you. Thanks ;)
Re: netpro
Posted: Fri Apr 18, 2014 5:26 pm
by rust collector
This is not much of a test, but at least it shows the correct stuff for the network I am on.
I don't have any other networks to connect to, at the moment.
Re: netpro
Posted: Fri Apr 18, 2014 6:18 pm
by GekkoP
Nice, very nice.
Re: netpro
Posted: Sat Apr 19, 2014 7:00 pm
by wuxmedia
Very handy indeed!
Will test, this is what I spend a lot of time doing, go to work - change wifi, go home, change wifi... go home, home- change wifi.
Re: netpro
Posted: Wed Apr 30, 2014 9:00 pm
by wuxmedia
I forgot... to post that this works *perfectly*, hot swapping from work, to home, my flat, on the road.
For convenience I've, aliased the 'sudo' line and put into the sudoers file there's no password needed.
even works for my phone hotspot function, excellent work.
Re: netpro
Posted: Wed May 21, 2014 8:36 am
by machinebacon
That's great to know, thanks wux. I tried it on two PCs with 3 different locations and it does work for me, too.
Re: netpro
Posted: Wed May 21, 2014 10:38 am
by wuxmedia
It currently holds:
work, home, home2, home3, on-call flat, and the phone.
The sudoers trick makes it nice and quick.
Now, if one could pass it the saved name as an argument, that would be ultra sweet (and I suppose even xkeybound?) 8)
not related to your script, but I though ceni was supposed to support this 'roaming' - I never got it to work, surprise... 8)
Re: netpro
Posted: Wed May 21, 2014 1:29 pm
by machinebacon
oh that would be nice indeed. I look into it...
Re: netpro
Posted: Wed Dec 10, 2014 3:28 pm
by wuxmedia
Code: Select all
#!/bin/bash
# ripped off from bacon linuxbbq.org
# Rainbow edition 8)
ROOT_UID=0
if [ "$UID" -ne "$ROOT_UID" ]
then
echo -e "\E[31mYou must be root to run this script.\033[0m"
exit 87
fi
saveprofile()
{
echo -e "\E[35mEnter name for profile:\033[0m "
read proname
echo
echo -e "\E[32mSaved as /etc/network/net.$proname\033[0m"
cp /etc/network/interfaces /etc/network/net.$proname
}
loadprofile()
{
#echo "killing wlan0, for some reason or other"
#ifdown wlan0 > /dev/null 2>&1 && ifdown wlan0 > /dev/null 2>&1
clear
ls -1 /etc/network/net.* | cut -d . -f2
echo -e "\E[33mEnter net. profile to load:\033[0m "
read proname
echo
#echo "nameserver 8.8.8.8" >> /etc/resolv.conf
cp /etc/network/interfaces /etc/network/interfaces.backup
echo -e "\E[35mCurrent profile saved as /etc/network/interfaces.backup\033[0m"
cp /etc/network/net.$proname /etc/network/interfaces
# The next lines are optional, they automatically restart the active interface.
# snip ------------------------------------
iface=$(cat /etc/network/interfaces |grep iface|cut -d" " -f2|sed 's/lo//g')
echo -e "\E[32m$proname loaded!\033[0m \E[33mRestarting:\033[0m \E[34m$iface\033[0m"
ifdown $iface > /dev/null 2>&1
echo -e "\E[33mWorking...\033[0m"
ifup $iface > /dev/null 2>&1
echo -e "\E[32mDone!\033[0m"
# ------------------------------------ snap
}
editprofile()
{
nano /etc/network/interfaces
}
clear
echo -e "\E[36;40mType a key to choose your option:\033[0m"
echo -e "\E[34m[r]un CENI to config network\033[0m"
echo -e "\E[32m[s]ave current network profile as ...\033[0m"
echo -e "\E[33m[l]oad profile from list\033[0m"
echo -e "\E[35m[e]dit current interfaces file\033[0m"
echo -e "\E[31m[q]uit\033[0m"
read -n1 -s sel
if [ $sel = 's' ]; then
saveprofile
elif [ $sel = 'l' ]; then
loadprofile
elif [ $sel = 'q' ]; then
exit 0
elif [ $sel = 'e' ]; then
editprofile
elif [ $sel = 'r' ]; then
ceni
fi
All my own work *zazehem* - hmm might look into getting it to read the conf as an argument, now I more or less understand the layout.
Re: netpro
Posted: Wed Dec 10, 2014 4:47 pm
by machinebacon
completely forgot about this one... I'll do something that reads the conf without looking at your version, and then we compare penis length :D
Re: netpro
Posted: Wed Dec 10, 2014 7:01 pm
by wuxmedia
Heh, not a problem, works perfectly well right now, I was bored and decided to rainbow it clean the presets and just /dev/null the output, just ricing :)
Re: netpro
Posted: Tue Apr 21, 2015 7:20 am
by wuxmedia
more boredness:
Code: Select all
#!/bin/bash
# ripped off from bacon linuxbbq.org
# Rainbow edition 8)
# Pass config arg direct?
proname=("$@")
ROOT_UID=0
if [ "$UID" -ne "$ROOT_UID" ]
then
echo -e "\E[31mYou must be root to run this script.\033[0m"
exit 87
fi
saveprofile()
{
echo -e "\E[35mEnter name for profile:\033[0m "
read proname
echo
echo -e "\E[32mSaved as /etc/network/net.$proname\033[0m"
cp /etc/network/interfaces /etc/network/net.$proname
}
loadprofile()
{
#echo "killing wlan0, for some reason or other"
#ifdown wlan0 > /dev/null 2>&1 && ifdown wlan0 > /dev/null 2>&1
clear
ls -1 /etc/network/net.* | cut -d . -f2
echo -e "\E[33mEnter net. profile to load:\033[0m "
read proname
echo
cp /etc/network/interfaces /etc/network/interfaces.backup
echo -e "\E[35mCurrent profile saved as /etc/network/interfaces.backup\033[0m"
cp /etc/network/net.$proname /etc/network/interfaces
# The next lines are optional, they automatically restart the active interface.
# snip ------------------------------------
iface=$(cat /etc/network/interfaces |grep iface|cut -d" " -f2|sed 's/lo//g')
echo -e "\E[32m$proname loaded!\033[0m \E[33mRestarting:\033[0m \E[34m$iface\033[0m"
ifdown $iface > /dev/null 2>&1
echo -e "\E[33mWorking...\033[0m"
ifup $iface > /dev/null 2>&1
ping -c2 google.com > /dev/null
if [ $? -eq 0 ]
then
echo -e "\E[32mAll Done!\033[0m"
else
echo -e "\E[31mFailed :(\033[0m"
fi
# ------------------------------------ snap
}
editprofile()
{
nano /etc/network/interfaces
}
if [ -z ${proname+x} ]; then
clear
echo -e "\E[36;40mType a key to choose your option:\033[0m"
echo -e "\E[34m[r]un CENI to config network\033[0m"
echo -e "\E[32m[s]ave current network profile as ...\033[0m"
echo -e "\E[33m[l]oad profile from list\033[0m"
echo -e "\E[35m[e]dit current interfaces file\033[0m"
echo -e "\E[31m[q]uit\033[0m"
read -n1 -s sel
if [ $sel = 's' ]; then
saveprofile
elif [ $sel = 'l' ]; then
loadprofile
elif [ $sel = 'q' ]; then
exit 0
elif [ $sel = 'e' ]; then
editprofile
elif [ $sel = 'r' ]; then
ceni
fi
else
cp /etc/network/interfaces /etc/network/interfaces.backup
echo -e "\E[35mCurrent profile saved as /etc/network/interfaces.backup\033[0m"
cp /etc/network/net.$proname /etc/network/interfaces
# The next lines are optional, they automatically restart the active interface.
# snip ------------------------------------
iface=$(cat /etc/network/interfaces |grep iface|cut -d" " -f2|sed 's/lo//g')
echo -e "\E[32m$proname loaded!\033[0m \E[33mRestarting:\033[0m \E[34m$iface\033[0m"
ifdown $iface > /dev/null 2>&1
echo -e "\E[33mWorking...\033[0m"
ifup $iface > /dev/null 2>&1
ping -c2 google.com > /dev/null
if [ $? -eq 0 ]
then
echo -e "\E[32mAll Done!\033[0m"
else
echo -e "\E[31mFailed :(\033[0m"
fi
fi
It's dirty and nasty and doesn't reuse code, doesn't check to see if your $filename exists, however although a bit messy looking because bacon coded the original so robustly, it doesn't matter; just loads the backup or something :)
So anyway now it accepts command line config:
Re: netpro
Posted: Tue Apr 21, 2015 9:22 am
by machinebacon
Oh nice, thanks, it does what it should do!
Re: netpro
Posted: Tue Apr 21, 2015 10:29 am
by wuxmedia
well you are welcome :) I just bolted on a variable and reused your code.
I only move around a few places, so makes sense to pass an argument.
Was thinking of bloating it to grep the wifi SSID's available on 'resuming' then pass that to connect. then a fancy icon, then a signal strength... ten... I stopped myself :)
Re: netpro
Posted: Tue Apr 21, 2015 12:53 pm
by machinebacon
Well, there are some points to improve or change or worsen, as long as it does what its user wants it to do, it's OK :D and yes, we could have an icon/applet for it - provided we want to have GTK2 dependencies. :) Nice little exercise.
Re: netpro
Posted: Sat Jul 18, 2020 11:20 am
by wuxmedia
I would like to get this roam, I think that would be nice. not 100% sure how though
Re: netpro
Posted: Mon Aug 10, 2020 12:08 am
by catfood
wuxmedia wrote: ↑Wed Dec 10, 2014 7:01 pm
Heh, not a problem, works perfectly well right now, I was bored and decided to rainbow it clean the presets and just /dev/null the output, just ricing :)
I appreciate your rainbow terminal ricing, and also
Code: Select all
"# ripped off from bacon linuxbbq.org"
Any script I do finally get working is gonna have to say
#Bloated by leet haxxor
#Stolen from Pidsley, Machinebacon, and or both
#Even less warranty than original!