Monday, July 26, 2010

Command Line Favorites #2

Update to my previous post. This is a few more months of command line favs!


##System Version Checks
#
sysversionOSX=`system_profiler SPSoftwareDataType | grep "System Version:" | awk '{print $6}' | awk -F . '{print $1$2}'`
echo $sysversionOSX
#The version is displayed as the major version without periods. Thses are the possible results.
# 10.4.11 will equal 104
# 10.4.7 will equal 104
# 10.5 will equal 105
# 10.6 will equal 106
# 10.6.2 will equal 106

#This command will give you the whole OS with periods:
# system_profiler SPSoftwareDataType | grep "System Version:" | awk '{print $6}'
# Example Result: 10.6.2

#This command will show you witout the periods:
# system_profiler SPSoftwareDataType | grep "System Version:" | awk '{print $6}' | awk -F . '{print $1$2$3}'
# Example Result: 1062

#This will exit 1 if the version is 10.5 or less
# if [ "$sysversionOSX" -gt "105" ]
# then
# echo "Greater Than"
# exit 1
# fi

#This will exit 1 if the version is 10.6 or greater
if [ "$sysversionOSX" -lt "106" ]; then
echo "Less Than"
exit 1
else
echo "10.6 or higher"
fi

##Check DHCP packets
ipconfig getpacket en0

##Chech DS Cache
dscacheutil -statistics

##Watch files accessed by system
fs_usage

##List processes
ps -ef

##Show vm statistics
vm_stat

##Copy last 5 commands to textedit file
history -n -5 | open -f

##Pasteboard access
pbpaste

##Convert plist to xml1
plutil -convert xml1

##Set password policy for accounts
pwpolicy

##Spotlight info
mdls filename

##Spotlight find
mdfind variable

##List all RAID status
diskutil appleRAID list

##List bill of materials for packages
lsbom -p UGMsF pathto.bom

##Expand flat package
pkgutil --expand name.pkg destination

##List packages installed
repair_packages --list-standard-pkgs

##Repair Mail permissions
sqlite3 ~/Library/Envelope Index vacum

##Set Startup Disk
sudo systemsetup -setstartupdisk /System/Library/CoreServices

##Set fullname for user
sudo dscl . -create /Users/etcadmin RealName "etcadmin"

##Set User Picture
sudo dscl . -create /Users/etcadmin picture "/Library/User Pictures/ETC/etc.jpg"

##Set Auto login
/usr/libexec/PlistBuddy -c "Add autoLoginUser string etcadmin" $3/Library/Preferences/com.apple.loginwindow.plist

##Update auto login
/usr/libexec/PlistBuddy -c "Set autoLoginUser etcadmin" $3/Library/Preferences/com.apple.loginwindow.plist

##Remove Auto Login
/usr/libexec/PlistBuddy -c "Remove autoLoginUser string etcadmin" $3/Library/Preferences/com.apple.loginwindow.plist
OR
/usr/libexec/PlistBuddy -c "Set autoLoginUser" $3/Library/Preferences/com.apple.loginwindow.plist

##Find battery capacity information
ioreg -l | grep Capacity

##Find Battery Information
#Cycle Count
ioreg -l | grep Capacity | grep "Cycle Count"| cut -d, -f6 |sed -e s/\"//g | sed -e s/}//g
#Cycle Count Number Only
ioreg -l | grep Capacity | grep "Cycle Count"| cut -d, -f6 |sed -e s/\"Cycle\ Count\"=//g | sed -e s/}//g
#Max Capacity
ioreg -l | grep MaxCapacity | sed s/\"//g| awk '{print $3,$4,$5}'
#Max Capacity Number Only
ioreg -l | grep MaxCapacity | sed s/\"//g | awk '{print $5}'
#Current Capacity
ioreg -l | grep CurrentCapacity | sed s/\"//g| awk '{print $3,$4,$5}'
#Current Capacity Number Only
ioreg -l | grep CurrentCapacity | sed s/\"//g | awk '{print $5}'
##Design Capacity
ioreg -l | grep DesignCapacity | sed s/\"//g | awk '{print $3,$4,$5}'
#Design Capacity Number Only
ioreg -l | grep DesignCapacity | sed s/\"//g | awk '{print $5}'

##Allow local admins to auth screen saver
perl -i~backup -pe s/"account required pam_group.so no_warn group=admin,wheel fail_safe"/"account sufficient pam_group.so no_warn group=admin,wheel fail_safe"/g /etc/pam.d/screensaver

## Watch packagemaker changes to system
cd /Developer/Applications/Utilities/PackageMaker.app/Contents/MacOS
./packagemaker --watch

