通过ssh通道去扫描容器内都除了22端口,都开放了哪些
(clientConn *ssh.Client)
| 148 | |
| 149 | // 通过ssh通道去扫描容器内都除了22端口,都开放了哪些 |
| 150 | func scanServerPorts(clientConn *ssh.Client) []int { |
| 151 | session, err := clientConn.NewSession() |
| 152 | if err != nil { |
| 153 | clientConn.Close() |
| 154 | } |
| 155 | |
| 156 | // 在ssh主机上执行命令 |
| 157 | /* tmpSesssion, _ := clientConn.NewSession() |
| 158 | tmpSesssion.Run(`apt update |
| 159 | apt install net-tools`) */ |
| 160 | // https://www.tecmint.com/install-netstat-in-linux/ |
| 161 | cmd := `netstat -tunlp` |
| 162 | out, err := session.CombinedOutput(cmd) |
| 163 | outContent := string(out) |
| 164 | common.CheckError(err) |
| 165 | |
| 166 | // 分析命令行打印的结果,提取端口 |
| 167 | var ports []int |
| 168 | //common.SmartIDELog.Debug(outContent) |
| 169 | netstatInfoArray := parseCmdOutput(outContent) |
| 170 | for _, netstatInfo := range netstatInfoArray { |
| 171 | if netstatInfo.Port != model.CONST_Container_WebIDEPort && |
| 172 | netstatInfo.Port != model.CONST_Container_JetBrainsIDEPort && |
| 173 | netstatInfo.Port != model.CONST_Container_OpensumiIDEPort && |
| 174 | netstatInfo.Host != "127.0.0.1" && |
| 175 | netstatInfo.Port != model.CONST_Container_SSHPort && |
| 176 | netstatInfo.PID != "" && |
| 177 | !common.Contains4Int(ports, netstatInfo.Port) { |
| 178 | |
| 179 | // 防止重复的debug |
| 180 | if statOutput != outContent { |
| 181 | statOutput = outContent |
| 182 | common.SmartIDELog.Debug(outContent) |
| 183 | } |
| 184 | |
| 185 | ports = append(ports, netstatInfo.Port) |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | return ports |
| 190 | } |
| 191 | |
| 192 | var statOutput string = "" |
| 193 |
no test coverage detected