DaemonForums  

Go Back   DaemonForums > FreeBSD > FreeBSD General

FreeBSD General Other questions regarding FreeBSD which do not fit in any of the categories below.

Reply
 
Thread Tools Display Modes
  #1   (View Single Post)  
Old 23rd October 2008
ninjatux's Avatar
ninjatux ninjatux is offline
Real Name: Baqir Majlisi
Spam Deminer
 
Join Date: May 2008
Location: Antarctica
Posts: 293
Default Automation Script

I use this script in Mac OS X to handle some startup actions that I need automated. What do you think of the script? It runs well, but would you do it this way or another way?

Code:
# startup_actions (Automator action-"Run Shell Script")

# Set appropriate PATH variable
export PATH=/opt/local/bin:/opt/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin

# Wait 3 seconds for WiFi to come up
sleep 3

# Variable declarations
LOCAL_IP_TEST=192.168
LOCAL_IP=192.168.2.4
EXT_IP=67.84.74.65
TEST_SITE=www.google.com
MOUNT_PT=/Volumes/starbox_ninjasb
count=3

if [ 'ping -c $count "$TEST_SITE"' ] # Test for network connection
then
	# Create mount point if necessary
	if [ ! -e "$MOUNT_PT" ]
	then
		mkdir "$MOUNT_PT"
	fi

	# Mount /home/ninjasb@starbox via sshfs
	if ifconfig | grep "$LOCAL_IP_TEST" > /dev/null # Test connection type for local or ext
	then
		sshfs ninjasb@"$LOCAL_IP":/home/ninjasb "$MOUNT_PT" -oreconnect,volname=starbox_ninjasb
	else
		sshfs ninjasb@"$EXT_IP":/home/ninjasb "$MOUNT_PT" -oreconnect,volname=starbox_ninjasb
	fi
	
	# Start mpd if it isn't already running
	if [ -z $(pidof mpd) ]
	then
		sleep 1; mpd # Wait for sshfs mount to settle, then run mpd
	fi
else
	exit 0 # Fail-no internet connection
fi
__________________
"UNIX is basically a simple operating system, but you have to be a genius to understand the simplicity."
MacBook Pro (Darwin 9), iMac (Darwin 9), iPod Touch (Darwin 9), Dell Optiplex GX620 (FreeBSD 7.1-STABLE)
Reply With Quote
  #2   (View Single Post)  
Old 23rd October 2008
J65nko J65nko is offline
Administrator
 
Join Date: May 2008
Location: Budel - the Netherlands
Posts: 4,125
Default

You can replace
Code:
	# Create mount point if necessary
	if [ ! -e "$MOUNT_PT" ]
	then
		mkdir "$MOUNT_PT"
	fi
with a simpler
Code:
mkdir -p ${MOUNT_PT}
I would echo some information about the actions taken
Code:
# Mount /home/ninjasb@starbox via sshfs
echo $0 Testing connection type for local or ext ...
if ifconfig | grep "$LOCAL_IP_TEST" > /dev/null ; then
    echo $0: Using local IP address: ${LOCAL_IP}
    IP=${LOCAL_IP}
else
    echo $0: Using external  IP address : ${EXT_IP}
    IP=${EXT_IP}
fi

echo $0 : mounting in progress ....
sshfs ninjasb@"${IP}":/home/ninjasb "$MOUNT_PT" -oreconnect,volname=starbox_ninjasb
Doing something similar for the mpd connection is left as an exercise
__________________
You don't need to be a genius to debug a pf.conf firewall ruleset, you just need the guts to run tcpdump
Reply With Quote
  #3   (View Single Post)  
Old 24th October 2008
anomie's Avatar
anomie anomie is offline
Local
 
Join Date: Apr 2008
Location: Texas
Posts: 445
Lightbulb

I'd paint that bike shed like this:

Code:
#!/bin/sh

<init vars here> 

mount_it() {

  echo "Attempting to mount to ${1}..."

  # If connect scan to tcp 22 works, then mount
  ( nc -z -w 2 ${1} 22 >/dev/null ) && <sshfs mount ${1} here>

  # If that all worked, we get out
  if [ ${?} -eq 0 ] ; then
    echo 'Mounted.'
    exit 0
  fi

}

mount_it ${local_ip}
mount_it ${external_ip}

exit 0
netcat (nc) should be included with your Mac OSX base system.
__________________
Kill your t.v.
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Crontab won't run script Petrocelli08 FreeBSD General 4 7th March 2009 04:19 AM
Backup script(s)? giddyupman General software and network 2 3rd January 2009 02:06 PM
Handy X11 script Gabe_G23 Guides 6 25th October 2008 05:08 PM
my 1st sh script graudeejs Programming 12 18th August 2008 10:25 PM
Shell Script. bsdnewbie999 Programming 21 15th July 2008 07:54 AM


All times are GMT. The time now is 05:47 AM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Content copyright © 2007-2010, the authors
Daemon image copyright ©1988, Marshall Kirk McKusick