LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-28-2018, 02:51 PM   #16
individual
Member
 
Registered: Jul 2018
Posts: 315
Blog Entries: 1

Rep: Reputation: 233Reputation: 233Reputation: 233

Please see my latest update. I fixed a corner case where there is only the country name available.
 
Old 09-28-2018, 03:08 PM   #17
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,735

Rep: Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212
Quote:
Originally Posted by pedropt View Post
Ok Scasey , here it is another example .
Code:
whois 139.99.118.122 | grep -iE ^country | awk {'print$2'}
hope you understood what i mean with whois tool
Interesting. OVH is a Canadian company, of course, so the CA refrence on the 139.99.0.0/16 block makes sense.
The 139.99.0.0/17 block and the 139.99.118.64/26 blocks are OVH Singapore. Presumably other 139.99. IPs are elsewhere, but it would take multiple queries to figure that out.

For your purposes, I'd think that SG is all you'd need to know, and you'd block the /26 block to start.

Note that the abuse reporting addresses are all the same...which is all I usually care about.

This particular anomaly is probably caused by having SG addresses in the ARIN RIR
 
Old 09-29-2018, 02:01 AM   #18
pedropt
Member
 
Registered: Aug 2014
Distribution: Devuan
Posts: 345

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by individual View Post
Please see my latest update. I fixed a corner case where there is only the country name available.
Thank you , i will check it out .

Quote:
Interesting. OVH is a Canadian company, of course, so the CA refrence on the 139.99.0.0/16 block makes sense.
The 139.99.0.0/17 block and the 139.99.118.64/26 blocks are OVH Singapore. Presumably other 139.99. IPs are elsewhere, but it would take multiple queries to figure that out.

For your purposes, I'd think that SG is all you'd need to know, and you'd block the /26 block to start.

Note that the abuse reporting addresses are all the same...which is all I usually care about.

This particular anomaly is probably caused by having SG addresses in the ARIN RIR
Yesterday 02:51 PM
One of the solutions for me on whois tool for this kind of results where to validate only the first result and ignore the other 2 , but on different ips i notice that sometimes the real country came as a 2nd variable from whois and not as the 1st .

Worst than that is when i get EU (Europe) as result from whois and the real country is in US or anywhere else .
Of course if i wanted the abuse contacts only then whois tool was perfect for the job as you told before , but in my case i only need the country , and in this case the country letter code .
Maybe using the code from "Individual i will post here the full code with the country list hosted on an external server somewhere , ex : github , for those who need it , and the script code will pick up the country list in github and display the full country name in the script when it is not available from dnslytics .
 
Old 09-29-2018, 03:02 AM   #19
pedropt
Member
 
Registered: Aug 2014
Distribution: Devuan
Posts: 345

Original Poster
Rep: Reputation: Disabled
Ok guys , here it is the code on github .
Feel free to give suggestions or optimize it by sending pull requests if you think you need .

https://github.com/peterpt/ipcountry

The script uses a file to retrieve full country name witch is in the git also .
 
1 members found this post helpful.
Old 09-29-2018, 09:53 AM   #20
individual
Member
 
Registered: Jul 2018
Posts: 315
Blog Entries: 1

Rep: Reputation: 233Reputation: 233Reputation: 233
Quote:
Originally Posted by pedropt View Post
Ok guys , here it is the code on github .
Feel free to give suggestions or optimize it by sending pull requests if you think you need .

https://github.com/peterpt/ipcountry

The script uses a file to retrieve full country name witch is in the git also .
Hey, thank you for giving me credit! If I could, I have a few suggestions:
1. Instead of using nc to check for a network connection, why not use ping?
Code:
ping -c1 [HOSTNAME] &>/dev/null
2. You should put your usage banner in its own function, that way you aren't duplicating code.
Code:
function usage {
    echo "Ipcountry"
    echo "-------------------------------------------------"
    echo "Switches :"
    echo "$0 172.217.168.174 (to retrieve country from IP)"
    echo "$0 -h (This help)"
    echo "-------------------------------------------------"
    echo "Thanks to (Individual) LQ for the help" 
    echo "-------------------------------------------------" 
    echo "This Scripts uses Dnslytics.com\n"
    exit 1
}
3. You can simplify your country name lookup.
Code:
cntname=$(grep -E "^ $country" < "$cntlist" | tail -c+7)
 
Old 09-29-2018, 10:27 AM   #21
pedropt
Member
 
Registered: Aug 2014
Distribution: Devuan
Posts: 345

Original Poster
Rep: Reputation: Disabled
ping in some computers dont work well because uses an echo request to google , some computers have problem with that and i believe that is because of some firewall rules on those computers , i have notice that very often in the past in other github projects i have collaborated .
Instead ping i used netcat to connect directly to port 80 using tcp protocol witch is 100% reliable .

Yup , you are right in the function used to display the help , i did the script very fast and i did not realize that option to use .
Usually on my other scripts when i have plenty of time to look into the code and optimize it i do that , but this one was made in 10 minutes and i just uploaded it to github .
I will do that as soon as i get sometime here because right now i have some issues in my cloud to deal and my web server is right now under fully attack .
I will change the code during next week , or if you want just create a github account , clone ipcountry git , make your changes in your code and send the pull request to me that i will accept it .
Maybe if you have any other ideas for the script then feel free to add them at your will , since basically you did the hardest part of the script witch i would problably wont get there easily .
 
Old 09-29-2018, 10:27 AM   #22
pedropt
Member
 
Registered: Aug 2014
Distribution: Devuan
Posts: 345

Original Poster
Rep: Reputation: Disabled
ping in some computers dont work well because uses an echo request to google , some computers have problem with that and i believe that is because of some firewall rules on those computers , i have notice that very often in the past in other github projects i have collaborated .
Instead ping i used netcat to connect directly to port 80 using tcp protocol witch is 100% reliable .

Yup , you are right in the function used to display the help , i did the script very fast and i did not realize that option to use .
Usually on my other scripts when i have plenty of time to look into the code and optimize it i do that , but this one was made in 10 minutes and i just uploaded it to github .
I will do that as soon as i get sometime here because right now i have some issues in my cloud to deal and my web server is right now under fully attack .
I will change the code during next week , or if you want just create a github account , clone ipcountry git , make your changes in your code and send the pull request to me that i will accept it .
Maybe if you have any other ideas for the script then feel free to add them at your will , since basically you did the hardest part of the script witch i would problably wont get there easily .
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] retrieve html output in bash StorageDon Linux - Newbie 4 11-28-2016 01:06 PM
How to retrieve the data lost in installation? BryanWalters Linux - General 5 05-14-2016 06:19 AM
how to retrieve some specific set of lines from a file and store it in a char buffer. vigneshinbox Programming 3 04-02-2009 01:16 AM
Best way to retrieve data baldurpet Linux - Software 3 12-25-2008 12:48 AM
Very urgent! Need to retrieve some data! Help bikov_k General 2 10-16-2004 06:51 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:39 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration