github.com/alibaba/sealer@v0.8.6-0.20220430115802-37a2bdaa8173/utils/ssh/sshcmd.go (about) 1 // Copyright © 2021 Alibaba Group Holding Ltd. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package ssh 16 17 import ( 18 "fmt" 19 "os/exec" 20 "strings" 21 22 "github.com/alibaba/sealer/common" 23 "github.com/alibaba/sealer/utils" 24 ) 25 26 const SUDO = "sudo " 27 28 func (s *SSH) Ping(host string) error { 29 if utils.IsLocalIP(host, s.LocalAddress) { 30 return nil 31 } 32 client, _, err := s.Connect(host) 33 if err != nil { 34 return fmt.Errorf("[ssh %s]create ssh session failed, %v", host, err) 35 } 36 err = client.Close() 37 if err != nil { 38 return err 39 } 40 return nil 41 } 42 43 func (s *SSH) CmdAsync(host string, cmds ...string) error { 44 var execFunc func(cmd string) error 45 46 if utils.IsLocalIP(host, s.LocalAddress) { 47 execFunc = func(cmd string) error { 48 c := exec.Command("/bin/sh", "-c", cmd) 49 stdout, err := c.StdoutPipe() 50 if err != nil { 51 return err 52 } 53 54 stderr, err := c.StderrPipe() 55 if err != nil { 56 return err 57 } 58 59 if err := c.Start(); err != nil { 60 return fmt.Errorf("failed to start command %s: %v", cmd, err) 61 } 62 63 ReadPipe(stdout, stderr, s.IsStdout) 64 65 err = c.Wait() 66 if err != nil { 67 return fmt.Errorf("failed to execute command(%s) on host(%s): error(%v)", cmd, host, err) 68 } 69 return nil 70 } 71 } else { 72 execFunc = func(cmd string) error { 73 client, session, err := s.Connect(host) 74 if err != nil { 75 return fmt.Errorf("failed to create ssh session for %s: %v", host, err) 76 } 77 defer client.Close() 78 defer session.Close() 79 stdout, err := session.StdoutPipe() 80 if err != nil { 81 return fmt.Errorf("failed to create stdout pipe for %s: %v", host, err) 82 } 83 stderr, err := session.StderrPipe() 84 if err != nil { 85 return fmt.Errorf("failed to create stderr pipe for %s: %v", host, err) 86 } 87 88 if err := session.Start(cmd); err != nil { 89 return fmt.Errorf("failed to start command %s on %s: %v", cmd, host, err) 90 } 91 92 ReadPipe(stdout, stderr, s.IsStdout) 93 94 err = session.Wait() 95 if err != nil { 96 return fmt.Errorf("failed to execute command(%s) on host(%s): error(%v)", cmd, host, err) 97 } 98 99 return nil 100 } 101 } 102 103 for _, cmd := range cmds { 104 if cmd == "" { 105 continue 106 } 107 if s.User != common.ROOT { 108 cmd = SUDO + cmd 109 } 110 if err := execFunc(cmd); err != nil { 111 return err 112 } 113 } 114 115 return nil 116 } 117 118 func (s *SSH) Cmd(host, cmd string) ([]byte, error) { 119 if s.User != common.ROOT { 120 cmd = SUDO + cmd 121 } 122 if utils.IsLocalIP(host, s.LocalAddress) { 123 return exec.Command("/bin/sh", "-c", cmd).CombinedOutput() 124 } 125 126 client, session, err := s.Connect(host) 127 if err != nil { 128 return nil, fmt.Errorf("[ssh][%s] create ssh session failed, %s", host, err) 129 } 130 defer client.Close() 131 defer session.Close() 132 b, err := session.CombinedOutput(cmd) 133 if err != nil { 134 return b, fmt.Errorf("[ssh][%s]run command failed [%s]", host, cmd) 135 } 136 137 return b, nil 138 } 139 140 //CmdToString is in host exec cmd and replace to spilt str 141 func (s *SSH) CmdToString(host, cmd, split string) (string, error) { 142 data, err := s.Cmd(host, cmd) 143 str := string(data) 144 if err != nil { 145 return str, fmt.Errorf("exec command failed %s %s %v", host, cmd, err) 146 } 147 if data != nil { 148 str = strings.ReplaceAll(str, "\r\n", split) 149 str = strings.ReplaceAll(str, "\n", split) 150 return str, nil 151 } 152 return str, fmt.Errorf("command %s %s return nil", host, cmd) 153 }