コード例 #1
0
func main() {
	flag.Parse()

	if *username == "" {
		fmt.Printf("Enter a valid username: "******"<get-system-information/>"))
	if err != nil {
		panic(err)
	}
	var q SystemInformation

	xml.Unmarshal([]byte(reply.RawReply), &q)
	fmt.Printf("hostname: %s\n", q.HostName)
	fmt.Printf("model: %s\n", q.HardwareModel)
	fmt.Printf("version: %s\n", q.OsVersion)
}
コード例 #2
0
ファイル: ssh_example1.go プロジェクト: Juniper/go-netconf
func main() {
	sshConfig := &ssh.ClientConfig{
		Config: ssh.Config{
			Ciphers: []string{"aes128-cbc", "hmac-sha1"},
		},
		User: "******",
		Auth: []ssh.AuthMethod{ssh.Password("mypass")},
	}

	s, err := netconf.DialSSH("1.1.1.1", sshConfig)

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

	defer s.Close()

	fmt.Println(s.ServerCapabilities)
	fmt.Println(s.SessionID)

	// Sends raw XML
	reply, err := s.Exec(netconf.RawMethod("<get-chassis-inventory/>"))
	if err != nil {
		panic(err)
	}
	fmt.Printf("Reply: %+v", reply)
}
コード例 #3
0
ファイル: junos_agent.go プロジェクト: RobWC/jmcom
// RunCommand Run a command against a host
func (a *JunosAgent) RunCommand(command string) {
	r, err := a.Session.Exec(netconf.RawMethod(fmt.Sprintf("<command format=\"ascii\">%s</command>", command)))
	if err != nil {
		a.returnMsg(r.Data, command, err)
	}
	v := a.parser.Trim(r.Data)
	a.returnMsg(v, command, err)
}
コード例 #4
0
ファイル: junos_agent.go プロジェクト: RobWC/jmcom
// LoadConfig load a set of configuration data to the device
func (a *JunosAgent) LoadConfig(config string, cfgType CfgType, overwrite bool) {
	// pull the config to the agent
	// open the configuration mode
	// commit the configuration
	r, err := a.Session.Exec(netconf.RawMethod(fmt.Sprintf("", config)))
	if err != nil {
		a.returnMsg(r.Data, "config", err)
	}
	a.returnMsg(r.Data, "config", err)
}
コード例 #5
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)
	}
}
コード例 #6
0
ファイル: test.go プロジェクト: RobWC/jmcom
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")
}
コード例 #7
0
ファイル: gohan_remote.go プロジェクト: gitter-badger/gohan
//SetUp sets up vm to with environment
func init() {
	gohanRemoteInit := func(env *Environment) {
		vm := env.VM

		builtins := map[string]interface{}{
			"gohan_netconf_open": func(call otto.FunctionCall) otto.Value {
				if len(call.ArgumentList) != 2 {
					panic("Wrong number of arguments in gohan_netconf_open call.")
				}
				rawHost, _ := call.Argument(0).Export()
				host, ok := rawHost.(string)
				if !ok {
					return otto.NullValue()
				}
				rawUserName, _ := call.Argument(1).Export()
				userName, ok := rawUserName.(string)
				if !ok {
					return otto.NullValue()
				}
				config := util.GetConfig()
				publicKeyFile := config.GetString("ssh/key_file", "")
				if publicKeyFile == "" {
					return otto.NullValue()
				}
				sshConfig := &ssh.ClientConfig{
					User: userName,
					Auth: []ssh.AuthMethod{
						util.PublicKeyFile(publicKeyFile),
					},
				}
				s, err := netconf.DialSSH(host, sshConfig)

				if err != nil {
					ThrowOttoException(&call, "Error during gohan_netconf_open: %s", err.Error())
				}
				value, _ := vm.ToValue(s)
				return value
			},
			"gohan_netconf_close": func(call otto.FunctionCall) otto.Value {
				if len(call.ArgumentList) != 1 {
					panic("Wrong number of arguments in gohan_netconf_close call.")
				}
				rawSession, _ := call.Argument(0).Export()
				s, ok := rawSession.(*netconf.Session)
				if !ok {
					ThrowOttoException(&call, "Error during gohan_netconf_close")
				}
				s.Close()
				return otto.NullValue()
			},
			"gohan_netconf_exec": func(call otto.FunctionCall) otto.Value {
				if len(call.ArgumentList) != 2 {
					panic("Wrong number of arguments in gohan_netconf_exec call.")
				}
				rawSession, _ := call.Argument(0).Export()
				s, ok := rawSession.(*netconf.Session)
				if !ok {
					return otto.NullValue()
				}
				rawCommand, _ := call.Argument(1).Export()
				command, ok := rawCommand.(string)
				if !ok {
					return otto.NullValue()
				}
				reply, err := s.Exec(netconf.RawMethod(command))
				resp := map[string]interface{}{}
				if err != nil {
					resp["status"] = "error"
					resp["output"] = err.Error()
				} else {
					resp["status"] = "success"
					resp["output"] = reply
				}
				value, _ := vm.ToValue(resp)
				return value
			},
			"gohan_ssh_open": func(call otto.FunctionCall) otto.Value {
				if len(call.ArgumentList) != 2 {
					panic("Wrong number of arguments in gohan_netconf_open call.")
				}
				rawHost, _ := call.Argument(0).Export()
				host, ok := rawHost.(string)
				if !ok {
					return otto.NullValue()
				}
				rawUserName, _ := call.Argument(1).Export()
				userName, ok := rawUserName.(string)
				if !ok {
					return otto.NullValue()
				}
				config := util.GetConfig()
				publicKeyFile := config.GetString("ssh/key_file", "")
				if publicKeyFile == "" {
					return otto.NullValue()
				}
				sshConfig := &ssh.ClientConfig{
					User: userName,
					Auth: []ssh.AuthMethod{
						util.PublicKeyFile(publicKeyFile),
					},
				}
				conn, err := ssh.Dial("tcp", host, sshConfig)
				if err != nil {
					ThrowOttoException(&call, "Error during gohan_ssh_open %s", err)
				}
				session, err := conn.NewSession()
				if err != nil {
					ThrowOttoException(&call, "Error during gohan_ssh_open %s", err)
				}
				value, _ := vm.ToValue(session)
				return value
			},
			"gohan_ssh_close": func(call otto.FunctionCall) otto.Value {
				if len(call.ArgumentList) != 1 {
					panic("Wrong number of arguments in gohan_netconf_close call.")
				}
				rawSession, _ := call.Argument(0).Export()
				s, ok := rawSession.(*ssh.Session)
				if !ok {
					ThrowOttoException(&call, "Error during gohan_ssh_close")
				}
				s.Close()
				return otto.NullValue()
			},
			"gohan_ssh_exec": func(call otto.FunctionCall) otto.Value {
				if len(call.ArgumentList) != 2 {
					panic("Wrong number of arguments in gohan_netconf_exec call.")
				}
				rawSession, _ := call.Argument(0).Export()
				s, ok := rawSession.(*ssh.Session)
				if !ok {
					return otto.NullValue()
				}
				rawCommand, _ := call.Argument(1).Export()
				command, ok := rawCommand.(string)
				if !ok {
					return otto.NullValue()
				}
				var stdoutBuf bytes.Buffer
				s.Stdout = &stdoutBuf
				err := s.Run(command)
				resp := map[string]interface{}{}
				if err != nil {
					resp["status"] = "error"
					resp["output"] = err.Error()
				} else {
					resp["status"] = "success"
					resp["output"] = stdoutBuf.String()
				}
				value, _ := vm.ToValue(resp)
				return value
			},
		}
		for name, object := range builtins {
			vm.Set(name, object)
		}
	}
	RegistInit(gohanRemoteInit)
}
コード例 #8
0
func (*ChassisEnvCmd) Method() netconf.RawMethod {
	return netconf.RawMethod(CHASSIS_ENV_CMD_TMPL)
}
コード例 #9
0
func (*ChassisZonesRequest) Method() netconf.RawMethod {
	return netconf.RawMethod(CHASSIS_ZONES_REQUEST_TMPL)
}