I know there are other posts on this but i cannot get any of them to work. My problem is that i want to create a script on my rpi to automatically change wifi networks and change eth0 between static and dhcp. It is not yet finished i am currently only working on the wireless part. But the script so far reads…
#!/bin/bash
ANS=''
ssid=''
psk=''
file='/etc/network/interfaces'
function wireless {
echo 'The wireless network '$ssid' has now been set up'
start
}
function ssid {
echo 'What is your Network SSID?'
echo -e '> \c'
read ssid
echo 'You entered '$ssid'. Is this correct? (y/n)'
echo -e '> \c'
read ANS
if [ $ANS = 'y' ]; then
psk
else
echo 'Please renter your SSID'
ssid
fi
}
function psk {
echo 'What is your Network PSK?'
echo -e '> \c'
read psk
echo 'You entered '$psk'. Is this correct? (y/n)'
echo -e '> \c'
read ANS
if [ $ANS = 'y' ]; then
wireless
else
echo 'Please renter your PSK'
psk
fi
}
function start {
echo 'What do you want to do?'
echo ''
echo 'Press w to set up wireless ssid and psk'
echo 'Press s to change eth0 to a static ip address'
echo 'Press d to change eth0 to a dhcp ip address'
echo ''
echo 'Or press ctrl+c to quit'
echo -e '> \c'
read ANS
if [ $ANS = 'w' ]; then
ssid
else
if [ $ANS = 's' ]; then
static
else
if [ $ANS = 'd' ]; then
dhcp
fi
fi
fi
}
#backup of /etc/network/interfaces
#auto lo
#iface lo inet loopback
#iface eth0 inet dhcp
#iface eth0 inet static
# address ###########
# netmask #############
# broadcast ###########
# gateway ##########
#
#allow-hotplug wlan0
#
#auto wlan0
#
#iface wlan0 inet dhcp
#wpa-ssid "Home Network"
#wpa-psk "psk"
start
exit 0
So thats all fine but in the function wireless i want to send the psk and the ssid to lines 15 and 16 of /etc/network/interfaces. Please could someone tell me the best method to do this by.
Thanks
You can perform automatic edits using sed like this:
This command matches any complete line beginning with wpa-ssid and replaces it with “wpa-ssid” followed by the contents of the $ssid variable.
^wpa-ssid.*\$is the match pattern.^means the beginning of the line,.*means match anything, and\$means the end of the line.wpa-ssid \"$ssid\"/is what to replace it with.The -i option means to edit the file, rather than print the result to standard out.
psk will work the same way.
You should consider having your script back up the file the first time it is run.