Skip to content

Commit 7c8cc3a

Browse files
committed
Basic working version with README.md
1 parent cd3b8ad commit 7c8cc3a

File tree

3 files changed

+90
-0
lines changed

3 files changed

+90
-0
lines changed

README.md

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# PortCheck
2+
3+
Just a simple ping-like tool that will also take a port argument. This
4+
is useful if you not only want to see if a host is up, but if a specific
5+
service on that host is up.
6+
7+
```
8+
Usage:
9+
portcheck [options] HOST PORT
10+
11+
Options:
12+
-count int
13+
Number of packets to send (default 1)
14+
```
15+
16+
## Installation
17+
18+
### Building from local repo
19+
To build portcheck from your local repo, run:
20+
```
21+
go build ./...
22+
```
23+

cmd/portcheck/main.go

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
Simple tool to check whether a port is active, and keep checking
3+
it like ping
4+
5+
Written by:
6+
Paul Larson <pwlars@gmail.com>
7+
*/
8+
9+
package main
10+
11+
import (
12+
"flag"
13+
"fmt"
14+
"net"
15+
"os"
16+
"time"
17+
)
18+
19+
type Args struct {
20+
host string
21+
port string
22+
count int
23+
}
24+
25+
func getArgs() Args {
26+
var args Args
27+
count := flag.Int("count", 1, "Number of packets to send")
28+
flag.Parse()
29+
if flag.NArg() != 2 {
30+
usage()
31+
os.Exit(1)
32+
}
33+
args.host = flag.Arg(0)
34+
args.port = flag.Arg(1)
35+
args.count = *count
36+
return args
37+
}
38+
39+
func usage() {
40+
fmt.Printf("Usage: %s [options] HOST PORT\n\n", os.Args[0])
41+
fmt.Println("Options:")
42+
flag.PrintDefaults()
43+
}
44+
45+
func main() {
46+
args := getArgs()
47+
for i := 0; i < args.count; i++ {
48+
fmt.Printf("%d %s:%s - %s\n", i, args.host, args.port, connect(args.host, args.port))
49+
time.Sleep(time.Second)
50+
}
51+
}
52+
53+
func connect(host, port string) string {
54+
timeout := time.Second
55+
conn, err := net.DialTimeout("tcp", net.JoinHostPort(host, port), timeout)
56+
if err != nil {
57+
return "Down"
58+
}
59+
if conn == nil {
60+
return "Down"
61+
}
62+
conn.Close()
63+
return "Up"
64+
}

go.mod

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module github.com/plars/portcheck
2+
3+
go 1.18

0 commit comments

Comments
 (0)