# Creating a snapshot package:
packagemaker --watch [normal options, except --doc and --root]

## Remove username from Cisco VPN menu
perl -i~backup -pe s/`cat /private/etc/opt/cisco-vpnclient/Profiles/ITS\ Wireless\ at\ University\ Park.pcf|grep Username`//g /private/etc/opt/cisco-vpnclient/Profiles/ITS\ Wireless\ at\ University\ Park.pcf

## Run script to update software
#!/bin/sh

#General settings to Software Updates to run in background defaults -currentHost write com.apple.SoftwareUpdate AgreedToLicenseAgrement -bool YES defaults -currentHost write com.apple.SoftwareUpdate AutomaticDownload -bool YES defaults -currentHost write com.apple.SoftwareUpdate LaunchAppInBackground -bool YES

#Set Software Update to run in background with no user interaction defaults write /System/Library/CoreServices/Software\ Update.app/Contents/Info NSUIElement -bool TRUE

#Check for new updates, what else?
/System/Library/CoreServices/Software\ Update.app/Contents/Resources/SoftwareUpdateCheck

#Re-enable Software Update to run in foreground defaults write /System/Library/CoreServices/Software\ Update.app/Contents/Info NSUIElement -bool FALSE

#Add all updates to update index under IsntallAtLogout section updatelist=(`defaults read /Library/Updates/index ProductPaths | grep -v "[{}]" | awk -F "=" '{print $1}' | grep -o "[^\" ]\+"`) defaults write /Library/Updates/index InstallAtLogout -array ${updatelist[*]}

#Tell OSX that we want to install updates at logout touch /var/db/.SoftwareUpdateAtLogout chmod og-r /var/db/.SoftwareUpdateAtLogout

#Check to see if a user is logged in or not if who | grep -q console; then
#user logged on
#Bring up an alert to reboot
osascript -e 'tell application "System Events" to display alert "A restart is required to finish updates. Please click restart to restart your computer now" buttons {"Cancel", "Restart"} as warning default button "Restart" cancel button "Cancel"
if button returned of result = "Restart" then
tell application "System Events" to restart
end if'
else

#login window
#reboot to install updates
#current errors out
osascript -e 'tell application "System Events" to tell process "SecurityAgent" to click button "Restart" of window 1'

fi

## Find UUID of computer
ioreg -rd1 -c IOPlatformExpertDevice | grep -E '(UUID)'| sed s/\"//g | sed s/\ //g | awk -F\= '{print $2}'

# Flush DNS Cache
# *10.6 and later
dscacheutil -flushcache

# 10.5.2 - 10.5.8
dscacheutil -flushcache

# 10.5.1 and earlier:
lookupd -flushcache

## List all local admins exept for root and etcadmin
sudo dscl . read /Groups/admin GroupMembership | sed 's/GroupMembership\:\ //g;s/root\ //g;s/etcadmin\ //g'

## Show All Extensions On turn on extensions in Finder and file dialogs
defaults write -g AppleShowAllExtensions -bool YES; killall Finder

## allow any user to set dvd region code
"$3"/usr/libexec/PlistBuddy -c "Set :rights:system.device.dvd.setregion.initial:class allow" "$3"/etc/authorization

## Remove "downloaded this app from internet" message
xattr -d com.apple.quarantine firefoxapp.app

## Move MacSpeech Data to local Network Users folders
for i in `ls /Users/NetworkUsers/`
do
sudo cp /Users/Shared/Library/Preferences/com.macspeech.dictate.plist /Users/NetworkUsers/$i/Library/Preferences/
done
sudo cp -R /Users/Shared/Library/Application\ Support/MacSpeech /Library/PSUadmin/guestmaster/Library/Application\ Support/
sudo cp /Users/Shared/Library/Preferences/com.macspeech.dictate.plist /Library/PSUadmin/guestmaster/Library/Preferences/

for i in `ls /Users/NetworkUsers/`
do
sudo cp -R /Users/Shared/Library/Application\ Support/MacSpeech /Users/NetworkUsers/$i/Library/Application\ Support/
sudo chmod 777 /Users/NetworkUsers/$i/Library/Application\ Support/MacSpeech
done

## Starts the Flurry screensaver over the login window when idle for 60 seconds
$defaults write "${PREFS_DIR}/com.apple.screensaver" loginWindowIdleTime -int 60
$defaults write "${PREFS_DIR}/com.apple.screensaver" loginWindowModulePath "/System/Library/Screen Savers/Flurry.saver"

## Wait for network to be ready
/usr/sbin/networksetup -detectnewhardware

## Enable Assistive Devices
sudo touch /private/var/db/.AccessibilityAPIEnabled

No comments: