/
ip-query.go
86 lines (76 loc) · 1.96 KB
/
ip-query.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package main
import (
"fmt"
"github.com/cloudfoundry/cli/plugin"
"os"
"strings"
"net"
)
type IpQuery struct{}
func (c *IpQuery) GetMetadata() plugin.PluginMetadata{
primaryUsage := "cf ip-query <IP>"
secondaryUsage := " Only apps you have access to will be queried."
flags := make(map[string]string)
return plugin.PluginMetadata{
Name: "ip-query",
Version: plugin.VersionType{
Major: 0,
Minor: 1,
Build: 0,
},
MinCliVersion: plugin.VersionType{
Major: 6,
Minor: 12,
Build: 0,
},
Commands: []plugin.Command{
{
Name: "ip-query",
HelpText: "Find what apps are running at a specific IP address.",
UsageDetails: plugin.Usage{
Usage: strings.Join([]string{primaryUsage, secondaryUsage}, "\n\n"),
Options: flags,
},
},
},
}
}
func main() {
plugin.Start(new(IpQuery))
}
func (c * IpQuery) Run(cliConnection plugin.CliConnection, args []string) {
if args[0] == "ip-query" {
if len(args) < 2 {
fmt.Println("Incorrect Usage. \n\nIP is a required argument")
os.Exit(1)
}
ip := args[1]
if net.ParseIP(ip) == nil {
fmt.Println("Invalid IP address")
os.Exit(1)
}
fmt.Printf("Searching for apps running on %s that you have permissions to access. Depending on the number of apps, this may take some time.\n\n", ip)
matchCount := 0
api := NewCliCcApi(cliConnection)
newApps, _ := api.StartedApps()
for _, app := range newApps {
stats, error := api.AppStats(app.Guid)
if error != nil {
fmt.Println(error.Error())
}
for _, instance := range stats.AppInstanceStats {
if ip == instance.Host {
space, error := api.Space(app.SpaceUrl)
if error != nil {
fmt.Println("Error: " + error.Error())
}
matchCount++
fmt.Printf("Organization: %s, Space: %s, App: %s, Instance: %s\n", space.OrgName, space.Name, app.Name, instance.Instance)
}
}
}
if matchCount == 0 {
fmt.Println("No apps found")
}
}
}