Compare commits

..

No commits in common. "master" and "0.3.0" have entirely different histories.

7 changed files with 81 additions and 109 deletions

View file

@ -49,8 +49,6 @@ Usage of ./multissh:
-j print output in json format -j print output in json format
-k string -k string
ssh private key ssh private key
-keyexchanges string
keyexchanges
-l In linux mode,multi command combine with && ,such as date&&cd /opt&&ls -l In linux mode,multi command combine with && ,such as date&&cd /opt&&ls
-n int -n int
max execute number (default 20) max execute number (default 20)
@ -84,12 +82,13 @@ show clock
**ssh.json 示例** **ssh.json 示例**
``` ```
{ {
"SshHosts": [{ "SshHosts": [
{
"Host": "192.168.31.51", "Host": "192.168.31.51",
"Port": 22, "Port": 22,
"Username": "admin", "Username": "admin",
"Password": "admin", "Password": "admin",
"cmds": "show clock;show clock" "cmds":"show clock;show clock"
}, },
{ {
"Host": "192.168.80.131", "Host": "192.168.80.131",
@ -100,12 +99,7 @@ show clock
"linuxMode": true, "linuxMode": true,
"CmdFile": "cmd2.txt.example" "CmdFile": "cmd2.txt.example"
} }
], ]
"Global": {
"Ciphers": "aes128-ctr,aes192-ctr,aes256-ctr,aes128-cbc,3des-cbc",
"KeyExchanges": "diffie-hellman-group1-sha1,curve25519-sha256@libssh.org,ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521,diffie-hellman-group-exchange-sha256,diffie-hellman-group14-sha1"
}
} }
``` ```

View file

