Verify network identity through unix commands - macos

First some background: I'm using the LaunchD feature in Mac OSX to periodically launch an application I'll call "AppX". Optimally I like to run this application nearly 24/7. But due to issues with memory leakage (that is my best guess), AppX closes periodically. To solve this, I've created and loaded a simple plist file to launch the application every 6 hours. This itself works perfectly and minimizes application downtime. However, AppX itself can be a drain on my battery, and I'd prefer it only launch when I'm at home, connected to my wifi network.
Please be aware that while I have some experience with C++ and Java, I know very little in the way of Unix.
My question: I'd like to use an if statement to check whether the network I'm connected to is my home wifi network. Being the case that it is, the system will execute the command:
open -a AppX
So... How would I implement an if statement to accomplish this? Any help is appreciated.

There's an older SO question that gives part of the answer:
Get wireless SSID through shell script on Mac OS X
As for the if statement, the following should work:
homenet = "MyHomeNetwork"
netname = /System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport -I | awk '/ SSID/ {print substr($0, index($0, $2))}'
if [ "$netname" -eq "$homenet" ]
then
# Do fancy service startup here
else
# This is not my home network
fi

Related

xset in BASH script does not work under Cron

To learn about BASH scripting, I set myself the objective to write a Cron script which shuts down a PC with Mint 20 when activity on the Ethernet interface dropped below a threshold over an 1 hour.
I mainly (but not exclusively) use the PC as File/DLNA Server. The script works, but now I find that it also shuts down the PC the rare times I'm using the the front end. So I want my script to verify if the screen has been blanked (As per Power Management settings)
To test the principle I included this in my script:
screenon=$(/usr/bin/xset -q | grep 'Monitor is' | cut -d "s" -f 2)}
which when run in a terminal window gives (debug: set -x)
screenon= On
but when run from cron gives. (logger)
/usr/bin/xset: unable to open display ""
I have learned about similar problems, but cannot figure out how to solve this.
My script includes:PATH=$PATH:/usr/local/bin
and my PATH is: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
Thanks in advance for any help.

Is there a way to get my laptop to beep from within a bash script running on a remote server via SSH?

I have a bash script that I have to regularly run on a remote server. Part of the script includes running a backup which takes a while, and after it has run, I have to hit "Y" to confirm that the backup worked before the script will continue.
I would like to know if there is a way to get my laptop to make a beep (or some sort of sound) when that happens. I know that echo -e '\a' makes a beep, but if I run it from within a script on the remote server, the beep happens on the remote server.
I have control of the script that is being run, so I could easily change it to do something special.
You could send the command through ssh back to your computer like:
ssh user#host "echo -e '\a'"
Just make sure you have ssh key authentication from your server to your computer so the command can run smoothly
In my case the offered solutions with echo didn't work. I'm using a macbook and connect to an ubuntu system. I keep the terminal open and I'd like to be informed when a long running bash script is ready.
What I did notice is that if I shutdown the remote system then it will beep the macbook and show an alarm icon on the relevant tab. So I have now implemented a bit of dirty workaround:
sudo shutdown 1440 && shutdown -c
This will initiate the system to shutdown and will immediately cancel the request. And I do get the alarm beep + icon. You will need to setup sudo to allow the user to permit shutdown. As it was my own remote server it was no problem but could limit the usability for others.

How to compare if the cpu serial is correct

I have a question. I would like to check when the linux system starts or if the serial cpu is correct.
If not, he would be rebooting. So he would be doing reboot loops all the time.
I found the command to check the serial cpu command:
cat /proc/cpuinfo | grep Serial | cut -d ' ' -f 2
How to compare the result of this command to the value of eg 000000ddd0d0d??
And I do not know how to look like such a check script and where to put it in the Ubuntu system (/etc/init.d/rc.local ??).
It is correctly??:
#!/bin/bash
STR=cat /proc/cpuinfo | grep Serial | cut -d ' ' -f 2
if $STR != '000000ddd0d0d'; then
reboot
Thank you for your help
Sorry for my English.
You probably should do the whole thing in one grep command:
grep -q '^Serial.*000000ddd0d0d' /proc/cpuinfo || reboot
This will reboot unless a line with Serial and 000000ddd0d0d in it is found in /etc/cpuinfo.
BUT this is questionable for several reasons.
What should a reboot be good for in such a case? As this is supposed to be done during machine startup, you will then enter an infinite loop of reboots which can only be stopped by switching the computer off. This is horrible! You probably never encountered such a problem (as admin) or you wouldn't produce it voluntarily. The only way to fix such a system is to boot it via some other medium (USB thumb drive or similar).
Not all Linux distributions or kernel versions offer a CPU serial number in /proc/cpuinfo. My current system, for instance, doesn't. So on my computer this would not work at all.
The whole idea of reacting on a CPU serial number is highly questionable as a CPU might break (not likely but possible) and then probably is replaced and the new CPU, while surely having a different serial number, should not pose any trouble.
So, I think you might want to reconsider.

Regarding -i option usage of psexec

I was successfully running psexec to open application on remote PC using the following command:
psexec -s -i 1 \\135.20.230.160 -u administrator -p force calc
But suddenly today I found that 'calc' is not opening in the remote machine. Instead it is just running on the process list in task manager.
After some experiments when I changed '-i 1' to '-i 2' I found it working again.
Can anyone explain why this happened and how can I decide that the session number needs to be changed?
I need to build automation script for different users, so this is important to resolve.
Thanks.
You can use tasklist to display all tasks and see what session they are currently running on under the session# section.
The Psexec -i is asking for the session you would like to use.
Therefore as users log in to the machine the session numbers can be anywhere from 0 and up. To find out use tasklist with and check a process you know is running and view its session number.

How to wait in script until device is connected

I have a Sky wireless sensor node and a script which prints the output from the node.
sudo ./serialdump-linux -b115200 /dev/tmotesky1
If I start this script before my pc detects the node, I get the following error:
/dev/tmotesky1: No such file or directory
But if I wait for example 20 seconds, I miss the initial prints (which are important).
Is there a way to detect if the /dev/tmotesky1 exists?
Something like
while [ ! -f /dev/tmotesky1 ] ; do sleep 1; print 'Waiting...'; done
Thanks in advance!
Your code indicates that you are using Linux where you can use the hotplugging mechanism.
On generic systems, you can write an udev rule (--> see with udevadmin monitor -e what happens when you attach the device) which starts e.g. a program or writes something into a pipe. When systemd is used, you can start a service (see man systemd.device).
On small/embedded systems it is possible to write a custom /sbin/hotplug program (set in /proc/sys/kernel/hotplug) instead of using udev.

Resources