Hello my friend,
Just recently we have started discussion about the tools about the performance troubleshooting in networks. One of the questions we were asked afterwards was, how to programatically get your public IP? Well, that is interesting one.
1
2
3
4
5 No part of this blogpost could be reproduced, stored in a
retrieval system, or transmitted in any form or by any
means, electronic, mechanical or photocopying, recording,
or otherwise, for commercial purposes without the
prior permission of the author.
Can automation help with performance troubleshooting?
Yes, and that was a question we’ve started this blogpost with is about that. How can we rely the automation tools by troubleshooting? How can we get our IP to make any diagnostics? You will see some things right in this blogpost
Our network automation training has two faces: either live or self-paced. So you can choose yourself, what works better for you. On our side, we guide you from the foundation of the automation for the small networks till advanced automation use cases in big data centres, service providers, and clouds. You will lean how to structure the data using YANG modules, how to serialise it using JSON, XML, Protocol Buffers (Protobuf) depending on the application requirements and how to manage the fleet of your IT and network workloads relying gRPC, gNMI, NETCONF, REST API and many others with Bash, Ansible and Python. In addition to that you will master the solid foundation in Linux administration, Linux networking, network function virtualisation (NFV) with KVM and containerisation with Docker. That is all you need to be successful in network automation in one place.
The task definition
As said earlier, the major task is to get the public IP of the host. The one could say, that this is a pretty easy task: just take a look on the egress network interfaces and that’s it. In reality, the problem could be slightly more complicated. In many cases the customers, especially if we talk about the home or enterprise offices are locate, besides multiple firewalls, which are also doing NAT service. In such a scenario, you can’t just take a look on your interface. So you need somehow to access some public services and get the response with your public IP.
Solution description
There are numerous public service, which tells you your public IP, if go there via browser. There are slightly less, which allows you to collect the information in the programatic way. One of the resources, which we find quite useful is https://api.myip.com.
We don’t have any relations to this resource and don’t promote it by any mean. But we use it ourself in many occasions. Thanks for your job, guys.
How it works:
- You mare REST API get request towards that URL
- You get back your IP and your county
As this is REST API request. It is very easy to integrate it in the any program. Let’s take a closer look into implementation steps.
#1. Implementation Bash
Let’s start with Bash. Why? Because it is the easiest way to perform the REST API request and get the response. You can do it directly from you CLI from your Linux or MAC operating system:
1
2 $ curl -X GET https://api.myip.com
{"ip":"86.167.112.27","country":"United Kingdom","cc":"GB"}
The curl tool is part of any standard Linux distribution (probably only Alpine Linux doesn’t have it by default). So what is happening here:
- -X key is used to specify the request type, which is this case is GET.
- The URL https://api.myip.com is a resource the curl is executed against.
There are specific libraries, which allows you in Bash to parse JSON response. Come to our training to learn more about them.
#2. Implementation Ansible
Some time ago, we have shared with you how to convert your playbooks from Ansible 2.9 to 2.10, as there are a lot of changes, you need to be aware of. The playbook to implement that use case will look like as follows:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 $ cat get_my_ip.yml
---
- name: PUBLIC IP IDENTIFICATION
hosts: localhost
gather_facts: no
vars:
url: https://api.myip.com
tasks:
- name: COLLECT IP
ansible.builtin.uri:
url: "{{ url }}"
method: GET
return_content: yes
register: api_response
- name: VALIDATE IP
ansible.builtin.debug:
msg: "{{ api_response.json }}"
...
They playbook is created following Ansible 2.10 syntax, so you see the full namespace for modules. In details:
- You make an REST API request using uri module from ansible.builtin namespace and save the response in the api_response variable.
- This variable contains a part called json, which contains parsed response from the host. You can use the IP as api_response.json.ip and country as api_response.json.country further in your code.
Here is the execution example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 $ ansible-playbook get_my_ip.yml
PLAY [PUBLIC IP IDENTIFICATION] ***********************************************************************************************************************************************************
TASK [COLLECT IP] *************************************************************************************************************************************************************************
ok: [localhost]
TASK [VALIDATE IP] ************************************************************************************************************************************************************************
ok: [localhost] => {
"changed": false,
"msg": {
"cc": "GB",
"country": "United Kingdom",
"ip": "86.167.112.27"
}
}
PLAY RECAP ********************************************************************************************************************************************************************************
localhost : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
In our network Automation training we covers Ansible in-depth from basics till advanced level. Join us to learn how to automate networks and IT with Ansible.
#3. Implemenation Python
If prefer Python over Ansible and Bash, we can understand you. Python has a huge variety of the modules, which allows you to make the REST API requests. One of the most populars is requests. Despite it is not part of the standard Python distribution, you can use install as:
1 $ pip install requests
Once installed, your Python code will be very easy:
1
2
3
4
5
6
7
8
9
10
11
12 $ cat get_my_ip.py
#!/usr/bin/env python
# Modules
import requests
# Variables
target_url = "https://api.myip.com"
# Body
response = requests.get(target_url)
print(response.json())
Once the library is imported, you can use any its artefact, including the whole range of the REST API calls. The GET requires only a single argument – url it needs to access, which can be provided as either positional argument (as in the example) or as keyword (in that case it would be url=target_url). Following the same approach on the JSON body you have seen in Ansible 2.10, here you would have to use method .json() to decode the variables and be able to use the.
1
2 $ python get_my_ip.py
{'ip': '86.167.112.27', 'country': 'United Kingdom', 'cc': 'GB'}
As you can see, the result of the execution is a Python dictionary, so you can work with any of this variable as you need.
Python is one of the core topics in our network automation training, where you will learn how to apply it in many various situations and protocols.
Lessons learned
The automation is not a magic wand, but it is a very helpful toolkit. Knowing how to apply it will save a lot of time and efforts by doing the troubleshooting of network-related issues.
Conclusion
Knowing your IP is a first step in many troubleshooting flows, such addressing your ISP with request about bad connectivity between the endpoints and so on. It is also important often from the operational perspectives, where you can’t access some resources, which are protected by access-lists or other firewall means. Take care and good bye.
Support us
P.S.
If you have further questions or you need help with your networks, I’m happy to assist you, just send me message. Also don’t forget to share the article on your social media, if you like it.
BR,
Anton Karneliuk