5

I have edited my .bashrc to look like this with some help from others on this forum ... Thanks guys for the help on the weather segment of this script...enter image description here

An issue I have ran into is the code for the external ip address. If for some reason the site I use to check the external IP doesn't respond .. it locks up my bash at that point until it gets the information.

What I would like to do is change it so that I can have it so if it doesn't respond in a second or 500ms .. then to just move on so it doesn't hang. I hope that makes sense .. here is the code I'm using for it.

localnet()
{
declare -a INETARRAY
INETARRAY=( `/sbin/ifconfig | awk /'inet addr/ {print $2}' && /sbin/ifconfig | awk /'Bcast/ {print $3}'`)
echo -ne ${INETARRAY[@]} "ExtIP:" ; myip
}
myip ()
{
lynx -dump -hiddenlinks=ignore -nolist http://checkip.dyndns.org:8245/ | grep "Current IP Address" | cut -d":" -f2 | cut -d" " -f2
}

Basically I need an if statement that would do something like this:

if the site doesn't give whats expected withing a configurable amount of time load "line 1" else load "line 2"

Line 1 would contain the code without the external IP request Line 2 would contain the code with the external IP request.

Now .. I'm not actually sure what is causing the hang .. I don't know if the site is not responding .. or if it is busy and taking longer to respond or if its getting information but not whats expected ... it happens sporadically and if it hangs on me I can close the shell and open it and usually it will get the information .. I thought about ping but ... again .. not sure if the site would still respond to the ping but not produce what the script is looking for and still hang.

I just don't know how to set up the statement correctly to make this happen or something similar

Thanks in advanced.

I tried the line suggested by glenn jackman

curl --silent --output - --connect-timeout 2 --max-time 3 http://checkip.dyndns.org:8245/ | grep "Current IP Address" | cut -d":" -f2 | cut -d"" -f2

It did get the IP address but the resulted in this:

xxx.xxx.xxx.xxx</body></html>

I will have to probably adjust the second cut command .. lol .. guess I need to learn cut :D

If i use

curl --silent --output - --connect-timeout 2 --max-time 3 http://checkip.dyndns.org:8245/ | grep "Current IP Address"

I get the output of:

<html><head><title>Current IP Check</title></head><body>Current IP Address: xxx.xxx.xxx.xxx</body></html>

If I switch back to the lynx line instead of curl and use the -read_timeout=1 -connect-timeout=1 this does keep the shell from hanging if it has connections issues but .. it produces errors which was why I was hoping to make some sort of if statement

Network: addr:192.168.1.20 addr:127.0.0.1 Bcast:192.168.1.255 ExtIP:
Looking up checkip.dyndns.org:8245
Making HTTP connection to checkip.dyndns.org:8245
Alert!: Connection failed (too many retries).
Alert!: Unable to connect to remote host.

lynx: Can't access startfile http://checkip.dyndns.org:8245/
  • 1
    I use this: curl --silent --output - --connect-timeout 2 --max-time 3 $URL – glenn jackman May 6 '16 at 22:09
  • Also, be prepared to switch to a different source for this information: third party sites come and go. You can host your own: with PHP, it's a one-liner. – glenn jackman May 6 '16 at 22:11
  • yeah .. this is dyndns .. they have been around forever and probably will stay around for years to come but .. how can I set it up with php .. if you can put it in an answer maybe I can try that instead .. im sure it would probably be must faster and more stable if I could run it on my system .. but wouldn't it just give me my local IP not the external one? – John Orion May 6 '16 at 22:17
  • 1
    If you want to stick with lynx, it has both a -read_timeout and -connect_timeout - try setting those – steeldriver May 6 '16 at 22:34
  • 1
    Couldn't the myip section be this: curl --silent --output - --connect-timeout 2 --max-time 3 icanhazip.com ? – Terrance May 6 '16 at 23:07
3

What seem to be a working solution is using the curl command suggested by glenn jackman and the address suggested by Terrance. I haven't run into the hang yet so I don't know if it will through any errors but it seems to work well.. the new line is now

curl --silent --output - --connect-timeout 1 --max-time 2 http://icanhazip.com/ 

which seems to be working .. I have launched the terminal 10 times without a hang or an error. I think you guys did it again... Man I wish where was something like this forum for Windows... lol you go on their forums and ask a question .. you get a non related 'form letter response' and then nothing.

You guys are great .. I think this has now got the terminal shell just the way I want it and no hanging .. lol I disabled the external IP thing for a while because of the annoyance but now I can have it back again :D

If anyone decides to use the .bashrc I linked lol its slightly plagiarized but I don't remember the original person I got it from when the script first started out but I have been playing with it ever since.. it was like in 2010 or something.

The file also includes a link to .bashrcc that you can create where you can add commands and aliases without having to put them in the main file. This was done because I sync my bashrc across several computers and there are times where I want a special alias for one computer but I don't it available on all the PC's that are synced to the file. The .bashrcc will allow me to add it for one user only. Also remember that you will have to change the weather function with your zip code or you will be viewing my area's temps :D

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.