Created
July 14, 2016 21:21
-
-
Save n8felton/e7ab5c1d37c4fc7ef6cb1cc225827c7d to your computer and use it in GitHub Desktop.
Snippets from http://zeroonelabs.com/snippets/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| _logger () { | |
| echo "$(basename $0): ${1}" | |
| # -i Log the process id of the logger process with each line. | |
| # -t [tag] Mark every line in the log with the specified tag. | |
| logger -i -t "$(basename $0) " "${1}" | |
| } | |
| # Turn off file globbing so we can pass asterisks as parameters | |
| set -f | |
| # Sets BASH to exit immediately if any command has a non-zero exit status. | |
| set -e | |
| # Sets BASH to exit if a veriable hasn't been previously defined. | |
| set -u | |
| # can also be "set -eu" | |
| # This sets the script to carry non-zero exit codes through a pipe. | |
| set -o pipefail | |
| # Saves the Internal Field Separator. | |
| SAVEIFS=$IFS | |
| # Set the Internal Field Separator to only Tabs and Newlines. | |
| IFS=$'\n\t' | |
| # $colorful is an option to print out colorful output if run through the shell. It helps | |
| #+ pick out lines where something failed. | |
| if [[ $colorful = 1 ]];then | |
| success="$(echo $'\e[1;4;32;40m SUCCESS \e[0m')" | |
| failure="$(echo $'\e[1;4;31;40m FAILURE \e[0m')" | |
| fi | |
| openssl md5 /path/to/file | |
| # MD5(/path/to/file)= kajsdhfjaghs8t233f92fhskdfhj | |
| whosloggedin () { | |
| ls -l /dev/console | awk '{ print $3 }' | |
| # Convert the username to lowercase | |
| ls -l /dev/console | awk '{ print $3 }' | tr "[:upper:]" "[:lower:]" | |
| } | |
| fieldset () { | |
| test -z "${4}" && echo "Error: The 4th field/argument is not set." && exit 1 | |
| test -n "${4}" && echo "The 4th field/argument is set." && exit 0 | |
| } | |
| listSetArgs () { | |
| argarray=(${@}) | |
| argcount=$(echo ${#argarray[*]}) | |
| argmax=$((${argcount}-1)) | |
| i=0 | |
| while [[ $i -le ${argmax} ]];do | |
| test -n $argarray[$i] && echo "$i is set, which is \"${argarray[$i]}\"" | |
| i=$(($i+1)) | |
| done | |
| } | |
| bootVolume="$(system_profiler SPSoftwareDataType | grep "Boot Volume" | awk -F ": " '{print $NF}')" | |
| # This is for OS X 10.9 | |
| listNonSystemAccounts () { | |
| nonSysAccounts="$(dscl . list /Users | grep -Ev "_amavisd|_appleevents|_appowner|_appserver|_ard|_assetcache|_atsserver|_avbdeviced|_calendar|_ces|_clamav|_coreaudiod|_cvmsroot|_cvs|_cyrus|_devdocs|_devicemgr|_dovecot|_dovenull|_dpaudio|_eppc|_ftp|_geod|_installassistant|_installer|_jabber|_kadmin_admin|_kadmin_changepw|_krb_anonymous|_krb_changepw|_krb_kadmin|_krb_kerberos|_krb_krbtgt|_krbtgt|_lda|_locationd|_lp|_mailman|_mcxalr|_mdnsresponder|_mysql|_netbios|_netstatistics|_networkd|_postfix|_postgres|_qtss|_sandbox|_screensaver|_scsd|_securityagent|_serialnumberd|_softwareupdate|_spotlight|_sshd|_svn|_taskgated|_teamsserver|_timezone|_tokend|_trustevaluationagent|_unknown|_update_sharing|_usbmuxd|_uucp|_warmd|_webauthserver|_windowserver|_www|daemon|nobody|root" | tr "\n" " ")" | |
| echo "<result>${nonSysAccounts}</result>" | |
| } | |
| _resetActiveDirectory () { | |
| rm /Library/Preferences/OpenDirectory/Configurations/Active\ Directory/*.plist | |
| } | |
| # | |
| # | |
| ## Networking | |
| # | |
| # | |
| # Get all network interfaces - will print out en0, en1, fw1, etc to build an array. | |
| NICarray=($(ifconfig | grep -E "[a-z][a-z].*\: fl" | awk -F":" '{print $1}')) | |
| # will return "inactive" or "active" | |
| _getNICstatus () { | |
| nicstatus="" | |
| nicstatus="$(ifconfig "${1}" | grep status | awk -F": " '{print $2}')" | |
| } | |
| for NIC in "${NICarray[@]}";do | |
| _getNICstatus "${NIC}" | |
| echo "$NIC:${nicstatus}" | |
| done | |
| # | |
| # Print ipv4 info | |
| echo -e "open\nget State:/Network/Global/IPv4\nd.show\nquit" | scutil | |
| # | |
| # What device is AirPort assigned to? (Will print out "en0" or "en1" etc. | |
| echo -e "open\nlist\nquit" | scutil | grep -E "Setup.*AirPort" | awk -F"/" '{print $4}' | |
| # | |
| # Get the network service name for a NIC id | |
| networksetup -listallhardwareports| grep -B1 en0| grep -v en0| awk -F": " '{print $2}' | |
| # | |
| # Get's NIC link status. Will print "TRUE" or "FALSE" | |
| echo -e "open\nget State:/Network/Interface/en2/Link\nd.show\nquit" | scutil | grep Active | awk -F" : " '{print $2}' | |
| # | |
| # Build array of network interface hardware IDs | |
| NICHIDs=( $(echo -e "open\nlist\nquit" | scutil | grep -E "Setup.*Service/[A-Z0-9]{8}-[A-Z0-9]{4}-[A-Z0-9]{4}-[A-Z0-9]{4}-[A-Z0-9]{12}$" | awk -F"/" '{print $NF}') ) | |
| # | |
| # Print the service name of each of the NIC HIDs | |
| for NICHID in ${NICHIDs[@]};do | |
| echo -e "open\nget Setup:/Network/Service/${NICHID}\nd.show\nquit" | scutil | grep UserDefinedName | awk -F " : " '{print $2}' | |
| done | |
| # | |
| _getIPaddress () { | |
| host ${1} | grep -oE "[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}" | |
| } | |
| listAllUsers () { | |
| dscl . list /Users UniqueID | awk '$2>=401{print}' | awk '{print $1}' | |
| } | |
| whatami () { | |
| stat -f "%HT%SY" "${1}" | awk '{print $1}' | |
| # Possible outputs are: | |
| # Directory (directory, duh) | |
| # Regular (regular file) | |
| # Symbolic (symbolic link) | |
| } | |
| whatmodeami () { | |
| # This version of stat only works with OS X I believe. | |
| stat -f "%p" "${1}"|cut -c 4-6 | |
| } | |
| backupfile () { | |
| test -f "${1}" && mv "${1}" "${1}".bak | |
| } | |
| removefile () { | |
| test -f "${1}" && rm "${1}" | |
| } | |
| sanitizeACLs () { | |
| # Removes ACLs on all files recursively. | |
| chmod -RN "${1}" | |
| } | |
| howLongHaveIbeenAsleep () { | |
| echo $((`ioreg -c IOHIDSystem | sed -e '/HIDIdleTime/!{ d' -e 't' -e '}' -e 's/.* = //g' -e 'q'` / 1000000000)) | |
| # The same thing, but in Perl | |
| ioreg -c IOHIDSystem | perl -ane 'if(/Idle/) {$idle=(pop @F)/1000000000; print $idle, "\n"; last;}' | |
| } | |
| # This brings the OS X user to the login screen. | |
| uiQuickLogout () { | |
| /System/Library/CoreServices/Menu\ Extras/User.menu/Contents/Resources/CGSession -suspend | |
| } | |
| # Set global hot key to trigger a service (/Library/Services/) | |
| setGlobalHotKey () { | |
| defaults write /Library/Preferences/.GlobalPreferences NSUserKeyEquivalents -dict-add "${1}" "${2}" | |
| # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # | |
| # "${1}" "${2}" # | |
| # setGlobalHotKey "Lock Screen" "@^l" # | |
| # Service name |||---- "l" key # | |
| # ||----- "^" Control key # | |
| # |------ "@" Command key # | |
| # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # | |
| } | |
| getNetworkID () { | |
| # Returns the MAC address of a NIC. | |
| networksetup -getinfo "${1}" |\ | |
| grep -E "[a-g0-9][a-g0-9]:[a-g0-9][a-g0-9]:[a-g0-9][a-g0-9]:[a-g0-9][a-g0-9]:[a-g0-9][a-g0-9]:[a-g0-9][a-g0-9]" |\ | |
| awk '{print $NF}' | |
| } | |
| # getNetworkID "Wi-Fi" | |
| timeMachineDiskCreate () { | |
| hdiutil create -size 100g -fs HFS+ -type SPARSEBUNDLE -volname "tmbackups" tmbackup_$(system_profiler SPHardwareDataType | grep UUID | awk '{print $3}').sparsebundle | |
| } | |
| # Gets the hardware UUID | |
| # e.g.: XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX | |
| getHardwareUUID () { | |
| system_profiler SPHardwareDataType | grep UUID | awk '{print $3}' | |
| } | |
| displayBoundDomain () { | |
| dsconfigad -show | awk '/Active Directory Domain/{print $NF}' | |
| } | |
| # Timeout script to ping a HTTP server and check to see if a document exists. | |
| apacheCheck () { | |
| curl -m 60 --raw "http://server.com/8usjneflzkkd | grep -o "404 Not Found" | |
| website="server.com" | |
| URI="Pages/index.html" | |
| curl -m 60 --raw http://"${website}"/"${URI}" | grep -o "404 Not Found" | |
| } | |
| # apacheCheck http://server.com | |
| whereAmI () { | |
| domainget="$(scutil -r server.com | awk -F, '{print $NF}')" | |
| if [[ "${domainget}" = "Reachable" ]] || [[ "${domainget}" = "Transient Connection" ]];then | |
| result="in" | |
| fi | |
| if [[ "${domainget}" = "Not Reachable" ]];then | |
| result="out" | |
| fi | |
| if [[ "${domainget}" = "Connection Required" ]];then | |
| result="offline" | |
| fi | |
| } | |
| screenResolution () { | |
| local screenWidth="$(system_profiler SPDisplaysDataType | grep -i resolution | awk -F': ' '{print $2}' | awk -F'x' '{print $1}' | sed 's/ //')" | |
| local screenHeight="$(system_profiler SPDisplaysDataType | grep -i resolution | awk -F': ' '{print $2}' | awk -F'x ' '{print $2}')" | |
| } | |
| for device in dvd cd music picture video;do | |
| echo "${device} status (0=disabled, 1=enabled): $(defaults read /Users/$(whoami)/Library/Preferences/com.apple.digihub com.apple.digihub.blank."${device[@]}".appeared)" | |
| done | |
| # List the path to each share per line. | |
| getShares () { | |
| sharing -l | grep "path:" | awk -F: '{print $NF}' | sed 's/^..//g' | |
| } | |
| # | |
| # Software Update | |
| # | |
| ## Get value of setting | |
| serveradmin settings swupdate:productItems:091-7250:CFBundleName | |
| ## Get name of update | |
| serveradmin settings swupdate:productItems:061-3770:CFBundleName | awk -F "=" '{ print $2}' | sed 's/ \"//g' | sed 's/\"//' | |
| ## Get all update product IDs | |
| serveradmin settings swupdate | grep -E "swupdate:productItems:.*:enable" | sed 's/swupdate\:productItems\://g' | sed 's/\:enable = yes//g' | sed 's/\:enable = no//g' | |
| ## Get whether an update is enabled or not | |
| _SUpdated () { | |
| serveradmin settings swupdate:productItems:$1:enable | awk -F "=" '{ print $2 }' | |
| } | |
| _SUval () { | |
| serveradmin settings swupdate:productItems:$1:$2 | |
| } | |
| # | |
| # Web | |
| # | |
| ## Get document root for Apache | |
| sudo serveradmin settings web:defaultSecureSite:documentRoot | awk -F"= " '{print $NF}' | sed 's/"//g' | |
| ## | |
| # | |
| # Processes | |
| # | |
| ## | |
| # Apache web service | |
| ps ax | grep -q http[d]_server_app;echo $? | |
| # Tomcat | |
| ps ax | grep -q "[J]SS/Tomcat";echo $? | |
| # MySQL | |
| ps ax | grep -q "[m]ysqld";echo $? | |
| # Misc | |
| enableTRIMsupport () { | |
| # This is for 3rd-party SSD drives in Macs | |
| sudo cp /System/Library/Extensions/IOAHCIFamily.kext/Contents/PlugIns/IOAHCIBlockStorage.kext/Contents/MacOS/IOAHCIBlockStorage /System/Library/Extensions/IOAHCIFamily.kext/Contents/PlugIns/IOAHCIBlockStorage.kext/Contents/MacOS/IOAHCIBlockStorage.original | |
| sudo perl -pi -e 's|(\x52\x6F\x74\x61\x74\x69\x6F\x6E\x61\x6C\x00{1,20})[^\x00]{9}(\x00{1,20}\x54)|$1\x00\x00\x00\x00\x00\x00\x00\x00\x00$2|sg' /System/Library/Extensions/IOAHCIFamily.kext/Contents/PlugIns/IOAHCIBlockStorage.kext/Contents/MacOS/IOAHCIBlockStorage | |
| sudo touch /System/Library/Extensions/ | |
| # Reboot is required | |
| } | |
| # Feedback | |
| # $(tput bel) | |
| # Bell when a command finishes (works when SSH'd into another machine) | |
| echo && tput bel | |
| # Disable the “reopen windows when logging back in” option | |
| # This works, although the checkbox will still appear to be checked, | |
| # and the command needs to be entered again for every restart. | |
| defaults write com.apple.loginwindow TALLogoutSavesState -bool false | |
| defaults write com.apple.loginwindow LoginwindowLaunchesRelaunchApps -bool false | |
| # Avoid creating .DS_Store files on network volumes | |
| defaults write com.apple.desktopservices DSDontWriteNetworkStores -bool true | |
| # Prevent Time Machine from prompting to use new hard drives as backup volume | |
| defaults write com.apple.TimeMachine DoNotOfferNewDisksForBackup -bool true | |
| # OS X Cisco Snippets | |
| # | |
| # | |
| # Cisco AnyConnect IPv4 address | |
| /path/to/cisco/anyconnect/bin/vpn stats | grep "Client Address (IPv4)" | awk -F":" '{print $2}' | sed 's/.*.\ //' | |
| # Is the cisco VPN agent running? | |
| ps ax | grep [v]pnagent # "wc -l = 1" | |
| # Returns the IP address of the Cisco Anyconnect VPN client. | |
| getCiscoIP () { | |
| /path/to/cisco/anyconnect/bin/vpn stats | grep "Address (IPv4" | awk '{print $NF}' | |
| } | |
| getCiscoState () { | |
| # If it prints "connect." it's not connected. | |
| # If it prints "Available." you're connected. | |
| /path/to/cisco/anyconnect/bin/vpn state | grep notice | awk '{print $NF}' | |
| } | |
| # LINUX SNIPPETS | |
| # Check if password is locked | |
| sudo passwd -S g1xyz01 | |
| # Unlock password | |
| sudo passwd -uf g1xyz01 | |
| # Check account status of number of failures | |
| sudo /sbin/pam_tally2 --reset |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment