|
|
@ -20,13 +20,13 @@ type ConnInfo struct {
|
|
|
|
PrivateKey []byte `json:"privateKey"`
|
|
|
|
PrivateKey []byte `json:"privateKey"`
|
|
|
|
PassPhrase []byte `json:"passPhrase"`
|
|
|
|
PassPhrase []byte `json:"passPhrase"`
|
|
|
|
DialTimeOut time.Duration `json:"dialTimeOut"`
|
|
|
|
DialTimeOut time.Duration `json:"dialTimeOut"`
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Client *gossh.Client `json:"client"`
|
|
|
|
type SSHClient struct {
|
|
|
|
Session *gossh.Session `json:"session"`
|
|
|
|
Client *gossh.Client `json:"client"`
|
|
|
|
LastResult string `json:"lastResult"`
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (c *ConnInfo) NewClient() (*ConnInfo, error) {
|
|
|
|
func NewClient(c ConnInfo) (*SSHClient, error) {
|
|
|
|
if strings.Contains(c.Addr, ":") {
|
|
|
|
if strings.Contains(c.Addr, ":") {
|
|
|
|
c.Addr = fmt.Sprintf("[%s]", c.Addr)
|
|
|
|
c.Addr = fmt.Sprintf("[%s]", c.Addr)
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -55,18 +55,12 @@ func (c *ConnInfo) NewClient() (*ConnInfo, error) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
client, err := gossh.Dial(proto, addr, config)
|
|
|
|
client, err := gossh.Dial(proto, addr, config)
|
|
|
|
if nil != err {
|
|
|
|
if nil != err {
|
|
|
|
return c, err
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
c.Client = client
|
|
|
|
return &SSHClient{Client: client}, nil
|
|
|
|
return c, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (c *ConnInfo) Run(shell string) (string, error) {
|
|
|
|
func (c *SSHClient) Run(shell string) (string, error) {
|
|
|
|
if c.Client == nil {
|
|
|
|
|
|
|
|
if _, err := c.NewClient(); err != nil {
|
|
|
|
|
|
|
|
return "", err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
session, err := c.Client.NewSession()
|
|
|
|
session, err := c.Client.NewSession()
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
return "", err
|
|
|
@ -74,11 +68,10 @@ func (c *ConnInfo) Run(shell string) (string, error) {
|
|
|
|
defer session.Close()
|
|
|
|
defer session.Close()
|
|
|
|
buf, err := session.CombinedOutput(shell)
|
|
|
|
buf, err := session.CombinedOutput(shell)
|
|
|
|
|
|
|
|
|
|
|
|
c.LastResult = string(buf)
|
|
|
|
return string(buf), err
|
|
|
|
return c.LastResult, err
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (c *ConnInfo) Close() {
|
|
|
|
func (c *SSHClient) Close() {
|
|
|
|
_ = c.Client.Close()
|
|
|
|
_ = c.Client.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -88,7 +81,7 @@ type SshConn struct {
|
|
|
|
Session *gossh.Session
|
|
|
|
Session *gossh.Session
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (c *ConnInfo) NewSshConn(cols, rows int) (*SshConn, error) {
|
|
|
|
func (c *SSHClient) NewSshConn(cols, rows int) (*SshConn, error) {
|
|
|
|
sshSession, err := c.Client.NewSession()
|
|
|
|
sshSession, err := c.Client.NewSession()
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
return nil, err
|
|
|
|