angel120
September 16th, 2009, 07:28 PM
On my home network, I have a Windows Home Server. I wrote a shell script for my laptop that runs at startup which checks my IP, and mounts the Samba shares if they match my home network's internal IP addresses (LAN and/or WLAN). However, I'd like to improve upon it.
Problems with my current script:
My server is set to a timer (WOL (Magic Packet) @ 10am, and hibernate at 11:59pm). So, if my server hibernates while the shares are still mounted, Nautilus will crash just by opening it (without even attempting to access the shares). (This also happened when I added the shares to /etc/fstab)
I added the script to Startup Applications (command: gksudo ./sambaMount.sh), but this slows down my startup, since the gksudo prompt seems to pause the system until the password is entered.
If my laptop hasn't finished connecting to the network before the script runs, the script will not mount the shares (because my IP won't match the ones in the script).
The script checks my IP addresses. If I'm on a different network (away from home), but my IP address happens to match either one in the script, I'm not quite sure what would happen.. (This may be a million-to-one shot, but I like to cover all bases.)
What I'd like to do (for now) is:
Check which SSID my laptop is connected to, as opposed to my IP address(es), and then mount the shares if the SSID=="Home".
Mount the shares without prompting for the root password (so it would be fully automated).
Mount only if the server is online.
Run the script when booting from a full shutdown, sleep, or hibernate. (Though I often use sleep)
Future plans (if possible):
Get rid of the server's timer, and have all my home computers wake the server as soon as they connect to the network.
Hibernate the server once all of my home computers are offline. (I don't like the idea of having my server run 24/7 if it's not needed, for the electric bill & security reasons.)
So, how would I go about doing these things?
Here is the source code:
#!/bin/sh
#Reads the current IP address. If it's on the Home network, it will mount
#AngelWHS shares. Otherwise, it will exit.
IP=`ifconfig | grep 'inet addr:' | grep -v '127.0.0.1' | cut -d: -f2 | awk '{ print $1}'`;
if [ "$IP" = "192.168.1.126" ] || [ "$IP" = "192.168.1.134" ];
then
echo "\nHoney, I'm home!\n"
smbmount //angelwhs/users/angel /media/DocumentsWHS -o credentials=/home/angel/.smbpasswd
echo "Mounted Documents"
smbmount //angelwhs/public /media/PublicWHS -o credentials=/home/angel/.smbpasswd
echo "Mounted Public"
smbmount //angelwhs/music/angel /media/MusicWHS -o credentials=/home/angel/.smbpasswd
echo "Mounted Music"
smbmount //angelwhs/photos/angel /media/PhotosWHS -o credentials=/home/angel/.smbpasswd
echo "Mounted Photos"
smbmount //angelwhs/videos /media/VideosWHS -o credentials=/home/angel/.smbpasswd
echo "Mounted Videos"
smbmount //angelwhs/software /media/SoftwareWHS -o credentials=/home/angel/.smbpasswd
echo "Mounted Software"
smbmount //angelwhs/torrents /media/TorrentsWHS -o credentials=/home/angel/.smbpasswd
echo "Mounted Torrents"
fi
As a side note, this is my first shell script, and I'm fairly new to Linux (I've been a Windows user all my life). I know a lot about computer hardware, but very little about networking. I'm also a 4th-year computer science student, and a fairly decent programmer.
Any and all help is appreciated. I wish I had a better understanding of Linux commands, but I'm sure I'll be able to do better over time.
-Angel.
Problems with my current script:
My server is set to a timer (WOL (Magic Packet) @ 10am, and hibernate at 11:59pm). So, if my server hibernates while the shares are still mounted, Nautilus will crash just by opening it (without even attempting to access the shares). (This also happened when I added the shares to /etc/fstab)
I added the script to Startup Applications (command: gksudo ./sambaMount.sh), but this slows down my startup, since the gksudo prompt seems to pause the system until the password is entered.
If my laptop hasn't finished connecting to the network before the script runs, the script will not mount the shares (because my IP won't match the ones in the script).
The script checks my IP addresses. If I'm on a different network (away from home), but my IP address happens to match either one in the script, I'm not quite sure what would happen.. (This may be a million-to-one shot, but I like to cover all bases.)
What I'd like to do (for now) is:
Check which SSID my laptop is connected to, as opposed to my IP address(es), and then mount the shares if the SSID=="Home".
Mount the shares without prompting for the root password (so it would be fully automated).
Mount only if the server is online.
Run the script when booting from a full shutdown, sleep, or hibernate. (Though I often use sleep)
Future plans (if possible):
Get rid of the server's timer, and have all my home computers wake the server as soon as they connect to the network.
Hibernate the server once all of my home computers are offline. (I don't like the idea of having my server run 24/7 if it's not needed, for the electric bill & security reasons.)
So, how would I go about doing these things?
Here is the source code:
#!/bin/sh
#Reads the current IP address. If it's on the Home network, it will mount
#AngelWHS shares. Otherwise, it will exit.
IP=`ifconfig | grep 'inet addr:' | grep -v '127.0.0.1' | cut -d: -f2 | awk '{ print $1}'`;
if [ "$IP" = "192.168.1.126" ] || [ "$IP" = "192.168.1.134" ];
then
echo "\nHoney, I'm home!\n"
smbmount //angelwhs/users/angel /media/DocumentsWHS -o credentials=/home/angel/.smbpasswd
echo "Mounted Documents"
smbmount //angelwhs/public /media/PublicWHS -o credentials=/home/angel/.smbpasswd
echo "Mounted Public"
smbmount //angelwhs/music/angel /media/MusicWHS -o credentials=/home/angel/.smbpasswd
echo "Mounted Music"
smbmount //angelwhs/photos/angel /media/PhotosWHS -o credentials=/home/angel/.smbpasswd
echo "Mounted Photos"
smbmount //angelwhs/videos /media/VideosWHS -o credentials=/home/angel/.smbpasswd
echo "Mounted Videos"
smbmount //angelwhs/software /media/SoftwareWHS -o credentials=/home/angel/.smbpasswd
echo "Mounted Software"
smbmount //angelwhs/torrents /media/TorrentsWHS -o credentials=/home/angel/.smbpasswd
echo "Mounted Torrents"
fi
As a side note, this is my first shell script, and I'm fairly new to Linux (I've been a Windows user all my life). I know a lot about computer hardware, but very little about networking. I'm also a 4th-year computer science student, and a fairly decent programmer.
Any and all help is appreciated. I wish I had a better understanding of Linux commands, but I'm sure I'll be able to do better over time.
-Angel.