python libvirt api openAuth()

使用tcp连接libvirt服务,不适用密码

/etc/libvirt/libvirtd.conf 
auth_tcp="none"
service libvirtd restart
import libvirt
conn = libvirt.open("qemu+tcp://localhost/system")
print conn.getHostname()

使用tcp加密码连接libvirt

auth_tcp="sasl"
saslpasswd2 -a libvirt admin
sasldblistusers2 -f /etc/libvirt/passwd.db
service libvirtd restart

连接代码

import libvirt
USER = "root"
PASSWD = "mypasswd"

def authcb(credentials, user_data):
   for credential in credentials:
       if credential[0] == libvirt.VIR_CRED_AUTHNAME:
           credential[4] = USER
       elif credential[0] == libvirt.VIR_CRED_PASSPHRASE:
           credential[4] = PASSWD
   return 0

auth = [[libvirt.VIR_CRED_AUTHNAME, libvirt.VIR_CRED_PASSPHRASE], authcb, None]
conn = libvirt.openAuth("qemu+ssh://localhost/system", auth, 0)

print conn.getHostname()

参考文档:

  http://blog.csdn.net/gaoxingnengjisuan/article/details/9674315

  http://blog.csdn.net/qq_21398167/article/details/46422583

posted @ 2017-08-22 11:35  不会游泳的鱼pdj  阅读(719)  评论(0编辑  收藏  举报