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

转载于:https://www.cnblogs.com/pdjun/p/7410821.html

你可能感兴趣的:(运维,python,网络)