In this API example, we’ll look at how to pull out useful information about your servers. I’d like to get a list of all IP addresses used by my cloud servers, so let’s take a look at how to do that.
We’ve put together a few library functions that make that easier, so please install the api-lib library V0.19 or higher (install script), in /usr/local/bin/ . As with all commands using the library, make sure you run the following two commands first:
. /usr/local/bin/api-lib resty 'https://portal.cloudpassage.com/api/1*'
The first command loads a library of support tools into your shell. The second command tells the support tools to always put the “portal.cloudpassage.com” URL in front of any API calls.
First, go to the portal and get the name of a group with some servers; we’ll use “Test group” in this example. The API is case-sensitive for things like server and server groups.
Let’s start with just a list of the servers. We’ll use the ActiveHostnamesInGroup function from the library you just loaded:
Now, let’s grab all their IP addresses, one line per server:
$ ListServerIPs `ActiveHostnamesInGroup 'Test group'` 188.8.131.52 10.183.34.131 184.108.40.206 220.127.116.11 10.183.36.179 18.104.22.168 22.214.171.124 10.183.34.122 126.96.36.199 188.8.131.52 10.183.41.73 184.108.40.206
That’s a start, but it’s a little hard to match up with the particular host. If you want the hostname as the first parameter on the line, add “–show-host”:
$ ListServerIPs --show-host `ActiveHostnamesInGroup 'Test group'` cloud-test6 220.127.116.11 10.183.34.131 18.104.22.168 cloud-test7 22.214.171.124 10.183.36.179 126.96.36.199 cloud-test5 188.8.131.52 10.183.34.122 184.108.40.206 cloud-test2 220.127.116.11 10.183.41.73 18.104.22.168
Finally, if you want to see the interface names too, add “–show-interface”.
$ ListServerIPs --show-host --show-interface `ActiveHostnamesInGroup 'Test group'` cloud-test6 Connecting_IP_Address/22.214.171.124 eth1/10.183.34.131 eth0/126.96.36.199 cloud-test7 Connecting_IP_Address/188.8.131.52 eth1/10.183.36.179 eth0/184.108.40.206 cloud-test5 Connecting_IP_Address/220.127.116.11 eth1/10.183.34.122 eth0/18.104.22.168 cloud-test2 Connecting_IP_Address/22.214.171.124 eth1/10.183.41.73 eth0/126.96.36.199
This shows you the particular IP address for each interface, and also shows why we are seeing duplicate IP addresses. The first IP address is always the IP address that’s used for outgoing connections. If your cloud server is given a private address that starts with 10., 192.168., or 172.16.-172.31., this isn’t the IP address that is used for real-world communication. The IP used for real-world communication is the first IP on the list.
Finally, if you only want details on specific systems, you can put those right on the command line instead of asking for an entire group:
ListServerIPs --show-interface cloud-test6 'Test group' Connecting_IP_Address/188.8.131.52 eth1/10.183.34.131 eth0/184.108.40.206
Many thanks to Apurva for his help in setting this up.
Cheers! — Bill