12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273 |
- package cluster
- import (
- "bytes"
- "fmt"
- "log"
- "net"
- "golang.org/x/crypto/ssh"
- )
- func ssHConnect(user, password, host string, port int) (*ssh.Session, error) {
- var (
- auth []ssh.AuthMethod
- addr string
- clientConfig *ssh.ClientConfig
- client *ssh.Client
- session *ssh.Session
- err error
- )
- // get auth method
- auth = make([]ssh.AuthMethod, 0)
- auth = append(auth, ssh.Password(password))
- hostKeyCallbk := func(hostname string, remote net.Addr, key ssh.PublicKey) error {
- return nil
- }
- clientConfig = &ssh.ClientConfig{
- User: user,
- Auth: auth,
- // Timeout: 30 * time.Second,
- HostKeyCallback: hostKeyCallbk,
- }
- // connet to ssh
- addr = fmt.Sprintf("%s:%d", host, port)
- if client, err = ssh.Dial("tcp", addr, clientConfig); err != nil {
- return nil, err
- }
- // create session
- if session, err = client.NewSession(); err != nil {
- return nil, err
- }
- return session, nil
- }
- // wget https://www.jianyu360.com/upload/extract_v3.tgz
- var sshstr = `
- #!/bin/bash
- cd /opt
- kill -9 $(pidof extract_fbs)
- rm -rf extract_fbs*
- wget http://172.17.162.36:9080/res/extract_fbs.tgz
- tar -xzvf extract_fbs.tgz
- cd /opt/extract_fbs
- chmod 777 extract_fbs
- nohup ./extract_fbs >/opt/extract_fbs/nohup 2>&1 &
- exit
- `
- func RunSsh(ip, password string, port int) {
- var stdOut, stdErr bytes.Buffer
- session, err := ssHConnect("root", password, ip, port)
- if err != nil {
- log.Println(err)
- }
- defer session.Close()
- session.Stdout = &stdOut
- session.Stderr = &stdErr
- //session.Run("wget https://www.jianyu360.com/upload/2017/04/22/201704221113374530.jpg")
- session.Run(sshstr)
- log.Println("ok")
- }
|