I made a tiny little bash script, that prints the current IP Address to the command line. I checked the script with ShellCheck (0.4.6-1) on a local debian install and I get the following message:
SC2026: This word is outside of quotes. Did you intend to 'nest '"'single quotes'"' instead'?
What is wrong with the way I have been line-breaking the pipe, or what is proper practice?
#!/bin/bash
curl -s POST whatismyip.org\
| grep -A1 'Your IP Address'\
| awk -F '>' '{print 2ドル}'\
| sed 's/<\/h2//g'\
| sed 's/<\/span//g'
-
\$\begingroup\$ Interesting... I tried copying and pasting the code into ShellCheck and it reports "No issues detected!"... \$\endgroup\$Sᴀᴍ Onᴇᴌᴀ– Sᴀᴍ Onᴇᴌᴀ ♦2017年07月19日 20:47:29 +00:00Commented Jul 19, 2017 at 20:47
-
\$\begingroup\$ ok, sorry the link in the question was confusing. I have been using shellcheck 0.4.6-1 on a local debian install. (I'll edit my question to make that clear). But if the online version does not detect any issues, it might be rather a ShellCheck thing. \$\endgroup\$nath– nath2017年07月19日 20:58:26 +00:00Commented Jul 19, 2017 at 20:58
-
3\$\begingroup\$ #923. It was fixed in June. \$\endgroup\$Zeta– Zeta2017年07月19日 21:05:20 +00:00Commented Jul 19, 2017 at 21:05
-
\$\begingroup\$ Thanks @Zeta, so I need to wait for an upgrade for this annoying messages to disappear, which is much better then editing all my bash-goodies :-) \$\endgroup\$nath– nath2017年07月19日 21:12:45 +00:00Commented Jul 19, 2017 at 21:12
-
\$\begingroup\$ If you're on a LTS version you probably want to install it via stack, though. \$\endgroup\$Zeta– Zeta2017年07月19日 21:24:14 +00:00Commented Jul 19, 2017 at 21:24
3 Answers 3
Your code is fine, except for the stray method. POST
at that point isn't valid, and you will try to connect to a server called POST
. Just try your curl
call with -Iv
, you will notice two connections:
curl -Iv POST whatismyip.org
If you want to use the method, you would have to write -x POST
, but to just get information, you use -x GET
(which is the default when you don't transfer any data). Also, you should specify the protocol:
curl -s http://whatismyip.org
That being said there are other sites such as ipecho.net
that provide a direct method:
curl -s -L http://ipecho.net/plain; echo
By the way, whatismyip.org seems to be up for sale, so you might not get your IP in another few months, or the format might change. ipecho.net has a strange whois entry too, but at least its mentioned in a highly voted answer and returns your IP without the need to extract it from the HTML.
-
\$\begingroup\$ Much thanks for this feedback. The
curl -s -L http://ipecho.net/plain; echo
is not just simpler, it also is much faster! \$\endgroup\$nath– nath2017年07月19日 21:35:47 +00:00Commented Jul 19, 2017 at 21:35 -
\$\begingroup\$ @nath you're welcome. Make sure to read
man curl
to understand-L
(follows redirection). \$\endgroup\$Zeta– Zeta2017年07月20日 05:00:06 +00:00Commented Jul 20, 2017 at 5:00 -
\$\begingroup\$ You can remove the flags for curl if you use
curl icanhazip.com
\$\endgroup\$alexyorke– alexyorke2017年07月20日 11:55:54 +00:00Commented Jul 20, 2017 at 11:55
I would use a different service that provides output in a format that is good for tools (not humans (html is for displaying for humans)).
Hit this url:
https://ifconfig.co/json
You're output looks like this:
{
"ip": "154.140.296.288",
"ip_decimal": 926648668,
"country": "United States",
"city": "Seattle",
"hostname": "154-140-296-288.Bob.com"
}
Which is obviously json which can be parsed using the jq
tool.
> curl -s https://ifconfig.co/json | jq .ip -r
154.140.296.188
-
\$\begingroup\$ That's not a URL; did you mean
https://ifconfig.co/json
? \$\endgroup\$Toby Speight– Toby Speight2017年08月30日 14:41:50 +00:00Commented Aug 30, 2017 at 14:41
Other answers already drilled down the issue with your approach, and also provided couple of alternative resources to query over HTTP(S).
The approaches with HTTP are not future-proof because the domain might be sold, the specific resource might be taken down or deprecated, or worse -- could be compromised altogether resulting in garbaging out the client endpoint.
You would be better off using DNS for getting your public IP, some (big) providers provide you with your public IP when queried for a certain domain on their nameserver(s).
For example, Opendns provides this feature, and you would get desired result while querying for domain myip.opendns.com
on one of their resolvers (e.g. resolver1.opendns.com
):
% dig +short myip.opendns.com @resolver1.opendns.com
1.2.3.4 ## Obfuscated