fix: 抓包连接方式改用网元信息配置的终端

This commit is contained in:
TsMask
2024-06-15 14:49:26 +08:00
parent 146ad4ec7d
commit 7e879a97f9
2 changed files with 35 additions and 26 deletions

View File

@@ -5,9 +5,7 @@ import (
"strings"
"time"
"be.ems/src/framework/config"
"be.ems/src/framework/logger"
"be.ems/src/framework/utils/cmd"
"be.ems/src/framework/utils/date"
neService "be.ems/src/modules/network_element/service"
)
@@ -28,23 +26,26 @@ type TcpdumpImpl struct {
// DumpStart 触发tcpdump开始抓包 filePcapName, err
func (s *TcpdumpImpl) DumpStart(neType, neId, cmdStr string) (string, error) {
// 检查网元信息
// 查询网元获取IP
neInfo := s.neInfoService.SelectNeInfoByNeTypeAndNeID(neType, neId)
if neInfo.NeId != neId {
if neInfo.NeId != neId || neInfo.IP == "" {
return "", fmt.Errorf("noData")
}
// SSH命令
usernameNe := config.Get("ne.user").(string) // 网元统一用户
sshHost := fmt.Sprintf("%s@%s", usernameNe, neInfo.IP)
// 网元主机的SSH客户端
sshClient, err := s.neInfoService.NeRunSSHClient(neInfo.NeType, neInfo.NeId)
if err != nil {
return "", err
}
defer sshClient.Close()
// 是否拥有sudo权限并拼接
withSudo := ""
if _, err := cmd.ExecWithCheck("ssh", sshHost, "sudo -n uname"); err == nil {
if _, err := sshClient.RunCMD("sudo -n uname"); err == nil {
withSudo = "sudo "
}
if msg, err := cmd.ExecWithCheck("ssh", sshHost, fmt.Sprintf("%s tcpdump --version", withSudo)); err != nil {
if msg, err := sshClient.RunCMD(fmt.Sprintf("%s tcpdump --version", withSudo)); err != nil {
// stderr: bash: tcpdump未找到命令 => exit status 127
msg = strings.TrimSpace(msg)
logger.Warnf("DumpStart err: %s => %s", msg, err.Error())
@@ -58,7 +59,7 @@ func (s *TcpdumpImpl) DumpStart(neType, neId, cmdStr string) (string, error) {
sendCmd := fmt.Sprintf("cd /tmp \n %s nohup timeout 30m tcpdump -i any %s -s0 -w %s.pcap > %s.log 2>&1 & \necho $!", withSudo, cmdStr, fileName, fileName)
// cd /tmp
// sudo nohup timeout 60m tcpdump -i any -n -s 0 -v -w -s0 -w 20240115140822_UDM_001.pcap > 20240115140822_UDM_001.log 2>&1 & echo $!
msg, err := cmd.ExecWithCheck("ssh", sshHost, sendCmd)
msg, err := sshClient.RunCMD(sendCmd)
msg = strings.TrimSpace(msg)
if err != nil || strings.HasPrefix(msg, "stderr:") {
logger.Warnf("DumpStart err: %s => %s", msg, err.Error())
@@ -73,19 +74,22 @@ func (s *TcpdumpImpl) DumpStart(neType, neId, cmdStr string) (string, error) {
// DumpStop 停止已存在抓包句柄
func (s *TcpdumpImpl) DumpStop(neType, neId, fileName string) (string, error) {
// 检查网元信息
// 查询网元获取IP
neInfo := s.neInfoService.SelectNeInfoByNeTypeAndNeID(neType, neId)
if neInfo.NeId != neId {
if neInfo.NeId != neId || neInfo.IP == "" {
return "", fmt.Errorf("noData")
}
// SSH命令
usernameNe := config.Get("ne.user").(string) // 网元统一用户
sshHost := fmt.Sprintf("%s@%s", usernameNe, neInfo.IP)
// 网元主机的SSH客户端
sshClient, err := s.neInfoService.NeRunSSHClient(neInfo.NeType, neInfo.NeId)
if err != nil {
return "", err
}
defer sshClient.Close()
// 是否拥有sudo权限并拼接
withSudo := ""
if _, err := cmd.ExecWithCheck("ssh", sshHost, "sudo -n uname"); err == nil {
if _, err := sshClient.RunCMD("sudo -n uname"); err == nil {
withSudo = "sudo "
}
@@ -104,7 +108,7 @@ func (s *TcpdumpImpl) DumpStop(neType, neId, fileName string) (string, error) {
// 拼装命令
sendCmd := fmt.Sprintf("cd /tmp \n %s kill %s %s", withSudo, pid, viewLogFile)
msg, err := cmd.ExecWithCheck("ssh", sshHost, sendCmd)
msg, err := sshClient.RunCMD(sendCmd)
delete(s.tcpdumpPIDMap, neTypeID)
if err != nil || strings.HasPrefix(msg, "stderr:") {
logger.Warnf("DumpStop err: %s => %s", strings.TrimSpace(msg), err.Error())
@@ -115,23 +119,26 @@ func (s *TcpdumpImpl) DumpStop(neType, neId, fileName string) (string, error) {
// DumpUPF UPF标准版抓包
func (s *TcpdumpImpl) DumpUPF(neType, neId, cmdStr string) (string, string, error) {
// 检查网元信息
// 查询网元获取IP
neInfo := s.neInfoService.SelectNeInfoByNeTypeAndNeID(neType, neId)
if neInfo.NeId != neId {
if neInfo.NeId != neId || neInfo.IP == "" {
return "", "", fmt.Errorf("noData")
}
// SSH命令
usernameNe := config.Get("ne.user").(string) // 网元统一用户
sshHost := fmt.Sprintf("%s@%s", usernameNe, neInfo.IP)
// 网元主机的SSH客户端
sshClient, err := s.neInfoService.NeRunSSHClient(neInfo.NeType, neInfo.NeId)
if err != nil {
return "", "", err
}
defer sshClient.Close()
// 是否拥有sudo权限并拼接
withSudo := ""
if _, err := cmd.ExecWithCheck("ssh", sshHost, "sudo -n uname"); err == nil {
if _, err := sshClient.RunCMD("sudo -n uname"); err == nil {
withSudo = "sudo "
}
if msg, err := cmd.ExecWithCheck("ssh", sshHost, fmt.Sprintf("%s expect -version", withSudo)); err != nil {
if msg, err := sshClient.RunCMD(fmt.Sprintf("%s expect -version", withSudo)); err != nil {
// stderr: bash: expect未找到命令 => exit status 127
msg = strings.TrimSpace(msg)
logger.Warnf("DumpUPF err: %s => %s", msg, err.Error())
@@ -160,7 +167,7 @@ func (s *TcpdumpImpl) DumpUPF(neType, neId, cmdStr string) (string, string, erro
// sudo chmod +x pcapUPF.sh
// expect ./cap.sh 'pcap dispatch trace off' > 20240115165701_UDM_001.log 2>&1
// cat 20240115165701_UDM_001.log
msg, err := cmd.ExecWithCheck("ssh", sshHost, sendCmd)
msg, err := sshClient.RunCMD(sendCmd)
msg = strings.TrimSpace(msg)
if err != nil || strings.HasPrefix(msg, "stderr:") {
logger.Warnf("DumpUPF err: %s => %s", msg, err.Error())

View File

@@ -13,8 +13,10 @@ import (
func Setup(router *gin.Engine) {
logger.Infof("开始加载 ====> trace 模块路由")
traceGroup := router.Group("/trace")
// 信令抓包
tcpdumpGroup := router.Group("/tcpdump")
tcpdumpGroup := traceGroup.Group("/tcpdump")
{
tcpdumpGroup.POST("/start",
middleware.PreAuthorize(nil),