I was recently asked at work to write a small script that would restart AFP once a week. My boss wanted to get an e-mail with a list of the currently logged in users to AFP along with their respective IP addresses. This is a messy one liner to get most of the information, but it gets the job done:
sudo serveradmin command afp:command = getConnectedUsers | egrep '(ipAddress)|(name)' | sed '$!N;s/\n/ /' | cut -d '"' -f 2,4 | tr '"' '\t'
Here’s the full script:
# Created by: Mike Boylan # Robert Morris University # Date: Fri, Mar 4, 2011 logger -p local0.notice "AFP Restart script starting..." echo "AFP service is being restarted `date`" > /tmp/restartAFP.txt echo >> /tmp/restartAFP.txt echo "Currently Logged In Users" >> /tmp/restartAFP.txt echo >> /tmp/restartAFP.txt echo -e "IP Address:\tUsername:" >> /tmp/restartAFP.txt echo -e "-----------\t---------" >> /tmp/restartAFP.txt serveradmin command afp:command = getConnectedUsers | egrep '(ipAddress)|(name)' | sed '$!N;s/\n/ /' | cut -d '"' -f 2,4 | tr '"' '\t' >> /tmp/restartAFP.txt echo >> /tmp/restartAFP.txt echo >> /tmp/restartAFP.txt echo "Stopping AFP service..." >> /tmp/restartAFP.txt serveradmin stop afp >> /tmp/restartAFP.txt echo -e "\t...OK!" >> /tmp/restartAFP.txt echo >> /tmp/restartAFP.txt echo "Starting AFP service..." >> /tmp/restartAFP.txt serveradmin start afp >> /tmp/restartAFP.txt echo -e "\t...OK!" >> /tmp/restartAFP.txt `/usr/bin/mailx -s "Server1 AFP Restart Script Completed" email@example.com</tmp/restartAFP.txt` srm /tmp/restartAFP.txt logger -p local0.notice "AFP Restart script completed"