@ -16,13 +16,9 @@ const (
key = "../server.key" key = "../server.key"
) )
// Tests the SSH functionality of the package.
//
// It requires manual input of the local SSH private key path into the key
// variable, and the remote address into the ip variable.
func Test_SSH(t *testing.T) { func Test_SSH(t *testing.T) {
var cipherList []string var cipherList []string
session, err := connect(username, password, ip, key, port, cipherList, nil) session, err := connect(username, password, ip, key, port, cipherList)
if err != nil { if err != nil {
t.Error(err) t.Error(err)
return return

View file

@ -14,7 +14,7 @@ import (
"golang.org/x/crypto/ssh" "golang.org/x/crypto/ssh"
) )
func connect(user, password, host, key string, port int, cipherList, keyExchangeList []string) (*ssh.Session, error) { func connect(user, password, host, key string, port int, cipherList []string) (*ssh.Session, error) {
var ( var (
auth []ssh.AuthMethod auth []ssh.AuthMethod
addr string addr string
@ -45,16 +45,16 @@ func connect(user, password, host, key string, port int, cipherList, keyExchange
} }
auth = append(auth, ssh.PublicKeys(signer)) auth = append(auth, ssh.PublicKeys(signer))
} }
if len(cipherList) == 0 {
config.Ciphers = []string{"aes128-ctr", "aes192-ctr", "aes256-ctr", "aes128-gcm@openssh.com", "arcfour256", "arcfour128", "aes128-cbc", "3des-cbc", "aes192-cbc", "aes256-cbc"}
} else {
config.Ciphers = cipherList
}
if len(keyExchangeList) == 0 { if len(cipherList) == 0 {
config.KeyExchanges = []string{"diffie-hellman-group-exchange-sha1", "diffie-hellman-group1-sha1", "diffie-hellman-group-exchange-sha256"} config = ssh.Config{
Ciphers: []string{"aes128-ctr", "aes192-ctr", "aes256-ctr", "aes128-gcm@openssh.com", "arcfour256", "arcfour128", "aes128-cbc", "3des-cbc", "aes192-cbc", "aes256-cbc"},
KeyExchanges: []string{"diffie-hellman-group-exchange-sha1", "diffie-hellman-group1-sha1", "diffie-hellman-group-exchange-sha256"},
}
} else { } else {
config.KeyExchanges = keyExchangeList config = ssh.Config{
Ciphers: cipherList,
}
} }
clientConfig = &ssh.ClientConfig{ clientConfig = &ssh.ClientConfig{
@ -92,12 +92,12 @@ func connect(user, password, host, key string, port int, cipherList, keyExchange
return session, nil return session, nil
} }
func Dossh(username, password, host, key string, cmdlist []string, port, timeout int, cipherList, keyExchangeList []string, linuxMode bool, ch chan g.SSHResult) { func Dossh(username, password, host, key string, cmdlist []string, port, timeout int, cipherList []string, linuxMode bool, ch chan g.SSHResult) {
chSSH := make(chan g.SSHResult) chSSH := make(chan g.SSHResult)
if linuxMode { if linuxMode {
go dossh_run(username, password, host, key, cmdlist, port, cipherList, keyExchangeList, chSSH) go dossh_run(username, password, host, key, cmdlist, port, cipherList, chSSH)
} else { } else {
go dossh_session(username, password, host, key, cmdlist, port, cipherList, keyExchangeList, chSSH) go dossh_session(username, password, host, key, cmdlist, port, cipherList, chSSH)
} }
var res g.SSHResult var res g.SSHResult
@ -113,8 +113,8 @@ func Dossh(username, password, host, key string, cmdlist []string, port, timeout
return return
} }
func dossh_session(username, password, host, key string, cmdlist []string, port int, cipherList, keyExchangeList []string, ch chan g.SSHResult) { func dossh_session(username, password, host, key string, cmdlist []string, port int, cipherList []string, ch chan g.SSHResult) {
session, err := connect(username, password, host, key, port, cipherList, keyExchangeList) session, err := connect(username, password, host, key, port, cipherList)
var sshResult g.SSHResult var sshResult g.SSHResult
sshResult.Host = host sshResult.Host = host
@ -159,8 +159,8 @@ func dossh_session(username, password, host, key string, cmdlist []string, port
return return
} }
func dossh_run(username, password, host, key string, cmdlist []string, port int, cipherList, keyExchangeList []string, ch chan g.SSHResult) { func dossh_run(username, password, host, key string, cmdlist []string, port int, cipherList []string, ch chan g.SSHResult) {
session, err := connect(username, password, host, key, port, cipherList, keyExchangeList) session, err := connect(username, password, host, key, port, cipherList)
var sshResult g.SSHResult var sshResult g.SSHResult
sshResult.Host = host sshResult.Host = host

View file

@ -27,12 +27,6 @@ type SSHHost struct {
type HostJson struct { type HostJson struct {
SshHosts []SSHHost SshHosts []SSHHost
Global GlobalConfig
}
type GlobalConfig struct {
Ciphers string
KeyExchanges string
} }
type SSHResult struct { type SSHResult struct {
@ -42,9 +36,6 @@ type SSHResult struct {
} }
func SplitString(str string) (strList []string) { func SplitString(str string) (strList []string) {
if str == "" {
return
}
if strings.Contains(str, ",") { if strings.Contains(str, ",") {
strList = strings.Split(str, ",") strList = strings.Split(str, ",")
} else { } else {
@ -81,18 +72,20 @@ func Getfile(filePath string) ([]string, error) {
} }
//gu //gu
func GetJsonFile(filePath string) (HostJson, error) { func GetJsonFile(filePath string) ([]SSHHost, error) {
var result HostJson result := []SSHHost{}
b, err := ioutil.ReadFile(filePath) b, err := ioutil.ReadFile(filePath)
if err != nil { if err != nil {
log.Println("read file ", filePath, err) log.Println("read file ", filePath, err)
return result, err return result, err
} }
err = json.Unmarshal(b, &result) var m HostJson
err = json.Unmarshal(b, &m)
if err != nil { if err != nil {
log.Println("read file ", filePath, err) log.Println("read file ", filePath, err)
return result, err return result, err
} }
result = m.SshHosts
return result, nil return result, nil
} }
func WriteIntoTxt(sshResult SSHResult, locate string) error { func WriteIntoTxt(sshResult SSHResult, locate string) error {

View file

@ -9,5 +9,5 @@ package g
// json Unmarshal with error // json Unmarshal with error
// 0.2.3 // 0.2.3
const ( const (
VERSION = "0.4.0" VERSION = "0.3.0"
) )

13
main.go
View file

@ -23,7 +23,6 @@ func main() {
key := flag.String("k", "", "ssh private key") key := flag.String("k", "", "ssh private key")
port := flag.Int("port", 22, "ssh port") port := flag.Int("port", 22, "ssh port")
ciphers := flag.String("ciphers", "", "ciphers") ciphers := flag.String("ciphers", "", "ciphers")
keyExchanges := flag.String("keyexchanges", "", "keyexchanges")
cmdFile := flag.String("cmdfile", "", "cmdfile path") cmdFile := flag.String("cmdfile", "", "cmdfile path")
hostFile := flag.String("hostfile", "", "hostfile path") hostFile := flag.String("hostfile", "", "hostfile path")
ipFile := flag.String("ipfile", "", "ipfile path") ipFile := flag.String("ipfile", "", "ipfile path")
@ -37,7 +36,7 @@ func main() {
flag.Parse() flag.Parse()
var cmdList, hostList, cipherList, keyExchangeList []string var cmdList, hostList, cipherList []string
var err error var err error
sshHosts := []g.SSHHost{} sshHosts := []g.SSHHost{}
@ -89,9 +88,6 @@ func main() {
if *ciphers != "" { if *ciphers != "" {
cipherList = g.SplitString(*ciphers) cipherList = g.SplitString(*ciphers)
} }
if *keyExchanges != "" {
keyExchangeList = g.SplitString(*keyExchanges)
}
if *cfgFile == "" { if *cfgFile == "" {
for _, host := range hostList { for _, host := range hostList {
host_Struct.Host = host host_Struct.Host = host
@ -104,14 +100,11 @@ func main() {
sshHosts = append(sshHosts, host_Struct) sshHosts = append(sshHosts, host_Struct)
} }
} else { } else {
sshHostConfig, err := g.GetJsonFile(*cfgFile) sshHosts, err = g.GetJsonFile(*cfgFile)
if err != nil { if err != nil {
log.Println("load cfgFile error: ", err) log.Println("load cfgFile error: ", err)
return return
} }
cipherList = g.SplitString(sshHostConfig.Global.Ciphers)
keyExchangeList = g.SplitString(sshHostConfig.Global.KeyExchanges)
sshHosts = sshHostConfig.SshHosts
for i := 0; i < len(sshHosts); i++ { for i := 0; i < len(sshHosts); i++ {
if sshHosts[i].Cmds != "" { if sshHosts[i].Cmds != "" {
sshHosts[i].CmdList = g.SplitString(sshHosts[i].Cmds) sshHosts[i].CmdList = g.SplitString(sshHosts[i].Cmds)
@ -131,7 +124,7 @@ func main() {
startTime := time.Now() startTime := time.Now()
log.Println("Multissh start") log.Println("Multissh start")
limitFunc := func(chLimit chan bool, ch chan g.SSHResult, host g.SSHHost) { limitFunc := func(chLimit chan bool, ch chan g.SSHResult, host g.SSHHost) {
funcs.Dossh(host.Username, host.Password, host.Host, host.Key, host.CmdList, host.Port, *timeLimit, cipherList, keyExchangeList, host.LinuxMode, ch) funcs.Dossh(host.Username, host.Password, host.Host, host.Key, host.CmdList, host.Port, *timeLimit, cipherList, host.LinuxMode, ch)
<-chLimit <-chLimit
} }
for i, host := range sshHosts { for i, host := range sshHosts {

View file

@ -1,10 +1,11 @@
{ {
"SshHosts": [{ "SshHosts": [
{
"Host": "192.168.31.51", "Host": "192.168.31.51",
"Port": 22, "Port": 22,
"Username": "admin", "Username": "admin",
"Password": "admin", "Password": "admin",
"cmds": "show clock;show clock" "cmds":"show clock;show clock"
}, },
{ {
"Host": "192.168.80.131", "Host": "192.168.80.131",
@ -15,10 +16,5 @@
"linuxMode": true, "linuxMode": true,
"CmdFile": "cmd2.txt.example" "CmdFile": "cmd2.txt.example"
} }
], ]
"Global": {
"Ciphers": "aes128-ctr,aes192-ctr,aes256-ctr,aes128-cbc,3des-cbc",
"KeyExchanges": "diffie-hellman-group1-sha1,curve25519-sha256@libssh.org,ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521,diffie-hellman-group-exchange-sha256,diffie-hellman-group14-sha1"
}
} }