Commit 80fd6e64 authored by zdc's avatar zdc

added ssh, extend config values

parent 885a427f
...@@ -2,6 +2,29 @@ ...@@ -2,6 +2,29 @@
address = "" address = ""
port = "40080" port = "40080"
[routers] [router01]
user = "" address = "127.0.0.1"
port = "22"
user = "lookis"
password = "" password = ""
ping_command = "ping count 5"
traceroute_command = "traceroute as-number-lookup no-resolve"
showroute_command = "show route all"
[router02]
address = "127.0.0.2"
port = "22"
user = "lookis"
password = ""
ping_command = "ping count 5"
traceroute_command = "traceroute as-number-lookup no-resolve"
showroute_command = "show route all"
[router03]
address = "127.0.0.3"
port = "22"
user = "lookis"
password = ""
ping_command = "ping count 5"
traceroute_command = "traceroute as-number-lookup no-resolve"
showroute_command = "show route all"
\ No newline at end of file
...@@ -7,6 +7,7 @@ import ( ...@@ -7,6 +7,7 @@ import (
"net" "net"
"net/http" "net/http"
"github.com/spf13/viper" "github.com/spf13/viper"
"golang.org/x/crypto/ssh"
) )
func page01 (w http.ResponseWriter, r *http.Request) { func page01 (w http.ResponseWriter, r *http.Request) {
...@@ -14,20 +15,90 @@ func page01 (w http.ResponseWriter, r *http.Request) { ...@@ -14,20 +15,90 @@ func page01 (w http.ResponseWriter, r *http.Request) {
t.Execute(w, nil) t.Execute(w, nil)
} }
func bytetostring (byteinput []byte) string {
byteslen := len(byteinput)
stringoutput := string(byteinput[:byteslen])
return stringoutput
}
func doit (w http.ResponseWriter, r *http.Request) { func doit (w http.ResponseWriter, r *http.Request) {
type data struct { type data struct {
Command string Command string
Items string Items string
} }
if net.ParseIP(r.PostFormValue("router")) != nil && net.ParseIP(r.PostFormValue("argument")) != nil && ( r.PostFormValue("action") == "ping" || r.PostFormValue("action") == "traceroute" || r.PostFormValue("action") == "showroute") { if net.ParseIP(r.PostFormValue("argument")) != nil && ( r.PostFormValue("action") == "ping" || r.PostFormValue("action") == "traceroute" || r.PostFormValue("action") == "showroute") {
fmt.Println(net.ParseIP(r.PostFormValue("router"))) router_id := r.PostFormValue("router")
fmt.Println(net.ParseIP(r.PostFormValue("argument"))) router := viper.GetString(router_id+".address")
//router := r.PostFormValue("router") if net.ParseIP(router) == nil {
log.Fatal("Bad router id: ", router)
t, _ := template.ParseFiles("badinput.gtpl")
t.Execute(w, nil)
} else {
action := r.PostFormValue("action") action := r.PostFormValue("action")
argument := r.PostFormValue("argument") argument := r.PostFormValue("argument")
output := data{Command: action, Items: "Generated output for "+argument}
port := viper.GetString(router_id+".port")
username := viper.GetString(router_id+".user")
password := viper.GetString(router_id+".password")
ping_command := viper.GetString(router_id+".ping_command")
traceroute_command := viper.GetString(router_id+".traceroute_command")
showroute_command := viper.GetString(router_id+".showroute_command")
sshConfig := &ssh.ClientConfig {
User: username,
Auth: []ssh.AuthMethod {
ssh.Password(password),
},
}
connection, err := ssh.Dial("tcp", router+":"+port, sshConfig)
if err != nil {
log.Fatal("unable to connect: ", err)
}
defer connection.Close()
session, err := connection.NewSession()
if err != nil {
log.Fatal("Failed to create session: ", err)
}
modes := ssh.TerminalModes {
ssh.ECHO: 0, // disable echoing
ssh.TTY_OP_ISPEED: 14400, // input speed = 14.4kbaud
ssh.TTY_OP_OSPEED: 14400, // output speed = 14.4kbaud
}
if err := session.RequestPty("xterm", 80, 40, modes); err != nil {
session.Close()
log.Fatal("request for pseudo terminal failed: ", err)
}
var rawoutput []byte
if action == "ping" {
rawoutput, err = session.Output(ping_command+" "+argument)
if err != nil {
log.Fatal("Cannot execute command: ", err)
}
}
if action == "traceroute" {
rawoutput, err = session.Output(traceroute_command+" "+argument)
if err != nil {
log.Fatal("Cannot execute command: ", err)
}
}
if action == "showroute" {
rawoutput, err = session.Output(showroute_command+" "+argument)
if err != nil {
log.Fatal("Cannot execute command: ", err)
}
}
output := data{Command: action, Items: bytetostring(rawoutput)}
t, _ := template.ParseFiles("page02.gtpl") t, _ := template.ParseFiles("page02.gtpl")
t.Execute(w, output) t.Execute(w, output)
}
} else { } else {
t, _ := template.ParseFiles("badinput.gtpl") t, _ := template.ParseFiles("badinput.gtpl")
t.Execute(w, nil) t.Execute(w, nil)
...@@ -40,7 +111,7 @@ func main() { ...@@ -40,7 +111,7 @@ func main() {
viper.AddConfigPath(".") viper.AddConfigPath(".")
errconfig := viper.ReadInConfig() // Find and read the config file errconfig := viper.ReadInConfig() // Find and read the config file
if errconfig != nil { // Handle errors reading the config file if errconfig != nil { // Handle errors reading the config file
panic(fmt.Errorf("Fatal error config file: %s \n", errconfig)) panic(fmt.Errorf("Fatal error config file: %s \n", errconfig))
} }
server_address := viper.GetString("server.address") server_address := viper.GetString("server.address")
......
...@@ -18,7 +18,7 @@ ...@@ -18,7 +18,7 @@
<td style="width: 50px;">&nbsp;</td> <td style="width: 50px;">&nbsp;</td>
<td> <td>
<select name="router"> <select name="router">
<option value="127.0.0.1">router01</option> <option value="router01">router01</option>
<option value="router02">router02</option> <option value="router02">router02</option>
<option value="router03">router03</option> <option value="router03">router03</option>
</select> </select>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment