diff --git a/args.go b/args.go index 35e1b8c..bd9b607 100644 --- a/args.go +++ b/args.go @@ -7,6 +7,8 @@ import ( ) var INFO log.LogFlag +var RUN log.LogFlag +var SSH log.LogFlag func init() { INFO.B = false @@ -15,4 +17,18 @@ func init() { INFO.Short = "shell" INFO.Desc = "general info" INFO.Register() + + RUN.B = false + RUN.Name = "RUN" + RUN.Subsystem = "shell" + RUN.Short = "shell" + RUN.Desc = "Run() info" + RUN.Register() + + SSH.B = false + SSH.Name = "SSH" + SSH.Subsystem = "shell" + SSH.Short = "shell" + SSH.Desc = "ssh() info" + SSH.Register() } diff --git a/run.go b/run.go index 6dfda3f..0788105 100644 --- a/run.go +++ b/run.go @@ -1,19 +1,19 @@ package shell -import "strings" -import "time" -import "os/exec" -import "bytes" -import "io" -import "fmt" -import "os" -import "bufio" +import ( + "strings" + "time" + "os/exec" + "bytes" + "io" + "fmt" + "os" + "bufio" -import "github.com/svent/go-nbreader" -// import "github.com/davecgh/go-spew/spew" + "github.com/svent/go-nbreader" -import "log" -// import "go.wit.com/log" + "go.wit.com/log" +) var msecDelay int = 20 // check every 20 milliseconds @@ -39,7 +39,7 @@ func (cmd *Shell) Run(cmdline string) string { } func (cmd *Shell) InitProcess(cmdline string) { - log.Println("shell.InitProcess() START " + cmdline) + log.Log(RUN, "shell.InitProcess() START " + cmdline) cmd.Cmdline = Chomp(cmdline) // this is like 'chomp' in perl cmdArgs := strings.Fields(cmd.Cmdline) @@ -50,7 +50,7 @@ func (cmd *Shell) InitProcess(cmdline string) { } if (cmdArgs[0] == "cd") { if (len(cmdArgs) > 1) { - log.Println("os.Chdir()", cmd) + log.Log(RUN, "os.Chdir()", cmd) os.Chdir(cmdArgs[1]) } handleError(nil, 0) @@ -85,7 +85,7 @@ func (cmd *Shell) FileCreate(out string) { // NOTE: this might cause problems: // always remove the newlines at the end ? func (cmd *Shell) Exec(cmdline string) { - log.Println("shell.Run() START " + cmdline) + log.Log(RUN, "shell.Run() START " + cmdline) cmd.InitProcess(cmdline) if (cmd.Error != nil) { @@ -114,25 +114,25 @@ func (cmd *Shell) Exec(cmdline string) { if (err != nil) { cmd.Fail = true cmd.Error = err - log.Println("process.Wait() END err =", err.Error()) + log.Log(RUN, "process.Wait() END err =", err.Error()) } else { - log.Println("process.Wait() END") + log.Log(RUN, "process.Wait() END") } return } // nonblocking read until file errors func (cmd *Shell) Capture(f *File) { - // log.Debugln("nbrREADER() START") + log.Log(RUN, "nbrREADER() START") if (cmd.Buffer == nil) { cmd.Buffer = new(bytes.Buffer) } if (cmd.Buffer == nil) { - // log.Debugln("f.Buffer == nil") - // log.Debugln("SHOULD DIE HERE") f.Dead = false cmd.Error = fmt.Errorf("could not make buffer") + log.Error(cmd.Error, "f.Buffer == nil") + log.Error(cmd.Error, "SHOULD DIE HERE") cmd.Done = true } @@ -155,7 +155,7 @@ func (cmd *Shell) Capture(f *File) { // returns true if filehandle buffer is empty func (cmd *Shell) ReadToBuffer(f *File) bool { - // log.Debugln("ReadToBuffer() START") + log.Log(RUN, "ReadToBuffer() START") nbr := f.Fnbreader oneByte := make([]byte, 1024) if (nbr == nil) { diff --git a/ssh.go b/ssh.go index 18f6816..7882ada 100644 --- a/ssh.go +++ b/ssh.go @@ -1,19 +1,14 @@ package shell -import "log" -import "fmt" -import "io/ioutil" -import "time" +import ( + "fmt" + "io/ioutil" + "time" -// import "strings" -// import "path/filepath" -// import "os" -// import "bufio" -// import "os/user" -// import "runtime" - -import "golang.org/x/crypto/ssh" -import "github.com/tmc/scp" + "golang.org/x/crypto/ssh" + "github.com/tmc/scp" + "go.wit.com/log" +) var sshHostname string var sshPort int @@ -30,32 +25,32 @@ func SSHclientSet(hostname string, port int, username string, pass string, keyfi } func SSHclientSCP(localfile string, remotefile string) { - log.Println("shell.SSHclientSCP() START") - log.Println("shell.SSHclientSCP() \tlocalfile =", localfile) - log.Println("shell.SSHclientSCP() \tremotefile =", remotefile) - sess := SSH(sshHostname, sshPort, sshUsername, sshPassword, sshKeyfile) + log.Log(SSH, "shell.SSHclientSCP() START") + log.Log(SSH, "shell.SSHclientSCP() \tlocalfile =", localfile) + log.Log(SSH, "shell.SSHclientSCP() \tremotefile =", remotefile) + sess := mySsh(sshHostname, sshPort, sshUsername, sshPassword, sshKeyfile) err := scp.CopyPath(localfile, remotefile, sess) sess.Close() - log.Println("shell.SSHclientSCP() \tscp.CopyPath() err =", err) - log.Println("shell.SSHclientSCP() END") + log.Log(SSH, "shell.SSHclientSCP() \tscp.CopyPath() err =", err) + log.Log(SSH, "shell.SSHclientSCP() END") } func SSHclientRun(cmd string) { - log.Println("shell.SSHclientRun() START cmd =", cmd) - sess := SSH(sshHostname, sshPort, sshUsername, sshPassword, sshKeyfile) + log.Log(SSH, "shell.SSHclientRun() START cmd =", cmd) + sess := mySsh(sshHostname, sshPort, sshUsername, sshPassword, sshKeyfile) err := sess.Run(cmd) sess.Close() - log.Println("shell.SSHclientRun() END err =", err) + log.Log(SSH, "shell.SSHclientRun() END err =", err) } -func SSH(hostname string, port int, username string, pass string, keyfile string) *ssh.Session { +func mySsh(hostname string, port int, username string, pass string, keyfile string) *ssh.Session { // get host public key // hostKey := getHostKey(host) - // log.Println("hostkey =", hostKey) + // log.Log(SSH, "hostkey =", hostKey) publicKey, err := PublicKeyFile(keyfile) if (err != nil) { - log.Println("PublicKeyFile() error =", err) + log.Log(SSH, "PublicKeyFile() error =", err) } // ssh client config @@ -87,14 +82,14 @@ func SSH(hostname string, port int, username string, pass string, keyfile string // connect client, err := ssh.Dial("tcp", hostname+":"+sport, &config) if err != nil { - log.Fatal(err) + log.Error(err) } // defer client.Close() // start session sess, err := client.NewSession() if err != nil { - log.Fatal(err) + log.Error(err) } // defer sess.Close() @@ -103,12 +98,12 @@ func SSH(hostname string, port int, username string, pass string, keyfile string func Scp(sess *ssh.Session, localfile string, remotefile string) { err := scp.CopyPath(localfile, remotefile, sess) - log.Println("scp.CopyPath() err =", err) + log.Log(SSH, "scp.CopyPath() err =", err) } func PublicKeyFile(file string) (ssh.AuthMethod, error) { buffer, err := ioutil.ReadFile(file) - log.Println("buffer =", string(buffer)) + log.Log(SSH, "buffer =", string(buffer)) if err != nil { return nil, err } @@ -150,10 +145,10 @@ func getHostKey(host string) ssh.PublicKey { // 9enFJdMhb8eHN/6qfHSU/jww2Mo=|pcsWQCvAyve9QXBhjL+w/LhkcHU= ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBMQx8BJXxD+vk3wyjy7Irzw4FA6xxJvqUP7Hb+Z+ygpOuidYj9G8x6gHEXFUnABn5YirePrWh5tNsk4Rqs48VwU= hostKey, _, _, _, err = ssh.ParseAuthorizedKey([]byte("9enFJdMhb8eHN/6qfHSU/jww2Mo=|pcsWQCvAyve9QXBhjL+w/LhkcHU= ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBMQx8BJXxD+vk3wyjy7Irzw4FA6xxJvqUP7Hb+Z+ygpOuidYj9G8x6gHEXFUnABn5YirePrWh5tNsk4Rqs48VwU=")) - log.Println("hostkey err =", err) - log.Println("hostkey =", hostKey) + log.Log(SSH, "hostkey err =", err) + log.Log(SSH, "hostkey =", hostKey) if hostKey == nil { - log.Println("no hostkey found err =", err) + log.Log(SSH, "no hostkey found err =", err) log.Fatalf("no hostkey found for %s", host) }