Skip to content

Instantly share code, notes, and snippets.

@ledangtuanbk
Last active May 6, 2024 16:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ledangtuanbk/33b1f4e749bf4b2d039b210ea828651b to your computer and use it in GitHub Desktop.
Save ledangtuanbk/33b1f4e749bf4b2d039b210ea828651b to your computer and use it in GitHub Desktop.
Save connected wifi by date
echo "start `date`"
# Get the directory of the currently running script
script_dir=$(dirname "$0")
# Change the working directory to the directory of the script
cd "$script_dir"
echo `pwd`
results="results.csv"
if [[ -f $results ]]; then
echo "File exists."
else
echo "File does not exist."
touch $results
fi
# Now you're in the directory of the running script
# Get network name
wifiName=$(/usr/sbin/networksetup -getairportnetwork en0)
date=$(date +"%Y-%m-%d")
wifiName="${wifiName#"${wifiName%%[![:space:]]*}"}"
wifiName="${wifiName%"${wifiName##*[![:space:]]}"}"
out="$date;$wifiName"
#out="2024-04-24, Current Wi-Fi Network: Deputy_VN"
echo $out
if grep "$out" $results; then
echo "Text found in the file."
else
echo "Text not found in the file."
echo $out >> $results
fi
@ledangtuanbk
Copy link
Author

ledangtuanbk commented Apr 25, 2024

Configure to run automatically

Because MacOS have some security System Integrity Protection (SIP)

1 save file as ~/run.sh

2 create a crontab by run command

crontab -e
as content below
* * * * * ~/run.sh >> ~/crontab.log 2>&1

Check log at

~/crontab.log

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment