resetroot_169route_python2(用于ubuntu12.04和14.04,centos系列)
生活随笔
收集整理的這篇文章主要介紹了
resetroot_169route_python2(用于ubuntu12.04和14.04,centos系列)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
resetroot_169route_python2(用于ubuntu12.04和14.04,centos系列) #!/usr/bin/pythonimport os
import json
import subprocess
from cloudinit.sources.DataSourceConfigDrive import find_candidate_devs, read_config_drive
from cloudinit.util import mount_cb
from six.moves.urllib.request import urlopen
from six.moves.urllib.parse import urlencodedef shell(cmd):sp = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)out, err = sp.communicate()return out, errINSTANCE_ID_RUN_PATH = "/run/cloud-init/.instance-id"# Ensure /run/cloud-init/.instance-id not exists so we can reload DataSource from ConfigDrive.
if os.path.isfile(INSTANCE_ID_RUN_PATH):os.unlink(INSTANCE_ID_RUN_PATH)# Before resetroot, We must ensure if 169.254.169.254 have gateway
gateways = []
for dev in find_candidate_devs():results = mount_cb(dev, read_config_drive)network_data = results.get("networkdata")networks = network_data.get("networks",None)found = devif found:if not networks:continuefor network in networks:routes = network.get("routes", None)if not routes:continuefor route in routes:gateway = route.get('gateway')gateways.append(gateway)breakif gateways:gateway = gateways[0]cmd = "route add -host 169.254.169.254 gw %s" % gatewayout,err = shell(cmd)data = urlopen("http://169.254.169.254/openstack/latest/meta_data.json").read()
json_data = json.loads(data.decode("utf-8"))meta = json_data.get("meta")
if meta:adminPass = meta.get("admin_pass")if adminPass:os.system("echo 'root:%s' | chpasswd" % adminPass)params = urlencode({"delete":True})f = urlopen("http://169.254.169.254/openstack/latest/password", params)f.read() View Code
?
posted on 2018-09-12 18:00?gushiren 閱讀(...) 評論(...) 編輯 收藏轉載于:https://www.cnblogs.com/gushiren/p/9636373.html
總結
以上是生活随笔為你收集整理的resetroot_169route_python2(用于ubuntu12.04和14.04,centos系列)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 告诉你,我认识的上网的方式
- 下一篇: Python:每日一题002