Learning shell scripting while trying to fix Snow Leopard local network issue :)

Mac OS X is a great system, but this doesn’t mean it’s perfect and without issues. One thing is not working as it would (in Snow Leopard 10.6.X), it’s the automatic discovering and mounting of local machines connected to the same network. This is especially true if the network includes a lot of Windows computers. Theoretically those machines should be automatically discovered and displayed in Finder under “SHARED” label, but often this is not the case. Anyway in such cases is fortunately possible to connect to a particular device from Finder (once we know its IP address) by choosing “Connect to server…” (cmd+K) and by using the Samba protocol (ie: smb://ip-address). This is however very annoying, because we have to know each ip we want to connect to and type several addresses, so I spent some time to find a way to make this process automatic (because I didn’t find a solution to the original OS X issue) and I realized a shell script which tries to do the job the Leopard should do :)
I’m really a newbie when it comes to shell scripting (and networking too :P) and my script doesn’t work perfectly, anyway it does an acceptable work the most of times and although it doesn’t really solve the problem, it could help a lot.
The script is the following:


#!/bin/bash

# get user name
declare currentuser=$(whoami)

# get mac name
declare macname=$(scutil --get ComputerName)

# save arp command result
declare ipstring=$(arp -a)

# split ipstring into an array by using a regex to match ip addresses
declare -a iplist=($(echo $ipstring | grep -o -E "([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})"))

# display addresses found
echo "${#iplist[*]} ip addresses found"

# switch directory to user dir
cd /Users/$currentuser

# for each ip in list try to mount it using smbfs
for ip in ${iplist[@]}
do
if [ ! -d link-to-$ip ]
then
echo "creating directory link-to-$ip under /Users/$currentuser"
mkdir link-to-$ip
echo "trying to mount //$macname:@$ip/Public"
mount -t smbfs //$macname:@$ip/Public /Users/$currentuser/link-to-$ip
else
echo 'folder "link-to-$ip" already exists... skip'
fi
done

# complete
exit 0

Any comment and suggestion is really appreciated! Thanks