|
| 1 | +package SSHCommand |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "github.com/labstack/gommon/log" |
| 7 | + "golang.org/x/crypto/ssh" |
| 8 | + "io" |
| 9 | + "os" |
| 10 | +) |
| 11 | + |
| 12 | +type ScutInfo struct { |
| 13 | + Username, Password string |
| 14 | + AcIP, AcName string |
| 15 | +} |
| 16 | + |
| 17 | +type SSHClass struct { |
| 18 | + client *ssh.Client |
| 19 | + session *ssh.Session |
| 20 | + err error |
| 21 | + status bool // 记录上一操作是否成功 |
| 22 | + out io.Writer // 用于保存输出SSH运行结果 |
| 23 | + ScutInfo // 保存信息 |
| 24 | +} |
| 25 | + |
| 26 | +func (s *SSHClass) SetOut(out io.Writer) { |
| 27 | + // 先于 NewSession 被调用 |
| 28 | + s.out = out |
| 29 | +} |
| 30 | + |
| 31 | +func (s *SSHClass) NewSession() { |
| 32 | + // 每条命令都需要一个新 Session |
| 33 | + if s.status != true { |
| 34 | + log.Errorf("Dial Fails: %s", s.err.Error()) |
| 35 | + return |
| 36 | + } |
| 37 | + s.session, s.err = s.client.NewSession() |
| 38 | + s.Assert() |
| 39 | + |
| 40 | + // 设置输出 |
| 41 | + s.session.Stdout = s.out |
| 42 | +} |
| 43 | + |
| 44 | +func (s *SSHClass) Close() { |
| 45 | + if s.session != nil { |
| 46 | + s.session.Close() |
| 47 | + s.session = nil |
| 48 | + s.status = true |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +func (s *SSHClass) Assert() { |
| 53 | + if s.err != nil { |
| 54 | + log.Error(s.err.Error()) |
| 55 | + s.status = false |
| 56 | + return |
| 57 | + } |
| 58 | + s.status = true |
| 59 | +} |
| 60 | + |
| 61 | +func (s *SSHClass) RunCommand(cmd string) { |
| 62 | + s.NewSession() |
| 63 | + defer s.Close() |
| 64 | + |
| 65 | + if s.status != true { |
| 66 | + log.Errorf("New Session Fails: %s", s.err.Error()) |
| 67 | + return |
| 68 | + } |
| 69 | + err := s.session.Run(cmd) |
| 70 | + if err != nil { |
| 71 | + log.Error(err.Error()) |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +type accountInfo struct { |
| 76 | + Username, Password string |
| 77 | + AcIP, AcName string |
| 78 | +} |
| 79 | + |
| 80 | +func (s *SSHClass) preLoadInfo() { |
| 81 | + // 遇加载信息 |
| 82 | + infoFile, err := os.ReadFile("./Account.json") |
| 83 | + if err != nil { |
| 84 | + log.Info(err.Error()) |
| 85 | + return |
| 86 | + } |
| 87 | + accInfo := new(accountInfo) |
| 88 | + json.Unmarshal(infoFile, accInfo) |
| 89 | + |
| 90 | + s.Username = accInfo.Username |
| 91 | + s.Password = accInfo.Password |
| 92 | + s.AcIP = accInfo.AcIP |
| 93 | + s.AcName = accInfo.AcName |
| 94 | +} |
| 95 | + |
| 96 | +func (s *SSHClass) saveInfo() { |
| 97 | + infoFile, err := os.Create("./Account.json") |
| 98 | + if err != nil { |
| 99 | + log.Error(err.Error()) |
| 100 | + } |
| 101 | + |
| 102 | + accInfo := new(accountInfo) |
| 103 | + accInfo.Username = s.Username |
| 104 | + accInfo.Password = s.Password |
| 105 | + accInfo.AcIP = s.AcIP |
| 106 | + accInfo.AcName = s.AcName |
| 107 | + |
| 108 | + b, err := json.Marshal(accInfo) |
| 109 | + if err != nil { |
| 110 | + log.Error(err.Error()) |
| 111 | + } |
| 112 | + infoFile.Write(b) |
| 113 | + infoFile.Close() |
| 114 | +} |
| 115 | + |
| 116 | +func (s *SSHClass) RunSetScutInfo(username, password string) { |
| 117 | + log.Info("RunSetScutInfo is called") |
| 118 | + s.preLoadInfo() |
| 119 | + |
| 120 | + s.Username = username |
| 121 | + s.Password = password |
| 122 | + s.HelpSetInfo() |
| 123 | +} |
| 124 | + |
| 125 | +func (s *SSHClass) RunSetAcInfo(acIP, acName string) { |
| 126 | + log.Info("RunSetAcInfo is called") |
| 127 | + s.preLoadInfo() |
| 128 | + |
| 129 | + s.AcIP = acIP |
| 130 | + s.AcName = acName |
| 131 | + s.HelpSetInfo() |
| 132 | +} |
| 133 | + |
| 134 | +func (s *SSHClass) RunWiredLogin() { |
| 135 | + cmd := fmt.Sprintf("chmod +x scutlogin.sh && ./scutlogin.sh") |
| 136 | + log.Info("RunWiredLogin is called") |
| 137 | + s.RunCommand(cmd) |
| 138 | +} |
| 139 | + |
| 140 | +func (s *SSHClass) RunWirelessLogin() { |
| 141 | + cmd := fmt.Sprintf("chmod +x wl_login.sh && ./wl_login.sh") |
| 142 | + log.Info("RunWirelessLogin is called") |
| 143 | + s.RunCommand(cmd) |
| 144 | +} |
| 145 | + |
| 146 | +func (s *SSHClass) HelpSetInfo() { |
| 147 | + // 辅助函数 |
| 148 | + s.saveInfo() |
| 149 | + |
| 150 | + cmd := fmt.Sprintf("echo %s > SCUT_info.txt", s.Username) |
| 151 | + cmd += fmt.Sprintf(" && echo %s >> SCUT_info.txt", s.Password) |
| 152 | + cmd += fmt.Sprintf(" && echo %s >> SCUT_info.txt", s.AcIP) |
| 153 | + cmd += fmt.Sprintf(" && echo %s >> SCUT_info.txt", s.AcName) |
| 154 | + s.RunCommand(cmd) |
| 155 | +} |
| 156 | + |
| 157 | +func (s *SSHClass) RunSyncTime() { |
| 158 | + cmd := fmt.Sprintf("ntpd -n -q -p ntp.aliyun.com") |
| 159 | + |
| 160 | + log.Info("RunSyncTime is called") |
| 161 | + s.RunCommand(cmd) |
| 162 | +} |
| 163 | + |
| 164 | +func (s *SSHClass) RunAutoLogin() { |
| 165 | + log.Info("RunAutoLogin is called") |
| 166 | + |
| 167 | + cmd := fmt.Sprintf("crontab -l | sed '/.*\\/root\\/scutlogin.sh.*/d' > temp_cron") |
| 168 | + cmd += fmt.Sprintf(" && echo \"5 6 * * 1-5 /root/scutlogin.sh\" >> temp_cron") |
| 169 | + cmd += fmt.Sprintf(" && crontab temp_cron\nrm temp_cron") |
| 170 | + |
| 171 | + s.RunCommand(cmd) |
| 172 | +} |
| 173 | + |
| 174 | +func (s *SSHClass) CancelAutoLogin() { |
| 175 | + log.Info("CancelAutoLogin is called") |
| 176 | + |
| 177 | + cmd := fmt.Sprintf("crontab -l | sed '/.*\\/root\\/scutlogin.sh.*/d' > temp_cron") |
| 178 | + cmd += fmt.Sprintf(" && crontab temp_cron\nrm temp_cron") |
| 179 | + s.RunCommand(cmd) |
| 180 | +} |
| 181 | + |
| 182 | +type routerConfig struct { |
| 183 | + Addr string `json:"addr"` |
| 184 | + Password string `json:"password"` |
| 185 | + Port string `json:"port"` |
| 186 | +} |
| 187 | + |
| 188 | +func NewRunSSH() *SSHClass { |
| 189 | + buf, err := os.ReadFile("./config.json") |
| 190 | + if err != nil { |
| 191 | + log.Error(err.Error()) |
| 192 | + f, _ := os.Create("./config.json") |
| 193 | + f.Close() |
| 194 | + return nil |
| 195 | + } |
| 196 | + |
| 197 | + rConfig := new(routerConfig) |
| 198 | + err = json.Unmarshal(buf, rConfig) |
| 199 | + if err != nil { |
| 200 | + log.Error(err.Error()) |
| 201 | + return nil |
| 202 | + } |
| 203 | + |
| 204 | + config := ssh.ClientConfig{ |
| 205 | + Config: ssh.Config{}, |
| 206 | + User: "root", |
| 207 | + Auth: []ssh.AuthMethod{ssh.Password(rConfig.Password)}, |
| 208 | + HostKeyCallback: ssh.InsecureIgnoreHostKey(), |
| 209 | + } |
| 210 | + SSHObj := new(SSHClass) |
| 211 | + addrPort := fmt.Sprintf("%s:%s", rConfig.Addr, rConfig.Port) |
| 212 | + SSHObj.client, SSHObj.err = ssh.Dial("tcp", addrPort, &config) |
| 213 | + SSHObj.Assert() |
| 214 | + |
| 215 | + return SSHObj |
| 216 | +} |
0 commit comments