public inbox for gentoo-user@lists.gentoo.org
 help / color / mirror / Atom feed
* [gentoo-user] ip_change_notifier - empty IP address
@ 2023-06-27 16:34 thelma
  2023-06-27 17:04 ` Neil Bothwick
  2023-06-28 14:37 ` Matt Connell
  0 siblings, 2 replies; 10+ messages in thread
From: thelma @ 2023-06-27 16:34 UTC (permalink / raw
  To: Gentoo mailing list

I run this little script to notify me when the IP changes.  However at time to time the file: "old_ip.txt"
is being populated with an empty line "no IP address"

What could be causing it?


#!/bin/bash

# Replace "YOUR_EMAIL" with your actual email address
EMAIL="your_email@gmail.com"

# File to store the old IP address
IP_FILE="/home/fd/business/scripts/old_ip.txt"

# Read the old IP address from the file
OLD_IP=$(cat "$IP_FILE")

# Function to retrieve the IP address
get_ip_address() {
     local ip_address
     ip_address=$(curl -s https://ifconfig.me/ip)
     echo "$ip_address"
}

# Query the API to get the current IP address
NEW_IP=$(curl -s https://api.ipify.org)

sleep 2

# Compare the new IP address with the old one
if [[ "$NEW_IP" != "$OLD_IP" ]]; then
     echo "Your IP address has changed to $NEW_IP"

     # Send an email notification
     echo "New IP address: $NEW_IP" | mailto -s "IP address change" $EMAIL

     # Allow overwriting of the file
     set +o noclobber

     # Update the old IP address in the file
#     printf "%s" "$NEW_IP" > "$IP_FILE"
      echo -n "$NEW_IP" > "$IP_FILE"

     # Restore the noclobber option
     set -o noclobber

else
     echo "Your IP address is still $OLD_IP"
fi


-- 
Thelma


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [gentoo-user] ip_change_notifier - empty IP address
  2023-06-27 16:34 [gentoo-user] ip_change_notifier - empty IP address thelma
@ 2023-06-27 17:04 ` Neil Bothwick
  2023-06-27 17:48   ` thelma
  2023-06-28 14:37 ` Matt Connell
  1 sibling, 1 reply; 10+ messages in thread
From: Neil Bothwick @ 2023-06-27 17:04 UTC (permalink / raw
  To: gentoo-user

[-- Attachment #1: Type: text/plain, Size: 1855 bytes --]

On Tue, 27 Jun 2023 10:34:22 -0600, thelma@sys-concept.com wrote:

> I run this little script to notify me when the IP changes.  However at
> time to time the file: "old_ip.txt" is being populated with an empty
> line "no IP address"
> 
> What could be causing it?
> 
> 
> #!/bin/bash
> 
> # Replace "YOUR_EMAIL" with your actual email address
> EMAIL="your_email@gmail.com"
> 
> # File to store the old IP address
> IP_FILE="/home/fd/business/scripts/old_ip.txt"
> 
> # Read the old IP address from the file
> OLD_IP=$(cat "$IP_FILE")
> 
> # Function to retrieve the IP address
> get_ip_address() {
>      local ip_address
>      ip_address=$(curl -s https://ifconfig.me/ip)
>      echo "$ip_address"
> }
> 
> # Query the API to get the current IP address
> NEW_IP=$(curl -s https://api.ipify.org)

I found this site very slow to respond. It could be timing out with an
empty result. OTOH the URL used in the get_ip_address function is rapid,
but you don't call that function.

> 
> sleep 2
> 
> # Compare the new IP address with the old one
> if [[ "$NEW_IP" != "$OLD_IP" ]]; then
>      echo "Your IP address has changed to $NEW_IP"
> 
>      # Send an email notification
>      echo "New IP address: $NEW_IP" | mailto -s "IP address change"
> $EMAIL
> 
>      # Allow overwriting of the file
>      set +o noclobber
> 
>      # Update the old IP address in the file
> #     printf "%s" "$NEW_IP" > "$IP_FILE"
>       echo -n "$NEW_IP" > "$IP_FILE"
> 
>      # Restore the noclobber option
>      set -o noclobber

You don't need to play with the noclobber option, just use >| for
redirection, which will always overwrite the file.

> 
> else
>      echo "Your IP address is still $OLD_IP"
> fi
> 
> 




-- 
Neil Bothwick

Linux like wigwam. No windows, no gates, Apache inside.

[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [gentoo-user] ip_change_notifier - empty IP address
  2023-06-27 17:04 ` Neil Bothwick
@ 2023-06-27 17:48   ` thelma
  2023-06-27 19:23     ` Neil Bothwick
  0 siblings, 1 reply; 10+ messages in thread
From: thelma @ 2023-06-27 17:48 UTC (permalink / raw
  To: gentoo-user


On 6/27/23 11:04, Neil Bothwick wrote:
> On Tue, 27 Jun 2023 10:34:22 -0600, thelma@sys-concept.com wrote:
> 
>> I run this little script to notify me when the IP changes.  However at
>> time to time the file: "old_ip.txt" is being populated with an empty
>> line "no IP address"
>>
>> What could be causing it?
>>
>>
>> #!/bin/bash
>>
>> # Replace "YOUR_EMAIL" with your actual email address
>> EMAIL="your_email@gmail.com"
>>
>> # File to store the old IP address
>> IP_FILE="/home/fd/business/scripts/old_ip.txt"
>>
>> # Read the old IP address from the file
>> OLD_IP=$(cat "$IP_FILE")
>>
>> # Function to retrieve the IP address
>> get_ip_address() {
>>       local ip_address
>>       ip_address=$(curl -s https://ifconfig.me/ip)
>>       echo "$ip_address"
>> }
>>
>> # Query the API to get the current IP address
>> NEW_IP=$(curl -s https://api.ipify.org)
> 
> I found this site very slow to respond. It could be timing out with an
> empty result. OTOH the URL used in the get_ip_address function is rapid,
> but you don't call that function.
> 
>>
>> sleep 2

Yes, I notice it, it take upwards 10sec to get an IP and sometimes I get a timeout, I increases "sleep 15" hopefully it will help.

>>
>> # Compare the new IP address with the old one
>> if [[ "$NEW_IP" != "$OLD_IP" ]]; then
>>       echo "Your IP address has changed to $NEW_IP"
>>
>>       # Send an email notification
>>       echo "New IP address: $NEW_IP" | mailto -s "IP address change"
>> $EMAIL
>>
>>       # Allow overwriting of the file
>>       set +o noclobber
>>
>>       # Update the old IP address in the file
>> #     printf "%s" "$NEW_IP" > "$IP_FILE"
>>        echo -n "$NEW_IP" > "$IP_FILE"
>>
>>       # Restore the noclobber option
>>       set -o noclobber
> 
> You don't need to play with the noclobber option, just use >| for
> redirection, which will always overwrite the file.
> 

Thanks for the pointer.
>>
>> else
>>       echo "Your IP address is still $OLD_IP"
>> fi


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [gentoo-user] ip_change_notifier - empty IP address
  2023-06-27 17:48   ` thelma
@ 2023-06-27 19:23     ` Neil Bothwick
  2023-06-28  0:37       ` thelma
  2023-06-28  0:49       ` thelma
  0 siblings, 2 replies; 10+ messages in thread
From: Neil Bothwick @ 2023-06-27 19:23 UTC (permalink / raw
  To: gentoo-user

[-- Attachment #1: Type: text/plain, Size: 1741 bytes --]

On Tue, 27 Jun 2023 11:48:16 -0600, thelma@sys-concept.com wrote:

> On 6/27/23 11:04, Neil Bothwick wrote:
> > On Tue, 27 Jun 2023 10:34:22 -0600, thelma@sys-concept.com wrote:
> >   
> >> I run this little script to notify me when the IP changes.  However
> >> at time to time the file: "old_ip.txt" is being populated with an
> >> empty line "no IP address"
> >>
> >> What could be causing it?
> >>
> >>
> >> #!/bin/bash
> >>
> >> # Replace "YOUR_EMAIL" with your actual email address
> >> EMAIL="your_email@gmail.com"
> >>
> >> # File to store the old IP address
> >> IP_FILE="/home/fd/business/scripts/old_ip.txt"
> >>
> >> # Read the old IP address from the file
> >> OLD_IP=$(cat "$IP_FILE")
> >>
> >> # Function to retrieve the IP address
> >> get_ip_address() {
> >>       local ip_address
> >>       ip_address=$(curl -s https://ifconfig.me/ip)
> >>       echo "$ip_address"
> >> }
> >>
> >> # Query the API to get the current IP address
> >> NEW_IP=$(curl -s https://api.ipify.org)  
> > 
> > I found this site very slow to respond. It could be timing out with an
> > empty result. OTOH the URL used in the get_ip_address function is
> > rapid, but you don't call that function.
> >   
> >>
> >> sleep 2  
> 
> Yes, I notice it, it take upwards 10sec to get an IP and sometimes I
> get a timeout, I increases "sleep 15" hopefully it will help.

It won't because the curl command is running synchronously. All
increasing the sleep does is make you wait longer after it fails. Why not
simply switch to the other service that seems to work more reliably?


-- 
Neil Bothwick

If someone with multiple personalities threatens to kill himself, is it
considered a hostage situation?

[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [gentoo-user] ip_change_notifier - empty IP address
  2023-06-27 19:23     ` Neil Bothwick
@ 2023-06-28  0:37       ` thelma
  2023-06-28  0:49       ` thelma
  1 sibling, 0 replies; 10+ messages in thread
From: thelma @ 2023-06-28  0:37 UTC (permalink / raw
  To: gentoo-user

On 6/27/23 13:23, Neil Bothwick wrote:
> On Tue, 27 Jun 2023 11:48:16 -0600, thelma@sys-concept.com wrote:
> 
>> On 6/27/23 11:04, Neil Bothwick wrote:
>>> On Tue, 27 Jun 2023 10:34:22 -0600, thelma@sys-concept.com wrote:
>>>    
>>>> I run this little script to notify me when the IP changes.  However
>>>> at time to time the file: "old_ip.txt" is being populated with an
>>>> empty line "no IP address"
>>>>
>>>> What could be causing it?
>>>>
>>>>
>>>> #!/bin/bash
>>>>
>>>> # Replace "YOUR_EMAIL" with your actual email address
>>>> EMAIL="your_email@gmail.com"
>>>>
>>>> # File to store the old IP address
>>>> IP_FILE="/home/fd/business/scripts/old_ip.txt"
>>>>
>>>> # Read the old IP address from the file
>>>> OLD_IP=$(cat "$IP_FILE")
>>>>
>>>> # Function to retrieve the IP address
>>>> get_ip_address() {
>>>>        local ip_address
>>>>        ip_address=$(curl -s https://ifconfig.me/ip)
>>>>        echo "$ip_address"
>>>> }
>>>>
>>>> # Query the API to get the current IP address
>>>> NEW_IP=$(curl -s https://api.ipify.org)
>>>
>>> I found this site very slow to respond. It could be timing out with an
>>> empty result. OTOH the URL used in the get_ip_address function is
>>> rapid, but you don't call that function.
>>>    
>>>>
>>>> sleep 2
>>
>> Yes, I notice it, it take upwards 10sec to get an IP and sometimes I
>> get a timeout, I increases "sleep 15" hopefully it will help.
> 
> It won't because the curl command is running synchronously. All
> increasing the sleep does is make you wait longer after it fails. Why not
> simply switch to the other service that seems to work more reliably?

Hmm,... do you mean i run:
NEW_IP=$(curl -s https://ifconfig.me/ip)

I tried it.  When the "curl -s https://ifconfig.me/ip" failed, showing empty IP
curl -s https://ifconfig.me/ip  was empty as well.

  


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [gentoo-user] ip_change_notifier - empty IP address
  2023-06-27 19:23     ` Neil Bothwick
  2023-06-28  0:37       ` thelma
@ 2023-06-28  0:49       ` thelma
  2023-06-28  7:05         ` Neil Bothwick
  1 sibling, 1 reply; 10+ messages in thread
From: thelma @ 2023-06-28  0:49 UTC (permalink / raw
  To: gentoo-user



Thelma

On 6/27/23 13:23, Neil Bothwick wrote:
> On Tue, 27 Jun 2023 11:48:16 -0600, thelma@sys-concept.com wrote:
> 
>> On 6/27/23 11:04, Neil Bothwick wrote:
>>> On Tue, 27 Jun 2023 10:34:22 -0600, thelma@sys-concept.com wrote:
>>>    
>>>> I run this little script to notify me when the IP changes.  However
>>>> at time to time the file: "old_ip.txt" is being populated with an
>>>> empty line "no IP address"
>>>>
>>>> What could be causing it?
>>>>
>>>>
>>>> #!/bin/bash
>>>>
>>>> # Replace "YOUR_EMAIL" with your actual email address
>>>> EMAIL="your_email@gmail.com"
>>>>
>>>> # File to store the old IP address
>>>> IP_FILE="/home/fd/business/scripts/old_ip.txt"
>>>>
>>>> # Read the old IP address from the file
>>>> OLD_IP=$(cat "$IP_FILE")
>>>>
>>>> # Function to retrieve the IP address
>>>> get_ip_address() {
>>>>        local ip_address
>>>>        ip_address=$(curl -s https://ifconfig.me/ip)
>>>>        echo "$ip_address"
>>>> }
>>>>
>>>> # Query the API to get the current IP address
>>>> NEW_IP=$(curl -s https://api.ipify.org)
>>>
>>> I found this site very slow to respond. It could be timing out with an
>>> empty result. OTOH the URL used in the get_ip_address function is
>>> rapid, but you don't call that function.
>>>    
>>>>
>>>> sleep 2
>>
>> Yes, I notice it, it take upwards 10sec to get an IP and sometimes I
>> get a timeout, I increases "sleep 15" hopefully it will help.
> 
> It won't because the curl command is running synchronously. All
> increasing the sleep does is make you wait longer after it fails. Why not
> simply switch to the other service that seems to work more reliably?

I think this is better version:
#!/bin/bash

# Replace "YOUR_EMAIL" with your actual email address
EMAIL="your_email@gmail.com"

# File to store the old IP address
IP_FILE="/home/fd/business/scripts/old_ip.txt"

# Read the old IP address from the file
OLD_IP=$(cat "$IP_FILE")

# Function to retrieve the IP address
get_ip_address() {
     local ip_address
     ip_address=$(wget -qO- ipinfo.io/ip)
     echo "$ip_address"
}

# Query the API to get the current IP address
NEW_IP=$(get_ip_address)

# Retry if the IP address is empty
retry_count=0
while [[ -z "$NEW_IP" && $retry_count -lt 3 ]]; do
     sleep 1
     NEW_IP=$(get_ip_address)
     ((retry_count++))
done

# Compare the new IP address with the old one
if [[ "$NEW_IP" != "$OLD_IP" ]]; then
     echo "Your IP address has changed to $NEW_IP"

     # Send an email notification
     echo "New IP address: $NEW_IP" | mailto -s "IP address change" $EMAIL

     # Update the old IP address in the file
     echo -n "$NEW_IP" >| "$IP_FILE"
else
     echo "Your IP address is still $OLD_IP"
fi



^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [gentoo-user] ip_change_notifier - empty IP address
  2023-06-28  0:49       ` thelma
@ 2023-06-28  7:05         ` Neil Bothwick
  2023-06-28  7:23           ` Paul Colquhoun
  0 siblings, 1 reply; 10+ messages in thread
From: Neil Bothwick @ 2023-06-28  7:05 UTC (permalink / raw
  To: gentoo-user

[-- Attachment #1: Type: text/plain, Size: 589 bytes --]

On Tue, 27 Jun 2023 18:49:55 -0600, thelma@sys-concept.com wrote:

> # Query the API to get the current IP address
> NEW_IP=$(get_ip_address)
> 
> # Retry if the IP address is empty
> retry_count=0
> while [[ -z "$NEW_IP" && $retry_count -lt 3 ]]; do
>      sleep 1
>      NEW_IP=$(get_ip_address)
>      ((retry_count++))
> done

That's better, although I would use a longer sleep to allow for network
issues.


-- 
Neil Bothwick

Documentation: (n.) a novel sold with software, designed to entertain the
               operator during episodes of bugs or glitches.

[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [gentoo-user] ip_change_notifier - empty IP address
  2023-06-28  7:05         ` Neil Bothwick
@ 2023-06-28  7:23           ` Paul Colquhoun
  2023-06-28 15:04             ` thelma
  0 siblings, 1 reply; 10+ messages in thread
From: Paul Colquhoun @ 2023-06-28  7:23 UTC (permalink / raw
  To: gentoo-user

On Wednesday, June 28, 2023 5:05:25 P.M. AEST Neil Bothwick wrote:
> On Tue, 27 Jun 2023 18:49:55 -0600, thelma@sys-concept.com wrote:
> > # Query the API to get the current IP address
> > NEW_IP=$(get_ip_address)
> > 
> > # Retry if the IP address is empty
> > retry_count=0
> > while [[ -z "$NEW_IP" && $retry_count -lt 3 ]]; do
> > 
> >      sleep 1
> >      NEW_IP=$(get_ip_address)
> >      ((retry_count++))
> > 
> > done
> 
> That's better, although I would use a longer sleep to allow for network
> issues.

# Retry if the IP address is empty
retry_count=0
while [[ -z "$NEW_IP" && $retry_count -lt 3 ]]; do

     ((retry_count++))
     sleep $retry_count
     NEW_IP=$(get_ip_address)

done


-- 
Reverend Paul Colquhoun, ULC.     http://andor.dropbear.id.au/
  Asking for technical help in newsgroups?  Read this first:
     http://catb.org/~esr/faqs/smart-questions.html#intro





^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [gentoo-user] ip_change_notifier - empty IP address
  2023-06-27 16:34 [gentoo-user] ip_change_notifier - empty IP address thelma
  2023-06-27 17:04 ` Neil Bothwick
@ 2023-06-28 14:37 ` Matt Connell
  1 sibling, 0 replies; 10+ messages in thread
From: Matt Connell @ 2023-06-28 14:37 UTC (permalink / raw
  To: gentoo-user

Not quoting anything because I'm just making a general reply, to the
general problem that you're generally trying to solve.  Generally.

You really should be using DNS for what you're trying to do, one way or
the other.  Reverse DNS, when set up properly, will always return the
appropriate IP address for you.  If this isn't achievable (I might
scoff at a few dollars a year for a domain but others might not), then
you should still be using DNS as a protocol, for both speed and
reliability reasons.  OpenDNS provides this a free service.

dig +short myip.opendns.com @resolver1.opendns.com

If you still don't want to use DNS for whatever reason, there are
services that are designed for infrastructural reliability for this,
namely:

icanhazip.com [1]

Since 2021 icanhazip is hosted by Cloudflare, and regardless of how you
feel about CF as a company, the service is designed for speed and
reliabilty for this exact purpose.

1: https://major.io/p/a-new-future-for-icanhazip/


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [gentoo-user] ip_change_notifier - empty IP address
  2023-06-28  7:23           ` Paul Colquhoun
@ 2023-06-28 15:04             ` thelma
  0 siblings, 0 replies; 10+ messages in thread
From: thelma @ 2023-06-28 15:04 UTC (permalink / raw
  To: gentoo-user

On 6/28/23 01:23, Paul Colquhoun wrote:
> On Wednesday, June 28, 2023 5:05:25 P.M. AEST Neil Bothwick wrote:
>> On Tue, 27 Jun 2023 18:49:55 -0600, thelma@sys-concept.com wrote:
>>> # Query the API to get the current IP address
>>> NEW_IP=$(get_ip_address)
>>>
>>> # Retry if the IP address is empty
>>> retry_count=0
>>> while [[ -z "$NEW_IP" && $retry_count -lt 3 ]]; do
>>>
>>>       sleep 1
>>>       NEW_IP=$(get_ip_address)
>>>       ((retry_count++))
>>>
>>> done
>>
>> That's better, although I would use a longer sleep to allow for network
>> issues.
> 
> # Retry if the IP address is empty
> retry_count=0
> while [[ -z "$NEW_IP" && $retry_count -lt 3 ]]; do
> 
>       ((retry_count++))
>       sleep $retry_count
>       NEW_IP=$(get_ip_address)
> 
> done

Thank you, no more empty IP in txt file.



^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2023-06-28 15:04 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-06-27 16:34 [gentoo-user] ip_change_notifier - empty IP address thelma
2023-06-27 17:04 ` Neil Bothwick
2023-06-27 17:48   ` thelma
2023-06-27 19:23     ` Neil Bothwick
2023-06-28  0:37       ` thelma
2023-06-28  0:49       ` thelma
2023-06-28  7:05         ` Neil Bothwick
2023-06-28  7:23           ` Paul Colquhoun
2023-06-28 15:04             ` thelma
2023-06-28 14:37 ` Matt Connell

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox