`
SpreadDiaries
  • 浏览: 54069 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

qemu guest agent 修改密码

阅读更多
"""
      openstack中 虚拟机nova_db.instances.id 需转换为16进制 后补8位
      http://wiki.qemu.org/Features/QAPI/GuestAgent
"""
import os
import sys
import json
import base64
import crypt
import string
import random
import re

if len(sys.argv)<3:
    print('Pls input argument instance password.')
    sys.exit(1)

instance=sys.argv[1]
pwd_=sys.argv[2]

FILE_OPEN_READ="""{"execute":"guest-file-open", "arguments":{"path":"%s","mode":"r"}}"""

FILE_OPEN_WRITE="""{"execute":"guest-file-open", "arguments":{"path":"%s","mode":"w+"}}"""

FILE_READ="""{"execute":"guest-file-read", "arguments":{"handle":%s,"count":%d}}"""

FILE_WRITE="""{"execute":"guest-file-write", "arguments":{"handle":%s,"buf-b64":"%s"}}"""

FILE_CLOSE="""{"execute":"guest-file-close", "arguments":{"handle":%s}}"""

def EXE(param):
    cmd="""virsh qemu-agent-command %s '%s' """ % (instance,param)
    #print "Exe command:%s" % cmd
    stream=os.popen(cmd).read()
    return None if not stream else json.loads(stream)

def guestFileRead(path):
    file_handle=-1
    try:
	file_handle=EXE(FILE_OPEN_READ % path)["return"]
	file_content=EXE(FILE_READ % (file_handle,1024000))["return"]["buf-b64"]
    except Exception,ex:
	print Exception,":",ex
	return None
    finally:
	EXE(FILE_CLOSE % file_handle)
    return file_content

def guestFileWrite(path,content):
    file_handle=-1
    try:
	file_handle=EXE(FILE_OPEN_WRITE % path)["return"]
	write_count=EXE(FILE_WRITE % (file_handle,content))["return"]["count"]
    except Exception,ex:
	print Exception,":",ex
	return -1
    finally:
	EXE(FILE_CLOSE % file_handle)
    return write_count

def genPwd(pwd):
    salt=''.join(random.choice(string.ascii_letters + string.digits) for _ in range(8))
    return crypt.crypt(pwd, "$6$%s" % salt)

def resetPwd(path,pwd):
    content=base64.standard_b64decode(guestFileRead(path))
    #print "Read file content:%s" % content
    user_array=re.split("\n",content)
    for i,line in enumerate(user_array):
	info=line.split(":")
	if info[0]=="root":
	    info[1]=genPwd(pwd)
	    user_array[i]=":".join(info)
	    #print "After change,user info:%s" % user_array[i]
	    break
    content=base64.standard_b64encode("\n".join(user_array)) 	
    write_count=guestFileWrite(path,content)
    print write_count>0
 
resetPwd("/etc/shadow",pwd_)

  转换qga执行的instanceid ---> "instance-%s" % hex(instid)[2:].zfill(8)

0
1
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics