Golang连接Libvirt获取主机列表示例 | 马犇-技术博客

Golang连接Libvirt获取主机列表示例

来源:本站原创 Golang 超过7,009 views围观 1条评论
package main

import (
    "fmt"
    libvirt "github.com/CD1/libvirt-golang"
    "os"
)

func main() {
    connStr := "qemu+ssh://root@10.3.0.190/system"
    var conn libvirt.Connection
    conn, err := libvirt.Open(connStr, libvirt.ReadOnly, os.Stdout)
    if err != nil {
        fmt.Println("connection failure")
    }   
    defer conn.Close()
    var domains []libvirt.Domain
    domains, err = conn.ListDomains(libvirt.DomListAll)
    if err != nil {
        fmt.Println("Get domain list failure!")
        return
    }   
    for _, dom := range domains {
        name, err := dom.Name()
        if err != nil {
            continue
        }   
        fmt.Println(name)
    }   
}