How can I do this better?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How can I do this better?
# 1  
Old 07-04-2018
How can I do this better?

Hello,


Still learning over here. I made the below based on what I've learned so far and what I've stolen from others. It updates my dynDNS addresses, both IPv4 and v6. I'm looking to see what I could have done better or how those more experienced than me would have done it. Thanks for your time!
(It wont let me post links cuz I'm still new here so I dropped one of the 't's in the URLs on purpose)



Code:
#! /bin/bash

IP6temp=$(ip -o -6 addr show eno1 | sed -e 's/^.*inet6 \([^ ]\+\).*/\1/' | grep 64 | grep 200) #All of this is to get one globally unique IPv6
#echo $IP6temp #debugging
IP6=$(echo $IP6temp | cut -f1 -d'/') #get rid of the /notation
#echo "IPv6 Address is $IP6"
last_ip_file="/tmp/last_ip"
last_ip=`cat $last_ip_file`
ip=$(curl -s htp://dynamicdns.park-your-domain.com/getip)
#echo "IPV4 is $ip"
if [ "$ip" == "$last_ip" ]; then
        #echo "IP Still same, not need to update."
        exit 0
fi

response=$(curl -s "<HTPS://<UserName>:<API-Key>@members.dyndns.org/v3/update?hostname=<domain>.better-than.tv&myip=$ip,$IP6")
#echo $response
response=$(curl -s "htps://<UserName>:<API-Key>@members.dyndns.org/v3/update?hostname=<other domain>.better-than.tv&myip=$ip")
echo $ip > $last_ip_file

# 2  
Old 07-04-2018
Code:
IP6=$(echo $IP6temp | cut -f1 -d'/') #get rid of the /notation

can be replaced by the much more efficient variable expansion:
Code:
IP6=${IP6temp%%/*} #get rid of the /notation

This User Gave Thanks to Don Cragun For This Post:
# 3  
Old 07-04-2018
Quote:
Originally Posted by Rhysers
Hello,


Still learning over here. I made the below based on what I've learned so far and what I've stolen from others. It updates my dynDNS addresses, both IPv4 and v6. I'm looking to see what I could have done better or how those more experienced than me would have done it. Thanks for your time!
(It wont let me post links cuz I'm still new here so I dropped one of the 't's in the URLs on purpose)
I can't speak for the curls but the following gets rid of unecessary greps, sed and cat.
Code:
#! /bin/bash

IP6=$(ip -6 -o addr show eno1 | awk '$4 ~ /64$/ && $4 ~ /200/ {$0 = $4;} sub("/.*","")') #All of this is to get one globally unique IPv6
#echo "IPv6 Address is $IP6"
last_ip_file="/tmp/last_ip"
last_ip=$( <$last_ip_file )
ip=$(curl -s htp://dynamicdns.park-your-domain.com/getip)
#echo "IPV4 is $ip"
if [ "$ip" == "$last_ip" ]; then
        #echo "IP Still same, not need to update."
        exit 0
fi

response=$(curl -s "<HTPS://<UserName>:<API-Key>@members.dyndns.org/v3/update?hostname=<domain>.better-than.tv&myip=$ip,$IP6")
#echo $response
response=$(curl -s "htps://<UserName>:<API-Key>@members.dyndns.org/v3/update?hostname=<other domain>.better-than.tv&myip=$ip")
echo $ip > $last_ip_file

The only problem I have with my solution is what if the output format of ip changes so that your address is no longer in field 4?

Andrew
# 4  
Old 07-04-2018
Code:
last_ip=`cat $last_ip_file`

is certainly more expensive than
Code:
last_ip=$( <$last_ip_file )

But in the case it is one line, the following works, too:
Code:
read last_ip <$last_ip_file

It would even allow the following two-lines expansion:
Code:
{
read last_ip
read last_ip6
} <$last_ip_file

And writing the two-lines file would be like
Code:
{
echo "$ip"
echo "$IP6"
} >$last_ip_file

or
Code:
echo "\
$ip
$IP6" > $last_ip_file

or
Code:
printf "%s\n" "$ip" "$IP6" > $last_ip_file

Login or Register to Ask a Question

Previous Thread | Next Thread
Login or Register to Ask a Question