How do I check for internet connection OSX - macos

My iMac wi-fi is a bit unstable. Sometimes it says it's connected and has full signal, but webpages won't load in Safari or Chrome. I have to turn wifi off/on to fix it.
The weird thing is when I ping google.com on Terminal, I have 0% packet loss.
PING www.google.com (68.104.213.123):
56 data bytes 64 bytes from 68.104.213.123: icmp_seq=0 ttl=52 time=21.530 ms
--- www.google.com ping statistics ---
1 packets transmitted, 1 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 21.530/21.530/21.530/0.000 ms
I made this Apple Script to try to fix this, but it seems like I need another method for checking actual internet connection instead of just pinging. What could I do?
repeat
try
do shell script "ping -o -t 2 www.google.com"
on error
say "Couldn't connect"
do shell script "networksetup -setairportpower en1 off"
do shell script "sleep 5"
do shell script "networksetup -setairportpower en1 on"
end try
delay 60
end repeat

curl www.apple.com will access the internet from terminal

Get inspiration from above question and answer, I changed code as below:
on idle
try
do shell script "curl -m 4 www.google.com"
on error
say "Couldn't connect"
do shell script "networksetup -setairportpower en1 off"
do shell script "sleep 5"
do shell script "networksetup -setairportpower en1 on"
end try
return 60
end idle
Because if you use repeat, you cannot exit program. By using on idle, you can easily exit program at any time. But you MUST save the script as runnable and check the keep it running checkbox when saving.

Related

AppleScript: how restart automatically the internet connection when it's stuck

When I upload a big amount of files through FTP for some reason my WiFi connection stops working. The connection doesn't show any problem and for solving this it's enough disable and re-enable the wifi. How to restart automatically it using AppleScript in the Script Editor?
My solution's this script that every second checks the connection to google and if there is a timeout close and reopen the en0 connection.
log "Internet checker: let's go"
repeat while true
try
do shell script "nc -w 4 -z 8.8.8.8 53"
# do shell script "nc -w 4 -z apple.com 80"
log "External TCP call test works"
on error
log "Restarting internet..."
do shell script "networksetup -setairportpower Wi-Fi off"
delay 2
do shell script "networksetup -setairportpower Wi-Fi on"
log "Done."
delay 8
end try
delay 2 # wait a moment before the next internet checking
end repeat
Update 24 Aug 022: This should be my 'definitive edition' of the script: privileges are no more requested. It used to be pretty slow to discover the lack of internet connection despite the -w timeout, so I try to ask directly to a "standard de facto" IP (a DNS server).
For more information about nc command you can consult this link: https://www.computerhope.com/unix/nc.htm

Using minicom to retrieve modem information stops after x seconds

I am using minicom in order to connect with my modem (quectelEC25). The goal is to send differente AT commands in order to retrieve ceratain information about the modem and save it in a outpu file. I wrote the following script in bash:
#!/bin/bash
while true;
do
sudo minicom -D /dev/ttyUSB2 -S script.txt -C AT_modems_responses_1.txt
sleep 1
done
Being the script.txt:
send AT
expect OK
send ATI
expect OK
send AT+COPS?
expect OK
start:
send AT+CCLK?
expect OK
send AT+CREG?
expect OK
send AT+CSQ
expect OK
sleep 1
goto start
The problem is that the AT commands stop working after 2 minutes (AT+CCLK? & AT+CSQ).
Why does it stop? What is the problem? Should I work with the AT commands in a different way?
Thank you in advance
The runscript by defautl exists after 120 seconds (2 minutes). This is the reason why the minicom was not working after 2 minutes, in order to run more time, a timeout has to be included in the script. For 5 minutes should be:
timeout 300
Don't know how it can be configured as infinite.

Apple script if while issiue

Hey guys my Applescript doesn't do what I expect.
Where is my mistake?
try
set NAS to do shell script "ping -c 1 192.168.222.5"
if NAS contains "100.0% packet loss" then repeat until NAS contains "0% packet loss"
do shell script "python /Users/Selim/Desktop/wol2.0.sh"
set NAS to do shell script "ping -c 1 192.168.222.5"
end repeat
delay 20
tell application "Terminal" to activate
end try
I want ping to my NAS and when I don't get response I want to wake him up, but my script stops after send ping without response.
Any idea what I am doing wrong?
I want to send packets until the NAS wakes up.
I have changed the Code
try
do shell script "ping -c 1 192.168.222.5"
on error
set NAS to "100% packet loss"
repeat while NAS contains "100% packet loss"
do shell script "python /Users/Selim/Desktop/wol2.0.sh"
set NAS to do shell script "ping -c 1 192.168.222.5"
if NAS contains "0% packet loss" then exit repeat
end repeat
say "Server startet, 20 sek"
delay 20
end try
tell application "Terminal" to activate
now i don't know how i get the loop to work with the on error command wich i get from the ping
Even if you have found the solution yourself there is no answer here so I try to complete it here. The string when we lay both strings on top of eachother we'll see "100.0%packet loss". As you can see when looking for "0% packet loss" it always will return true when you're sending just 1 packet. To be better safe than sorry you'd better match the entire line "1 packets transmitted, 1 packets received, 0.0% packet loss". Your first solution would be solved by changing the matches (also you can get rid if the if statement, if ping succeeds the first time the repeat will not run once):
set NAS to do shell script "ping -c 1 192.168.222.5 || true"
repeat until NAS contains "1 packets transmitted, 1 packets received, 0.0% packet loss"
do shell script "python /Users/Selim/Desktop/wol2.0.sh"
set NAS to do shell script "ping -c 1 192.168.222.5 || true"
end repeat
delay 20
tell application "Terminal" to activate
Update: A better version who does not match the printed string from ping but matches the returned number and coerce it into a boolean value.
repeat until ping("192.168.222.5")
do shell script "python /Users/Selim/Desktop/wol2.0.sh"
end repeat
delay 20
tell application "Terminal" to activate
on ping(IPNumber)
return (do shell script "ping -t 1 -c 1 " & IPNumber & " >/dev/null && echo yes || echo no") as boolean
end ping
the working script now:
try
do shell script "ping -c 1 192.168.222.5"
on error
set NAS to "q"
repeat until NAS contains "1 packets transmitted, 1 packets received, 0.0% packet loss"
do shell script "python /Users/Selim/Documents/scripte/wol2.0.sh"
delay 0.1
try
set NAS to do shell script "ping -c 1 192.168.222.5"
end try
end repeat
delay 5
end try
tell application "Kodi" to activate

Detecting the PC is running on UPS Power and shutdown Computer

I'm downloading huge sums of data at night and my PC in running through a UPS. Is there any way I can detect a power failure and command my PC to shut down automatically? Because I work at night, and there's no one to switch off the PC, it would be really helpful if anyone could help. Is it possible?
Thanks.
IMPORTANT: The scripts presented below will cause the system to shutdown whenever the network is down, so use them at your own risk!
A not so elegant way of doing what you want, if you have a network host (e.g. your router) that responds to ICMP echo requests and is not powered by the UPS (or at least the networking equipment is not powered by the UPS), would be to ping that host every few seconds and if it's down then shutdown the PC:
#!/bin/bash
while :
do
ping -c 1 -w 5 192.168.0.1 &> /dev/null
if [ $? -gt 0 ]; then
shutdown -hP now
break
fi
sleep 10s
done
You will have to change 192.168.0.1 to the IP address of the network host you want to ping.
You will also have to make the script executable with chmod +x <script_name> and place a call to it in /etc/rc.local (do not forget to append a & to make it run in the background) which will run the script as root on boot.
For completeness' sake, if the PC was running Windows XP one could use the following batch file:
:loop
ping -n 1 -w 5000 192.168.0.1
if not %ERRORLEVEL% == 0 (
shutdown -s
goto end
)
sleep 10
goto loop
:end
Note that the Windows batch file requires the sleep command which can be installed as part of the Windows Server 2003 Resource Kit Tools package (available as a free download from Microsoft's site)
If it's an APC and it has a data port you can use PowerChute. It's a java-based GUI (which could be a problem if this is your server) that does exactly what you are requesting.

How can I send a string to serial /dev/tty.* port, delay a second, disconnect from the port and continue my bash script in OSX?

This is in relation to resetting an Arduino, and then start pushing data to it from my usb xbee.
I've tried using screen, with no luck.
screen -S Xbee -d -m /dev/tty.usbserial-A900fra9 115200 *reset
I don't know how to close this session, not sure whether the args are correct, either.
to send anything to devices on /dev, you can use the > >> 2> 2>&1, etc.
Try this example from tty1 (ctrl+alt+F1):
echo "my string" > /dev/tty2
now go to tty2 (alt+F2) and you gonna see your string. It should work with any device.
and to sleep, use:
sleep 1
your problem could be also with permissions. Try it with root! ;)

Resources