commit
d3cc885465
@ -0,0 +1,9 @@
|
||||
.idea
|
||||
go.sum
|
||||
main.exe
|
||||
log.txt
|
||||
logs
|
||||
*.log
|
||||
uploads
|
||||
gin.log
|
||||
nogit_test.go
|
||||
@ -0,0 +1,8 @@
|
||||
module gssh
|
||||
|
||||
go 1.19
|
||||
|
||||
require (
|
||||
golang.org/x/crypto v0.0.0-20220722155217-630584e8d5aa // indirect
|
||||
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1 // indirect
|
||||
)
|
||||
@ -0,0 +1,70 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"golang.org/x/crypto/ssh"
|
||||
"os/user"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
var currentUser string
|
||||
|
||||
func init() {
|
||||
user, err := user.Current()
|
||||
if err != nil {
|
||||
currentUser = "root"
|
||||
return
|
||||
}
|
||||
currentUser = user.Username
|
||||
|
||||
}
|
||||
func main() {
|
||||
host := flag.String("h", "", "server host ip")
|
||||
port := flag.Int("p", 22, "sshd port")
|
||||
user := flag.String("u", currentUser, "sshd username")
|
||||
password := flag.String("p", "", "sshd password")
|
||||
cmd := flag.String("c", "ls", "sshd command")
|
||||
flag.Parse()
|
||||
sshClient, err := getClient(*host, *port, *user, *password)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
defer sshClient.Close()
|
||||
res, err := SSH(sshClient, *cmd)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
fmt.Println(res)
|
||||
|
||||
}
|
||||
|
||||
func getClient(host string, port int, username, password string) (*ssh.Client, error) {
|
||||
config := &ssh.ClientConfig{
|
||||
User: username,
|
||||
Auth: []ssh.AuthMethod{
|
||||
ssh.Password(password),
|
||||
},
|
||||
HostKeyCallback: ssh.InsecureIgnoreHostKey(), // 忽略主机密钥验证
|
||||
}
|
||||
client, err := ssh.Dial("tcp", host+":"+strconv.Itoa(port), config)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return client, nil
|
||||
}
|
||||
func SSH(sshClient *ssh.Client, cmd string) (string, error) {
|
||||
session, err := sshClient.NewSession()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer session.Close()
|
||||
var b []byte
|
||||
b, err = session.CombinedOutput(cmd)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return string(b), nil
|
||||
}
|
||||
@ -0,0 +1,25 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestSSH(tt *testing.T) {
|
||||
host := "172.16.0.5"
|
||||
port := "22"
|
||||
user := "root"
|
||||
passwd := "123456"
|
||||
cmd := "top -c -b -n 1"
|
||||
sshClient, err := getClient(host, port, user, passwd)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
defer sshClient.Close()
|
||||
res, err := SSH(sshClient, cmd)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
fmt.Println(res)
|
||||
|
||||
}
|
||||
@ -0,0 +1,14 @@
|
||||
ssh in golang
|
||||
|
||||
## build arm64 on oracle
|
||||
```bash
|
||||
cd /root
|
||||
rm -rf gssh
|
||||
git clone https://git.sre.ink/go/gssh.git
|
||||
cd gssh
|
||||
CGO_ENABLED=0 GOOS=linux GOARCH=arm64 GO111MODULE=on go mod tidy
|
||||
CGO_ENABLED=0 GOOS=linux GOARCH=arm64 GO111MODULE=on go build -v -a -o gssh main.go
|
||||
rm -rf /usr/bin/gssh
|
||||
mv gssh /usr/bin/
|
||||
gssh -h 127.0.0.1 -p 22 -u root -p 123456 -c ls
|
||||
```
|
||||
Loading…
Reference in new issue