-
Notifications
You must be signed in to change notification settings - Fork 109
/
get_system_information.go
121 lines (108 loc) · 3.07 KB
/
get_system_information.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// Go NETCONF Client - Juniper Example (show system information)
//
// Copyright (c) 2013-2018, Juniper Networks, Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"bufio"
"encoding/xml"
"flag"
"fmt"
"log"
"os"
"syscall"
"github.com/Juniper/go-netconf/netconf"
"golang.org/x/crypto/ssh"
"golang.org/x/crypto/ssh/terminal"
)
var (
host = flag.String("host", "vmx1", "Hostname")
username = flag.String("username", "", "Username")
key = flag.String("key", os.Getenv("HOME")+"/.ssh/id_rsa", "SSH private key file")
passphrase = flag.String("passphrase", "", "SSH private key passphrase (cleartext)")
nopassphrase = flag.Bool("nopassphrase", false, "SSH private key does not contain a passphrase")
pubkey = flag.Bool("pubkey", false, "Use SSH public key authentication")
agent = flag.Bool("agent", false, "Use SSH agent for public key authentication")
)
// SystemInformation provides a representation of the system-information container
type SystemInformation struct {
HardwareModel string `xml:"system-information>hardware-model"`
OsName string `xml:"system-information>os-name"`
OsVersion string `xml:"system-information>os-version"`
SerialNumber string `xml:"system-information>serial-number"`
HostName string `xml:"system-information>host-name"`
}
// BuildConfig captures information from the console to build a SSH Client Config
func BuildConfig() *ssh.ClientConfig {
var config *ssh.ClientConfig
var pass string
if *pubkey {
if *agent {
var err error
config, err = netconf.SSHConfigPubKeyAgent(*username)
if err != nil {
log.Fatal(err)
}
} else {
if *nopassphrase {
pass = "\n"
} else {
if *passphrase != "" {
pass = *passphrase
} else {
var readpass []byte
var err error
fmt.Printf("Enter Passphrase for %s: ", *key)
readpass, err = terminal.ReadPassword(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(syscall.Stdin)
if err != nil {
log.Fatal(err)
}
fmt.Println()
config = netconf.SSHConfigPassword(*username, string(bytePassword))
}
return config
}
func main() {
flag.Parse()
if *username == "" {
fmt.Printf("Enter a valid username: ")
r := bufio.NewScanner(os.Stdin)
r.Scan()
*username = r.Text()
}
config := BuildConfig()
s, err := netconf.DialSSH(*host, config)
if err != nil {
log.Fatal(err)
}
defer s.Close()
reply, err := s.Exec(netconf.RawMethod("<get-system-information/>"))
if err != nil {
panic(err)
}
var q SystemInformation
err = xml.Unmarshal([]byte(reply.RawReply), &q)
if err != nil {
log.Fatal(err)
}
fmt.Printf("hostname: %s\n", q.HostName)
fmt.Printf("model: %s\n", q.HardwareModel)
fmt.Printf("version: %s\n", q.OsVersion)
}