package main
import (
"bytes"
"fmt"
"go-test/src/model"
"golang.org/x/crypto/ssh"
"log"
)
func sshAuthMethod(passwd, pkFile, pkPasswd string) (auth []ssh.AuthMethod) {
// pkfile存在, 就进行密钥验证, 如果不存在,则跳过密钥验证。
// 密码不为空, 则添加密码验证。
if passwd != "" {
auth = append(auth, ssh.Password(passwd))
}
return auth
}
func main() {
model.DbInit("conf.toml.template")
sshConfig := ssh.Config{
Ciphers: []string{"aes128-ctr", "aes192-ctr", "aes256-ctr", "aes128-gcm@openssh.com", "arcfour256", "arcfour128", "aes128-cbc", "3des-cbc", "aes192-cbc", "aes256-cbc"},
}
//DefaultTimeout := time.Duration(1) * time.Minute
config := ssh.ClientConfig{
User: "root",
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
//Timeout: DefaultTimeout, 默认0即可
Config: sshConfig,
Auth: sshAuthMethod("xxxxx@","",""),
}
client, err2 := ssh.Dial("tcp", "192.168.9.107:22", &config)
if err2 != nil{
fmt.Println(err2)
}
defer client.Close()
session,err4:=client.NewSession()
if err4!= nil{
fmt.Println(err4)
}
defer session.Close()
var b bytes.Buffer //优先RUN之前
session.Stdout = &b
if err := session.Run("ifconfig"); err != nil {
log.Fatal("Failed to run: " + err.Error())
}
fmt.Println(b.String())
}