mirror of
https://github.com/httprunner/httprunner.git
synced 2026-07-09 06:23:52 +08:00
change: remove traceroute
This commit is contained in:
@@ -99,7 +99,6 @@ Available Commands:
|
|||||||
pytest run API test with pytest
|
pytest run API test with pytest
|
||||||
run run API test with go engine
|
run run API test with go engine
|
||||||
startproject create a scaffold project
|
startproject create a scaffold project
|
||||||
traceroute run integrated traceroute command
|
|
||||||
wiki visit https://httprunner.com
|
wiki visit https://httprunner.com
|
||||||
|
|
||||||
Flags:
|
Flags:
|
||||||
|
|||||||
@@ -92,7 +92,6 @@ Available Commands:
|
|||||||
pytest run API test with pytest
|
pytest run API test with pytest
|
||||||
run run API test with go engine
|
run run API test with go engine
|
||||||
startproject create a scaffold project
|
startproject create a scaffold project
|
||||||
traceroute run integrated traceroute command
|
|
||||||
wiki visit https://httprunner.com
|
wiki visit https://httprunner.com
|
||||||
|
|
||||||
Flags:
|
Flags:
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"runtime"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
@@ -11,9 +10,8 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
pingOptions dial.PingOptions
|
pingOptions dial.PingOptions
|
||||||
dnsOptions dial.DnsOptions
|
dnsOptions dial.DnsOptions
|
||||||
traceRouteOptions dial.TraceRouteOptions
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var pingCmd = &cobra.Command{
|
var pingCmd = &cobra.Command{
|
||||||
@@ -46,21 +44,6 @@ var dnsCmd = &cobra.Command{
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
var traceRouteCmd = &cobra.Command{
|
|
||||||
Use: "traceroute $url",
|
|
||||||
Short: "run integrated traceroute command",
|
|
||||||
Args: cobra.ExactArgs(1),
|
|
||||||
PreRun: func(cmd *cobra.Command, args []string) {
|
|
||||||
setLogLevel(logLevel)
|
|
||||||
},
|
|
||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
|
||||||
if runtime.GOOS == "windows" {
|
|
||||||
log.Info().Msg("using default probe number (3) on Windows")
|
|
||||||
}
|
|
||||||
return dial.DoTraceRoute(&traceRouteOptions, args)
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
rootCmd.AddCommand(pingCmd)
|
rootCmd.AddCommand(pingCmd)
|
||||||
pingCmd.Flags().IntVarP(&pingOptions.Count, "count", "c", 10, "Stop after sending (and receiving) N packets")
|
pingCmd.Flags().IntVarP(&pingOptions.Count, "count", "c", 10, "Stop after sending (and receiving) N packets")
|
||||||
@@ -73,9 +56,4 @@ func init() {
|
|||||||
dnsCmd.Flags().IntVar(&dnsOptions.DnsRecordType, "dns-record", 1, "DNS record type\n1: A\n28: AAAA\n5: CNAME")
|
dnsCmd.Flags().IntVar(&dnsOptions.DnsRecordType, "dns-record", 1, "DNS record type\n1: A\n28: AAAA\n5: CNAME")
|
||||||
dnsCmd.Flags().StringVar(&dnsOptions.DnsServer, "dns-server", "", "DNS server, only available for local DNS source")
|
dnsCmd.Flags().StringVar(&dnsOptions.DnsServer, "dns-server", "", "DNS server, only available for local DNS source")
|
||||||
dnsCmd.Flags().BoolVar(&dnsOptions.SaveTests, "save-tests", false, "Save DNS resolution result as json")
|
dnsCmd.Flags().BoolVar(&dnsOptions.SaveTests, "save-tests", false, "Save DNS resolution result as json")
|
||||||
|
|
||||||
rootCmd.AddCommand(traceRouteCmd)
|
|
||||||
traceRouteCmd.Flags().IntVarP(&traceRouteOptions.MaxTTL, "max-hops", "m", 30, "Set the max number of hops (max TTL to be reached)")
|
|
||||||
traceRouteCmd.Flags().IntVarP(&traceRouteOptions.Queries, "queries", "q", 1, "Set the number of probes per each hop")
|
|
||||||
traceRouteCmd.Flags().BoolVar(&traceRouteOptions.SaveTests, "save-tests", false, "Save traceroute result as json")
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,20 +0,0 @@
|
|||||||
package dial
|
|
||||||
|
|
||||||
type TraceRouteOptions struct {
|
|
||||||
MaxTTL int
|
|
||||||
Queries int
|
|
||||||
SaveTests bool
|
|
||||||
}
|
|
||||||
|
|
||||||
type TraceRouteResult struct {
|
|
||||||
IP string `json:"ip"`
|
|
||||||
Details []TraceRouteResultNode `json:"details"`
|
|
||||||
Suc bool `json:"suc"`
|
|
||||||
ErrMsg string `json:"errMsg"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type TraceRouteResultNode struct {
|
|
||||||
Id int `json:"id"`
|
|
||||||
Ip string `json:"ip"`
|
|
||||||
Time string `json:"time"`
|
|
||||||
}
|
|
||||||
@@ -1,104 +0,0 @@
|
|||||||
//go:build darwin || linux
|
|
||||||
|
|
||||||
package dial
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bufio"
|
|
||||||
"fmt"
|
|
||||||
"net/url"
|
|
||||||
"path/filepath"
|
|
||||||
"regexp"
|
|
||||||
"strconv"
|
|
||||||
"strings"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/pkg/errors"
|
|
||||||
"github.com/rs/zerolog/log"
|
|
||||||
|
|
||||||
"github.com/httprunner/httprunner/v4/hrp/internal/builtin"
|
|
||||||
"github.com/httprunner/httprunner/v4/hrp/internal/env"
|
|
||||||
"github.com/httprunner/httprunner/v4/hrp/internal/myexec"
|
|
||||||
)
|
|
||||||
|
|
||||||
var (
|
|
||||||
regexIPAddr = regexp.MustCompile(`([\d.]+)`)
|
|
||||||
regexElapsedTime = regexp.MustCompile(`(\d+\.\d+)`)
|
|
||||||
regexTraceroutePass = regexp.MustCompile(fmt.Sprintf(`(\d+)[\s*]+(\S+)\s+\(%s\)\s+%s\s+ms`, regexIPAddr, regexElapsedTime))
|
|
||||||
regexTracerouteFailure = regexp.MustCompile(`(\d+)[\s*]+$`)
|
|
||||||
)
|
|
||||||
|
|
||||||
func DoTraceRoute(traceRouteOptions *TraceRouteOptions, args []string) (err error) {
|
|
||||||
if len(args) != 1 {
|
|
||||||
return errors.New("there should be one argument")
|
|
||||||
}
|
|
||||||
var traceRouteResult TraceRouteResult
|
|
||||||
defer func() {
|
|
||||||
if traceRouteOptions.SaveTests {
|
|
||||||
traceRouteResultName := fmt.Sprintf("traceroute_result_%v.json", env.StartTimeStr)
|
|
||||||
traceRouteResultPath := filepath.Join(env.RootDir, traceRouteResultName)
|
|
||||||
err = builtin.Dump2JSON(traceRouteResult, traceRouteResultPath)
|
|
||||||
if err != nil {
|
|
||||||
log.Error().Err(err).Msg("save traceroute result failed")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
traceRouteTarget := args[0]
|
|
||||||
parsedURL, err := url.Parse(traceRouteTarget)
|
|
||||||
if err == nil && parsedURL.Host != "" {
|
|
||||||
log.Info().Msgf("parse input url %v and extract host %v", traceRouteTarget, parsedURL.Host)
|
|
||||||
traceRouteTarget = strings.Split(parsedURL.Host, ":")[0]
|
|
||||||
}
|
|
||||||
|
|
||||||
cmd := myexec.Command("traceroute", "-m", strconv.Itoa(traceRouteOptions.MaxTTL),
|
|
||||||
"-q", strconv.Itoa(traceRouteOptions.Queries), traceRouteTarget)
|
|
||||||
stdout, _ := cmd.StdoutPipe()
|
|
||||||
|
|
||||||
startT := time.Now()
|
|
||||||
defer func() {
|
|
||||||
log.Info().Msgf("for target %s, traceroute costs %v", traceRouteTarget, time.Since(startT))
|
|
||||||
}()
|
|
||||||
|
|
||||||
log.Info().Msgf("start to traceroute %v", traceRouteTarget)
|
|
||||||
err = cmd.Start()
|
|
||||||
if err != nil {
|
|
||||||
traceRouteResult.Suc = false
|
|
||||||
traceRouteResult.ErrMsg = "execute traceroute failed"
|
|
||||||
log.Error().Err(err).Msg("start command failed")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
scanner := bufio.NewScanner(stdout)
|
|
||||||
for scanner.Scan() {
|
|
||||||
hopLine := scanner.Text()
|
|
||||||
fmt.Println(hopLine)
|
|
||||||
failureLine := regexTracerouteFailure.FindStringSubmatch(hopLine)
|
|
||||||
if len(failureLine) == 2 {
|
|
||||||
hopID, _ := strconv.Atoi(failureLine[1])
|
|
||||||
traceRouteResult.Details = append(traceRouteResult.Details, TraceRouteResultNode{
|
|
||||||
Id: hopID,
|
|
||||||
})
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
passLine := regexTraceroutePass.FindStringSubmatch(hopLine)
|
|
||||||
if len(passLine) == 5 {
|
|
||||||
hopID, _ := strconv.Atoi(passLine[1])
|
|
||||||
traceRouteResult.Details = append(traceRouteResult.Details, TraceRouteResultNode{
|
|
||||||
Id: hopID,
|
|
||||||
Ip: passLine[3],
|
|
||||||
Time: passLine[4],
|
|
||||||
})
|
|
||||||
traceRouteResult.Suc = true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
hopCount := len(traceRouteResult.Details)
|
|
||||||
traceRouteResult.IP = traceRouteResult.Details[hopCount-1].Ip
|
|
||||||
err = cmd.Wait()
|
|
||||||
if err != nil {
|
|
||||||
traceRouteResult.Suc = false
|
|
||||||
traceRouteResult.ErrMsg = "wait traceroute finish failed"
|
|
||||||
log.Error().Err(err).Msg("wait command failed")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
@@ -1,103 +0,0 @@
|
|||||||
//go:build windows
|
|
||||||
|
|
||||||
package dial
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bufio"
|
|
||||||
"fmt"
|
|
||||||
"net/url"
|
|
||||||
"os"
|
|
||||||
"path/filepath"
|
|
||||||
"regexp"
|
|
||||||
"strconv"
|
|
||||||
"strings"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/pkg/errors"
|
|
||||||
"github.com/rs/zerolog/log"
|
|
||||||
|
|
||||||
"github.com/httprunner/httprunner/v4/hrp/internal/builtin"
|
|
||||||
"github.com/httprunner/httprunner/v4/hrp/internal/myexec"
|
|
||||||
)
|
|
||||||
|
|
||||||
var (
|
|
||||||
regexTracertPass = regexp.MustCompile(`(\d+)[\s*<]+(\d+)\s+ms`)
|
|
||||||
regexTracertFailure = regexp.MustCompile(`(\d+)[\s*]+Request timed out`)
|
|
||||||
)
|
|
||||||
|
|
||||||
func DoTraceRoute(traceRouteOptions *TraceRouteOptions, args []string) (err error) {
|
|
||||||
if len(args) != 1 {
|
|
||||||
return errors.New("there should be one argument")
|
|
||||||
}
|
|
||||||
var traceRouteResult TraceRouteResult
|
|
||||||
defer func() {
|
|
||||||
if traceRouteOptions.SaveTests {
|
|
||||||
traceRouteResultName := fmt.Sprintf("traceroute_result_%v.json", env.StartTimeStr)
|
|
||||||
traceRouteResultPath := filepath.Join(env.RootDir, traceRouteResultName)
|
|
||||||
err = builtin.Dump2JSON(traceRouteResult, traceRouteResultPath)
|
|
||||||
if err != nil {
|
|
||||||
log.Error().Err(err).Msg("save traceroute result failed")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
traceRouteTarget := args[0]
|
|
||||||
parsedURL, err := url.Parse(traceRouteTarget)
|
|
||||||
if err == nil && parsedURL.Host != "" {
|
|
||||||
log.Info().Msgf("parse input url %v and extract host %v", traceRouteTarget, parsedURL.Host)
|
|
||||||
traceRouteTarget = strings.Split(parsedURL.Host, ":")[0]
|
|
||||||
}
|
|
||||||
|
|
||||||
cmd := myexec.Command("tracert", "-h", strconv.Itoa(traceRouteOptions.MaxTTL), traceRouteTarget)
|
|
||||||
stdout, _ := cmd.StdoutPipe()
|
|
||||||
|
|
||||||
startT := time.Now()
|
|
||||||
defer func() {
|
|
||||||
log.Info().Msgf("for target %s, traceroute costs %v", traceRouteTarget, time.Since(startT))
|
|
||||||
}()
|
|
||||||
|
|
||||||
log.Info().Msgf("start to traceroute %v", traceRouteTarget)
|
|
||||||
err = cmd.Start()
|
|
||||||
if err != nil {
|
|
||||||
traceRouteResult.Suc = false
|
|
||||||
traceRouteResult.ErrMsg = "execute traceroute failed"
|
|
||||||
log.Error().Err(err).Msg("start command failed")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
scanner := bufio.NewScanner(stdout)
|
|
||||||
for scanner.Scan() {
|
|
||||||
hopLine := scanner.Text()
|
|
||||||
fmt.Println(hopLine)
|
|
||||||
failureLine := regexTracertFailure.FindStringSubmatch(hopLine)
|
|
||||||
if len(failureLine) == 2 {
|
|
||||||
hopID, _ := strconv.Atoi(failureLine[1])
|
|
||||||
traceRouteResult.Details = append(traceRouteResult.Details, TraceRouteResultNode{
|
|
||||||
Id: hopID,
|
|
||||||
})
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
passLine := regexTracertPass.FindStringSubmatch(hopLine)
|
|
||||||
if len(passLine) == 3 {
|
|
||||||
hopID, _ := strconv.Atoi(passLine[1])
|
|
||||||
fields := strings.Fields(hopLine)
|
|
||||||
hopIP := strings.Trim(fields[len(fields)-1], "[]")
|
|
||||||
traceRouteResult.Details = append(traceRouteResult.Details, TraceRouteResultNode{
|
|
||||||
Id: hopID,
|
|
||||||
Ip: hopIP,
|
|
||||||
Time: passLine[2],
|
|
||||||
})
|
|
||||||
traceRouteResult.Suc = true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
hopCount := len(traceRouteResult.Details)
|
|
||||||
traceRouteResult.IP = traceRouteResult.Details[hopCount-1].Ip
|
|
||||||
err = cmd.Wait()
|
|
||||||
if err != nil {
|
|
||||||
traceRouteResult.Suc = false
|
|
||||||
traceRouteResult.ErrMsg = "wait traceroute finish failed"
|
|
||||||
log.Error().Err(err).Msg("wait command failed")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user