Пример #1
0
func main() {
	username := flag.String("username", "", "User to login with")
	password := flag.String("password", "", "Password to login with")
	flag.Usage = func() {
		fmt.Fprintf(os.Stderr, "usage: %s [flags] targets...\n", os.Args[0])
		flag.PrintDefaults()
		os.Exit(2)
	}

	flag.Parse()

	if flag.NArg() == 0 {
		flag.Usage()
	}

	s, err := netconf.DialSSH(flag.Arg(0),
		netconf.SSHConfigPassword(*username, *password))
	if err != nil {
		panic(err)
	}

	defer s.Close()

	fmt.Printf("Server Capabilities: '%+v'\n", s.ServerCapabilities)
	fmt.Printf("Session Id: %d\n\n", s.SessionID)

	//reply, err := s.Exec([]byte("<rpc><get-config><source><running/></source></get-config></rpc>"))
	reply, err := s.ExecRPC(netconf.RPCGetConfig("running"))
	if err != nil {
		panic(err)
	}
	fmt.Printf("Reply: %+v", reply)

}
Пример #2
0
func (zr *ChassisZonesRequest) Run(count int, replyChan chan<- *netconf.RPCReply) {

	go func() {
		ticker := time.NewTicker(zr.Interval)

		for {
			select {
			case <-ticker.C:
				// Get SSH/NETCONF session going for use throughout the life of this request.
				if s, err := netconf.DialSSH(zr.LoopBackIP.String(),
					netconf.SSHConfigPassword(zr.UserName, zr.Password)); err != nil {
					log.Errorln(err)
					s.Close()
					close(replyChan)
				} else if ncReply, err := s.Exec(zr.Method()); err != nil {
					log.Errorln(err)
					s.Close()
					close(replyChan)
				} else {
					s.Close()
					replyChan <- ncReply
				}

				count--
				if count < 0 {
					break
				}
			}
		}
	}()

	/*		cfg := config.GetConfig()

			file, err := os.Create(cfg.ChassisZonesResultsFile)

			if err != nil {
				log.Fatalln(err)
			}

			for _ = range ticker.C {
				if ncReply, err := s.Exec(zr.Method()); err != nil {
					log.Error(err)
				} else if chassisEnvResp, err := response.NewChassisEnvResponse(&ncReply.Data); err != nil {
					log.Error(err)
				} else {
					chassisEnvResp.WriteCSV(file)
				}
			}*/
}
Пример #3
0
func BuildConfig() *ssh.ClientConfig {
	var config *ssh.ClientConfig
	var pass string
	if *pubkey == true {
		if *agent {
			var err error
			config, err = netconf.SSHConfigPubKeyAgent(*username)
			if err != nil {
				log.Fatal(err)
			}
		} else {
			if *nopassphrase {
				pass = "******"
			} else {
				if *passphrase != "" {
					pass = *passphrase
				} else {
					var readpass []byte
					var err error
					fmt.Printf("Enter Passphrase for %s: ", *key)
					readpass, err = terminal.ReadPassword(int(syscall.Stdin))
					if err != nil {
						log.Fatal(err)
					}
					pass = string(readpass)
					fmt.Println()
				}
			}
			var err error
			config, err = netconf.SSHConfigPubKeyFile(*username, *key, pass)
			if err != nil {
				log.Fatal(err)
			}
		}
	} else {
		fmt.Printf("Enter Password: ")
		bytePassword, err := terminal.ReadPassword(int(syscall.Stdin))
		if err != nil {
			log.Fatal(err)
		}
		fmt.Println()

		config = netconf.SSHConfigPassword(*username, string(bytePassword))
	}
	return config
}
Пример #4
0
func main() {

	rawMethod := netconf.RawMethod("<traceroute><host>8.8.8.8</host></traceroute>")
	s := new(netconf.Session)
	var err error

	if s, err = netconf.DialSSH("192.168.1.1", netconf.SSHConfigPassword("username", "password")); err != nil {
		log.Fatal(err)
	} else {
		defer s.Close()
	}

	tr := new(traceroute.TraceRoute)
	if ncReply, err := s.Exec(rawMethod); err != nil {
		log.Fatal(err)
	} else if _, err := tr.ReadXMLFrom(bytes.NewBuffer([]byte(ncReply.Data))); err != nil {
		log.Fatal(err)
	} else {
		tr.WriteCLITo(os.Stdout)
	}
}
Пример #5
0
func foo() {
	username := "******"
	password := "******"
	host := "172.19.100.49"

	s, err := netconf.DialSSH(host, netconf.SSHConfigPassword(username, password))
	if err != nil {
		panic(err)
	}

	defer s.Close()

	fmt.Printf("Session Id: %d\n\n", s.SessionID)

	reply, err := s.Exec(netconf.RawMethod("<command format=\"ascii\">show version</command>"))
	if err != nil {
		panic(err)
	}
	p := &Parser{}
	v := p.Trim(reply.Data)
	fmt.Println("REPLY START")
	fmt.Printf("%s", v)
	fmt.Println("REPLY END")
}
Пример #6
0
func (cer *ChassisEnvCmd) Run() (<-chan *netconf.RPCReply, <-chan error) {

	replyChan := make(chan *netconf.RPCReply)
	errChan := make(chan error)

	go func() {
		const thisFunc = "*ChassisEnvCmd.Run()"
		cntxLog := log.WithFields(log.Fields{
			"func":     thisFunc,
			"deadline": cer.Deadline,
			"interval": cer.Interval,
			"username": cer.UserName,
			"ip":       cer.LoopBackIP,
		})

		cntxLog.Debugln("Starting")

		// Flag for breaking loop
		done := false

		// Use a ticker to notify us when it's time to execute the command again.
		ticker := time.NewTicker(cer.Interval)
		// Use time.After to let us know when we've reached our deadline.
		deadline := time.After(cer.Deadline)

		for !done {
			select {
			case <-ticker.C:
				cntxLog.Debugln("dialing SSH")
				if s, err := netconf.DialSSH(cer.LoopBackIP.String(),
					netconf.SSHConfigPassword(cer.UserName, cer.Password)); err != nil {
					cntxLog.Debugln(err)
					errChan <- &RunError{
						Method:    thisFunc,
						ClientErr: fmt.Sprintf("could not dial %s via NETCONF over SSH", cer.LoopBackIP.String()),
						Err:       err,
						Cmd:       cer,
					}
				} else if ncReply, err := s.Exec(cer.Method()); err != nil {
					cntxLog.Debugln(err)
					s.Close()
					errChan <- &RunError{
						Method:    thisFunc,
						ClientErr: fmt.Sprintf("could not execute show chassis enviornment command"),
					}
				} else {
					s.Close()
					replyChan <- ncReply
				}
			case <-deadline:
				cntxLog.Debugln("deadline reached")
				close(replyChan)
				close(errChan)
				done = true
			case <-cer.cancelled:
				cntxLog.Debugln("command cancelled")
				close(replyChan)
				close(errChan)
				done = true
			}
		}

		cntxLog.Debugln("Exiting")
	}()

	return replyChan, errChan
}