36 lines
1.5 KiB
Python
36 lines
1.5 KiB
Python
import os
|
|
import sys
|
|
|
|
import paramiko
|
|
|
|
pw = os.environ["DEPLOY_PASSWORD"]
|
|
c = paramiko.SSHClient()
|
|
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
|
c.connect("20.191.152.26", username="wakka", password=pw, timeout=30)
|
|
|
|
script = r"""
|
|
set -e
|
|
TOKEN=$(curl -s -H Metadata:true \
|
|
'http://169.254.169.254/metadata/identity/oauth2/token?api-version=2018-02-01&resource=https%3A%2F%2Fmanagement.azure.com%2F' \
|
|
| python3 -c "import sys,json; print(json.load(sys.stdin).get('access_token',''))" 2>/dev/null || true)
|
|
if [ -z "$TOKEN" ]; then echo NO_MI_TOKEN; exit 0; fi
|
|
SUB=$(curl -s -H Metadata:true 'http://169.254.169.254/metadata/instance/compute/subscriptionId?api-version=2021-02-01&format=text')
|
|
RG=$(curl -s -H Metadata:true 'http://169.254.169.254/metadata/instance/compute/resourceGroupName?api-version=2021-02-01&format=text')
|
|
VM=$(curl -s -H Metadata:true 'http://169.254.169.254/metadata/instance/compute/name?api-version=2021-02-01&format=text')
|
|
echo SUB=$SUB RG=$RG VM=$VM
|
|
# list NSGs attached to NIC
|
|
NIC_URL=$(curl -s -H Metadata:true 'http://169.254.169.254/metadata/instance/network/interface/0/id?api-version=2021-02-01&format=text')
|
|
echo NIC=$NIC_URL
|
|
curl -s -H "Authorization: Bearer $TOKEN" "https://management.azure.com${NIC_URL}?api-version=2023-09-01" | python3 -c "
|
|
import sys,json
|
|
d=json.load(sys.stdin)
|
|
for rule in d.get('properties',{}).get('ipConfigurations',[]):
|
|
print('ipconfig', rule.get('name'))
|
|
" 2>/dev/null || true
|
|
"""
|
|
|
|
_, o, e = c.exec_command(script, timeout=60)
|
|
print(o.read().decode())
|
|
print(e.read().decode(), file=sys.stderr)
|
|
c.close()
|