示例#1
0
func dnsmasqHostInfo(c *minicli.Command, resp *minicli.Response) {
	// print info about the mapping
	resp.Header = []string{"ID", "MAC", "IP"}
	resp.Tabular = [][]string{}
	if c.StringArgs["ID"] == Wildcard {
		for id, v := range dnsmasqServers {
			for mac, ip := range v.DHCPhosts {
				resp.Tabular = append(resp.Tabular, []string{strconv.Itoa(id), mac, ip})
			}
		}
	} else {
		id, err := strconv.Atoi(c.StringArgs["ID"])
		if err != nil {
			resp.Error = "Invalid dnsmasq ID"
			return
		}
		if _, ok := dnsmasqServers[id]; ok {
			for mac, ip := range dnsmasqServers[id].DHCPhosts {
				resp.Tabular = append(resp.Tabular, []string{strconv.Itoa(id), mac, ip})
			}
		} else {
			resp.Error = "Invalid dnsmasq ID"
		}
	}
}
示例#2
0
func dnsmasqDNSInfo(c *minicli.Command, resp *minicli.Response) {
	// print info
	resp.Header = []string{"ID", "IP", "Hostname"}
	resp.Tabular = [][]string{}
	if c.StringArgs["ID"] == Wildcard {
		for id, v := range dnsmasqServers {
			for ip, host := range v.Hostnames {
				resp.Tabular = append(resp.Tabular, []string{strconv.Itoa(id), ip, host})
			}
		}
	} else {
		id, err := strconv.Atoi(c.StringArgs["ID"])
		if err != nil {
			resp.Error = "Invalid dnsmasq ID"
			return
		}
		if _, ok := dnsmasqServers[id]; ok {
			for ip, host := range dnsmasqServers[id].Hostnames {
				resp.Tabular = append(resp.Tabular, []string{strconv.Itoa(id), ip, host})
			}
		} else {
			resp.Error = "Invalid dnsmasq ID"
		}
	}
}
示例#3
0
文件: shell.go 项目: cdshann/minimega
func cliShell(c *minicli.Command, resp *minicli.Response, background bool) error {
	var sOut bytes.Buffer
	var sErr bytes.Buffer

	p, err := exec.LookPath(c.ListArgs["command"][0])
	if err != nil {
		return err
	}

	args := []string{p}
	if len(c.ListArgs["command"]) > 1 {
		args = append(args, c.ListArgs["command"][1:]...)
	}

	cmd := &exec.Cmd{
		Path:   p,
		Args:   args,
		Env:    nil,
		Dir:    "",
		Stdout: &sOut,
		Stderr: &sErr,
	}
	log.Info("starting: %v", args)
	if err := cmd.Start(); err != nil {
		return err
	}

	if background {
		go func() {
			if err := cmd.Wait(); err != nil {
				log.Error(err.Error())
				return
			}

			log.Info("command %v exited", args)
			if out := sOut.String(); out != "" {
				log.Info(out)
			}
			if err := sErr.String(); err != "" {
				log.Info(err)
			}
		}()

		return nil
	}

	if err = cmd.Wait(); err != nil {
		return err
	}

	resp.Response = sOut.String()
	resp.Error = sErr.String()

	return nil
}
示例#4
0
func dnsmasqDHCPOptionInfo(c *minicli.Command, resp *minicli.Response) {
	resp.Header = []string{"ID", "Option"}
	resp.Tabular = [][]string{}
	if c.StringArgs["ID"] == Wildcard {
		for id, v := range dnsmasqServers {
			for _, ent := range v.DHCPopts {
				resp.Tabular = append(resp.Tabular, []string{strconv.Itoa(id), ent})
			}
		}
	} else {
		id, err := strconv.Atoi(c.StringArgs["ID"])
		if err != nil {
			resp.Error = "Invalid dnsmasq ID"
			return
		}
		if _, ok := dnsmasqServers[id]; ok {
			for _, ent := range dnsmasqServers[id].DHCPopts {
				resp.Tabular = append(resp.Tabular, []string{strconv.Itoa(id), ent})
			}
		} else {
			resp.Error = "Invalid dnsmasq ID"
		}
	}
}
示例#5
0
func cliVyatta(c *minicli.Command, resp *minicli.Response) error {
	log.Warnln("the vyatta API is deprecated and will be removed in a future release")

	if c.BoolArgs["dhcp"] {
		net := c.StringArgs["network"]

		if len(c.StringArgs) == 0 {
			// List the existing DHCP services
			resp.Header = []string{"Network", "GW", "Start address", "Stop address", "DNS"}
			resp.Tabular = [][]string{}
			for k, v := range vyatta.Dhcp {
				resp.Tabular = append(resp.Tabular, []string{k, v.Gw, v.Start, v.Stop, v.Dns})
			}
		} else if c.StringArgs["gateway"] != "" {
			// Add a new DHCP service
			vyatta.Dhcp[net] = &vyattaDhcp{
				Gw:    c.StringArgs["gateway"],
				Start: c.StringArgs["low"],
				Stop:  c.StringArgs["high"],
				Dns:   c.StringArgs["dns"],
			}

			log.Debug("vyatta add dhcp %v", vyatta.Dhcp[net])
		} else {
			// Deleting a DHCP service
			if _, ok := vyatta.Dhcp[net]; !ok {
				resp.Error = "no such Dhcp service"
			} else {
				log.Debug("vyatta delete dhcp %v", net)
				delete(vyatta.Dhcp, net)
			}
		}
	} else if c.BoolArgs["interfaces"] {
		// Get or update IPv4 interfaces
		if len(c.ListArgs) == 0 {
			resp.Response = fmt.Sprintf("%v", vyatta.Ipv4)
		} else {
			vyatta.Ipv4 = c.ListArgs["net"]
		}
	} else if c.BoolArgs["interfaces6"] {
		// Get or update IPv6 interfaces
		if len(c.ListArgs) == 0 {
			resp.Response = fmt.Sprintf("%v", vyatta.Ipv6)
		} else {
			vyatta.Ipv6 = c.ListArgs["net"]
		}
	} else if c.BoolArgs["rad"] {
		// Get or update rad
		if len(c.ListArgs) == 0 {
			resp.Response = fmt.Sprintf("%v", vyatta.Rad)
		} else {
			vyatta.Rad = c.ListArgs["prefix"]
		}
	} else if c.BoolArgs["ospf"] {
		// Get or update ospf
		if len(c.ListArgs) == 0 {
			resp.Response = fmt.Sprintf("%v", vyatta.Ospf)
		} else {
			vyatta.Ospf = c.ListArgs["network"]
		}
	} else if c.BoolArgs["ospf3"] {
		// Get or update ospf
		if len(c.ListArgs) == 0 {
			resp.Response = fmt.Sprintf("%v", vyatta.Ospf3)
		} else {
			vyatta.Ospf3 = c.ListArgs["network"]
		}
	} else if c.BoolArgs["routes"] {
		if len(c.ListArgs) == 0 {
			resp.Header = []string{"Network", "Route"}
			resp.Tabular = [][]string{}

			for _, v := range vyatta.Routes {
				resp.Tabular = append(resp.Tabular, []string{v.Route, v.NextHop})
			}
		} else {
			err := vyattaUpdateRoutes(c.ListArgs["network"])
			if err != nil {
				resp.Error = err.Error()
			}
		}
	} else if c.BoolArgs["config"] {
		// override everything and just cram the listed file into the floppy
		// image
		if len(c.StringArgs) == 0 {
			resp.Response = vyatta.ConfigFile
		} else {
			vyatta.ConfigFile = c.StringArgs["filename"]
		}
	} else if c.BoolArgs["write"] {
		var err error
		resp.Response, err = vyattaWrite(c.StringArgs["filename"])
		if err != nil {
			resp.Error = err.Error()
		}
	} else {
		// Display info about running services
		var dhcpKeys []string
		for k, _ := range vyatta.Dhcp {
			dhcpKeys = append(dhcpKeys, k)
		}

		var routes []string
		for _, k := range vyatta.Routes {
			routes = append(routes, k.Route)
		}

		resp.Header = []string{
			"IPv4 addresses",
			"IPv6 addresses",
			"RAD",
			"DHCP servers",
			"OSPF",
			"OSPF3",
			"Routes",
		}
		resp.Tabular = [][]string{[]string{
			fmt.Sprintf("%v", vyatta.Ipv4),
			fmt.Sprintf("%v", vyatta.Ipv6),
			fmt.Sprintf("%v", vyatta.Rad),
			fmt.Sprintf("%v", dhcpKeys),
			fmt.Sprintf("%v", vyatta.Ospf),
			fmt.Sprintf("%v", vyatta.Ospf3),
			fmt.Sprintf("%v", routes),
		}}
	}

	return nil
}