yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/esxi/shell/ip.go (about) 1 // Copyright 2019 Yunion 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package shell 16 17 import ( 18 "sort" 19 20 "yunion.io/x/pkg/errors" 21 "yunion.io/x/pkg/util/netutils" 22 23 "yunion.io/x/cloudmux/pkg/multicloud/esxi" 24 "yunion.io/x/onecloud/pkg/util/shellutils" 25 ) 26 27 func init() { 28 type IPListOption struct { 29 Host bool `help:"List only host ips"` 30 Vm bool `help:"List only vm ips"` 31 Cidr string `help:"Filter ips in this cidr, such as 192.168.10.0/24"` 32 } 33 shellutils.R(&IPListOption{}, "ip-list", "List all ip", func(cli *esxi.SESXiClient, args *IPListOption) error { 34 host, vm := args.Host, args.Vm 35 if !host && !vm { 36 host, vm = true, true 37 } 38 var cidr netutils.IPV4Prefix 39 var err error 40 if len(args.Cidr) > 0 { 41 cidr, err = netutils.NewIPV4Prefix(args.Cidr) 42 if err != nil { 43 return errors.Wrap(err, "unable to parse cidr") 44 } 45 } 46 list := make([]IPDetails, 0, 5) 47 if host { 48 hostIps, _, err := cli.AllHostIP() 49 if err != nil { 50 return errors.Wrap(err, "AllHostIP") 51 } 52 for n, ip := range hostIps { 53 ipaddr, err := netutils.NewIPV4Addr(ip) 54 if err != nil { 55 return errors.Wrapf(err, "NewIPV4Addr for ip %s", ip) 56 } 57 if len(args.Cidr) > 0 && !cidr.Contains(ipaddr) { 58 continue 59 } 60 list = append(list, IPDetails{ 61 Name: n, 62 Type: "host", 63 Ip: ip, 64 Ipaddr: ipaddr, 65 }) 66 } 67 } 68 if vm { 69 vmipinfos, err := cli.VMIP2() 70 if err != nil { 71 return errors.Wrap(err, "VMIP2") 72 } 73 //split 74 for i := range vmipinfos { 75 for _, macip := range vmipinfos[i].MacIps { 76 mac := macip.Mac 77 if len(macip.IPs) == 0 { 78 list = append(list, IPDetails{ 79 Name: vmipinfos[i].Name, 80 Mac: mac, 81 Uuid: vmipinfos[i].Uuid, 82 Moid: vmipinfos[i].Moid, 83 PowerState: vmipinfos[i].PowerState, 84 Type: "vm", 85 Ip: "", 86 Ipaddr: 0, 87 }) 88 continue 89 } 90 ip := macip.IPs[0] 91 ipaddr, err := netutils.NewIPV4Addr(ip) 92 if err != nil { 93 return errors.Wrapf(err, "NewIPV4Addr for ip %s", ip) 94 } 95 if len(args.Cidr) > 0 && !cidr.Contains(ipaddr) { 96 continue 97 } 98 99 list = append(list, IPDetails{ 100 Name: vmipinfos[i].Name, 101 Mac: mac, 102 Uuid: vmipinfos[i].Uuid, 103 Moid: vmipinfos[i].Moid, 104 PowerState: vmipinfos[i].PowerState, 105 Type: "vm", 106 Ip: ip, 107 Ipaddr: ipaddr, 108 }) 109 } 110 } 111 112 } 113 sort.Slice(list, func(i, j int) bool { 114 return list[i].Ipaddr < list[j].Ipaddr 115 }) 116 printList(list, []string{"name", "type", "ip", "mac", "Power_State", "uuid", "moid"}) 117 return nil 118 }) 119 } 120 121 type IPDetails struct { 122 Moid string 123 PowerState string 124 Uuid string 125 Name string 126 Type string 127 Ip string 128 Mac string 129 Ipaddr netutils.IPV4Addr 130 } 131 132 func (i IPDetails) GetMoid() string { 133 return i.Moid 134 } 135 func (i IPDetails) GetPowerState() string { 136 return i.PowerState 137 } 138 func (i IPDetails) GetUuid() string { 139 return i.Uuid 140 } 141 func (i IPDetails) GetMac() string { 142 return i.Mac 143 } 144 func (i IPDetails) GetName() string { 145 return i.Name 146 } 147 148 func (i IPDetails) GetType() string { 149 return i.Type 150 } 151 152 func (i IPDetails) GetIp() string { 153 return i.Ip 154 }