Commit 98dee1e9 authored by zdc's avatar zdc

fixed command run, added suffix, examples

* deleted pty usage (not need to our purposes)
* added suffix support in commands (see examples in config.toml)
* filled examples in config
parent 0147e360
......@@ -3,6 +3,7 @@ address = ""
port = "40080"
[router01]
type = "JunOS"
address = "127.0.0.1"
port = "22"
user = "lookis"
......@@ -12,19 +13,23 @@ traceroute_command = "traceroute as-number-lookup no-resolve"
showroute_command = "show route all"
[router02]
type = "VyOS"
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"
ping_command = "/opt/vyatta/bin/ping"
ping_command_suffix = "count 5"
traceroute_command = "traceroute"
showroute_command = "/opt/vyatta/bin/vtyshow.pl show ip route"
[router03]
type = "RouterOS"
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
ping_command = "ping count=5"
traceroute_command = "tool traceroute count=1"
showroute_command = "ip route check"
showroute_command_suffix = "once"
......@@ -69,7 +69,7 @@ func SelectCommand (command, ping_command, traceroute_command, showroute_command
}
// connect to router and run command
func SSHRun (router_ip, port, username, password, command, argument string) DataOut {
func SSHRun (router_ip, port, username, password, command, argument, suffix string) DataOut {
var rawoutput []byte
var output DataOut
var allok bool = true
......@@ -82,13 +82,6 @@ func SSHRun (router_ip, port, username, password, command, argument string) Data
},
}
// setting terminal
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
}
// connect to router
connection, err := ssh.Dial("tcp", router_ip+":"+port, sshConfig)
if err != nil {
......@@ -104,24 +97,17 @@ func SSHRun (router_ip, port, username, password, command, argument string) Data
allok = false
connection.Close()
} else {
// run terminal
if err := session.RequestPty("xterm", 80, 40, modes); err != nil {
session.Close()
output = PrepareError ("Connection failed", "Request for pseudo terminal failed")
allok = false
}
}
if allok == true {
// run command and save output
rawoutput, err = session.Output(command+" "+argument)
if err != nil {
output = PrepareError ("Cannot execute command", command+" "+argument)
allok = false
connection.Close()
} else {
output = PrepareOutput ("Result", ByteToString(rawoutput))
connection.Close()
if allok == true {
// run command and save output
rawoutput, err = session.Output(command+" "+argument+suffix)
if err != nil {
output = PrepareError ("Cannot execute command", command+" "+argument+suffix)
allok = false
connection.Close()
} else {
output = PrepareOutput ("Result", ByteToString(rawoutput))
connection.Close()
}
}
}
}
......@@ -136,7 +122,7 @@ func ShowPage (w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" {
var action, argument string
var router_ip, port, username, password string
var ping_command, traceroute_command, showroute_command, command string
var ping_command, traceroute_command, showroute_command, command, suffix string
var output DataOut
var allok bool = true
......@@ -180,7 +166,11 @@ func ShowPage (w http.ResponseWriter, r *http.Request) {
// connect to router and run command
if allok == true {
command = SelectCommand(action, ping_command, traceroute_command, showroute_command)
output = SSHRun(router_ip, port, username, password, command, argument)
// check if command need suffix
if viper.IsSet(input_router+"."+action+"_command_suffix") != false {
suffix = " "+viper.GetString(input_router+"."+action+"_command_suffix")
}
output = SSHRun(router_ip, port, username, password, command, argument, suffix)
// show result to user
t.Execute(w, output)
} else {
......
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