This article is about [Writing Python scripts to run from the OCI Console Cloud Shell](https://medium.com/oracledevs/writing-python-scripts-to-run-from-the-oci-console-cloud-shell- This is the execution of the contents of a0be1091384c).
OCI Cloud Shell OCI Cloud Shell is a web browser-based Shell terminal accessible from the OCI web console. Cloud Shell has the following features:
--Pre-authenticated OCI CLI --No configuration required to start using CLI in Cloud Shell --A complete Linux shell with key developer tools to interact with Oracle Cloud Infrastructure services and a pre-installed language runtime --OCI SDK and Oracle Instant Client for each language --5 GB storage for home directory --You can save your work between Cloud Shell sessions --Console persistent frames that remain active when navigating to different pages of the console
list_ipaddress.py
#!/usr/bin/env python
import oci
delegation_token = open('/etc/oci/delegation_token', 'r').read()
signer = oci.auth.signers.InstancePrincipalsDelegationTokenSigner(
delegation_token=delegation_token
)
search_client = oci.resource_search.ResourceSearchClient({},signer=signer)
compute_client = oci.core.ComputeClient({},signer=signer)
network_client = oci.core.VirtualNetworkClient({},signer=signer)
resp = search_client.search_resources(
oci.resource_search.models.StructuredSearchDetails(
type="Structured",
query="query instance resources"
)
)
for instance in resp.data.items:
resp = compute_client.list_vnic_attachments(
compartment_id=instance.compartment_id,
instance_id=instance.identifier
)
for vnic_attachment in resp.data:
vnic = network_client.get_vnic(vnic_attachment.vnic_id).data
print("\t".join([
instance.display_name,
vnic.display_name,
vnic.private_ip,
vnic.public_ip
]))
$ python ./list_ipaddress.py
fmwf fmwf 10.0.0.4 150.136.236.XXX
db01 db01 10.0.0.3 150.136.37.XXX
gpu01 gpu01 10.0.0.5 150.136.89.XXX
fn fn 10.0.0.2 158.101.118.XXX
singer When writing a Python script that runs inside the Cloud Shell, you can use the existing authentication of the Cloud Shell. You can omit the separate API key settings required to run the script. Load an existing instance principal delegation token and generate a signer.
#!/usr/bin/env python
import oci
delegation_token = open('/etc/oci/delegation_token', 'r').read()
signer = oci.auth.signers.InstancePrincipalsDelegationTokenSigner(
delegation_token=delegation_token
)
Search for all instances in the current region Inquire about instance details and get an IP address API client definition
search_client = oci.resource_search.ResourceSearchClient({},signer=signer)
compute_client = oci.core.ComputeClient({},signer=signer)
network_client = oci.core.VirtualNetworkClient({},signer=signer)
Find all attached vnics for each instance Get both public and private IP addresses for each vnic
for instance in resp.data.items:
resp = compute_client.list_vnic_attachments(
compartment_id=instance.compartment_id,
instance_id=instance.identifier
)
for vnic_attachment in resp.data:
vnic = network_client.get_vnic(vnic_attachment.vnic_id).data
print("\t".join([
instance.display_name,
vnic.display_name,
vnic.private_ip,
vnic.public_ip
]))
Recommended Posts