To answer the question that's asked in the title: yes!
Charms written in Python
charmhelpers library includes the functionality that you're after:
from charmhelpers.core.hookenv import opened_ports
opened_ports() will provide you with a list of port ranges and protocols that are open:
['80/tcp', '8081-8083/tcp', 'icmp']
icmp protocol is special, as there are no port numbers.
If you wish to do this outside of Python, your script should execute a shell command:
You'll need to interpret its output from within your hook handler. So, what does
open-ports command actually provide? You can experiment by logging in to the unit with secure shell and running the command yourself.
First we set up the default model with the
$ juju bootstrap localhost c-testing
$ juju deploy ubuntu
Now we can ask Juju to grant us direct access to the machine running the
ubuntu charm in the context of the
$ juju run --unit ubuntu/0 -- opened-ports --help
Usage: opened-ports [options]
lists all ports or ranges opened by the unit
--format (= smart)
Specify output format (json|smart|yaml)
-o, --output (= "")
Specify an output file
Each list entry has format <port>/<protocol> (e.g. "80/tcp") or
<from>-<to>/<protocol> (e.g. "8080-8088/udp").
Just to confirm, we can run
opened-ports from the context of the unit that we care about in the cloud, directly from the laptop that we're sitting at:
$ juju run --unit ubuntu/0 -- opened-ports --format=smart
Or if you prefer machine-readable output:
$ juju run --unit ubuntu/0 -- opened-ports --format=json
Can we check open ports of a parent charm from its subordinate?
Not without some pain... I will try to flesh out an answer once I've figured something that's relatively simple.