diff options
Diffstat (limited to 'linuxstatus/old')
| -rw-r--r-- | linuxstatus/old/bash.go | 61 | ||||
| -rw-r--r-- | linuxstatus/old/dynamic-dns-update.go | 32 | ||||
| -rw-r--r-- | linuxstatus/old/fsnotify.go | 81 | ||||
| -rw-r--r-- | linuxstatus/old/nsupdate.go | 34 | ||||
| -rw-r--r-- | linuxstatus/old/rtnetlink.go | 26 | ||||
| -rw-r--r-- | linuxstatus/old/unix.go | 97 |
6 files changed, 331 insertions, 0 deletions
diff --git a/linuxstatus/old/bash.go b/linuxstatus/old/bash.go new file mode 100644 index 0000000..7143c1f --- /dev/null +++ b/linuxstatus/old/bash.go @@ -0,0 +1,61 @@ +package main + +import ( + "io" + "os" + "os/exec" + "os/signal" + "syscall" + + "github.com/creack/pty" + "golang.org/x/term" + + "go.wit.com/log" +) + +func test() error { + // Create arbitrary command. + c := exec.Command("bash") + + // Start the command with a pty. + ptmx, err := pty.Start(c) + if err != nil { + return err + } + // Make sure to close the pty at the end. + defer func() { _ = ptmx.Close() }() // Best effort. + + // Handle pty size. + ch := make(chan os.Signal, 1) + signal.Notify(ch, syscall.SIGWINCH) + go func() { + for range ch { + if err := pty.InheritSize(os.Stdin, ptmx); err != nil { + log.Println("error resizing pty: %s", err) + } + } + }() + ch <- syscall.SIGWINCH // Initial resize. + defer func() { signal.Stop(ch); close(ch) }() // Cleanup signals when done. + + // Set stdin in raw mode. + oldState, err := term.MakeRaw(int(os.Stdin.Fd())) + if err != nil { + panic(err) + } + defer func() { _ = term.Restore(int(os.Stdin.Fd()), oldState) }() // Best effort. + + // Copy stdin to the pty and the pty to stdout. + // NOTE: The goroutine will keep reading until the next keystroke before returning. + go func() { _, _ = io.Copy(ptmx, os.Stdin) }() + _, _ = io.Copy(os.Stdout, ptmx) + + return nil +} + +func mainBash() { + if err := test(); err != nil { + log.Error(err, "exit in mainBash()") + log.Exit(err) + } +} diff --git a/linuxstatus/old/dynamic-dns-update.go b/linuxstatus/old/dynamic-dns-update.go new file mode 100644 index 0000000..371a374 --- /dev/null +++ b/linuxstatus/old/dynamic-dns-update.go @@ -0,0 +1,32 @@ +package main + +/* + https://pkg.go.dev/github.com/miekg/dns#section-readme + +DYNAMIC UPDATES + +Dynamic updates reuses the DNS message format, but renames three of the sections. Question is Zone, Answer is Prerequisite, Authority is Update, only the Additional is not renamed. See RFC 2136 for the gory details. + +You can set a rather complex set of rules for the existence of absence of certain resource records or names in a zone to specify if resource records should be added or removed. The table from RFC 2136 supplemented with the Go DNS function shows which functions exist to specify the prerequisites. + +3.2.4 - Table Of Metavalues Used In Prerequisite Section + + CLASS TYPE RDATA Meaning Function + -------------------------------------------------------------- + ANY ANY empty Name is in use dns.NameUsed + ANY rrset empty RRset exists (value indep) dns.RRsetUsed + NONE ANY empty Name is not in use dns.NameNotUsed + NONE rrset empty RRset does not exist dns.RRsetNotUsed + zone rrset rr RRset exists (value dep) dns.Used + +The prerequisite section can also be left empty. If you have decided on the prerequisites you can tell what RRs should be added or deleted. The next table shows the options you have and what functions to call. + +3.4.2.6 - Table Of Metavalues Used In Update Section + + CLASS TYPE RDATA Meaning Function + --------------------------------------------------------------- + ANY ANY empty Delete all RRsets from name dns.RemoveName + ANY rrset empty Delete an RRset dns.RemoveRRset + NONE rrset rr Delete an RR from RRset dns.Remove + zone rrset rr Add to an RRset dns.Insert +*/ diff --git a/linuxstatus/old/fsnotify.go b/linuxstatus/old/fsnotify.go new file mode 100644 index 0000000..ba40c94 --- /dev/null +++ b/linuxstatus/old/fsnotify.go @@ -0,0 +1,81 @@ +package main + +// Watches for changes to a directory. Works cross-platform + +/* +import ( + "go.wit.com/log" + "github.com/fsnotify/fsnotify" +) + +// This would be a really dumb way to watch for new network interfaces +// since it then watches a linux only directory /sys/class/net for changes + +func watchSysClassNet() { + // Create new watcher. + watcher, err := fsnotify.NewWatcher() + if err != nil { + log.Error(err, "watchSysClassNet() failed") + return + } + defer watcher.Close() + + // Start listening for events. + go func() { + for { + select { + case event, ok := <-watcher.Events: + if !ok { + return + } + log.Println("event:", event) + if event.Has(fsnotify.Write) { + log.Println("modified file:", event.Name) + } + case err, ok := <-watcher.Errors: + if !ok { + return + } + log.Println("error:", err) + } + } + }() + + // Add a path. + err = watcher.Add("/tmp") + if err != nil { + log.Error(err, "watchSysClassNet() watcher.Add() failed") + return + } + + // Block main goroutine forever. + <-make(chan struct{}) +} + +func fsnotifyNetworkInterfaceChanges() error { + watcher, err := fsnotify.NewWatcher() + if err != nil { + return err + } + defer watcher.Close() + + // Watch for network interface changes + err = watcher.Add("/sys/class/net") + if err != nil { + return err + } + for { + select { + case event := <-watcher.Events: + log.Println("fsnotifyNetworkInterfaceChanges() event =", event) + if event.Op&fsnotify.Create == fsnotify.Create { + // Do something on network interface creation + } + case err := <-watcher.Errors: + log.Println("fsnotifyNetworkInterfaceChanges() event err =", err) + return err + } + } +} + +*/ diff --git a/linuxstatus/old/nsupdate.go b/linuxstatus/old/nsupdate.go new file mode 100644 index 0000000..635de4c --- /dev/null +++ b/linuxstatus/old/nsupdate.go @@ -0,0 +1,34 @@ +// inspired from: +// https://github.com/mactsouk/opensource.com.git +// and +// https://coderwall.com/p/wohavg/creating-a-simple-tcp-server-in-go + +package main + +import ( +) + +// ./go-nsupdate \ +// --tsig-algorithm=hmac-sha512 \ +// --tsig-secret="OWh5/ZHIyaz7B8J9m9ZDqZ8448Pke0PTpkYbZmFcOf5a6rEzgmcwrG91u1BHi1/4us+mKKEobDPLw1x6sD+ZJw==" \ +// -i eno2 farm001.lab.wit.com + +/* +func nsupdate() { + var tsigSecret string + log.Log(NET, "nsupdate() START") + cmd := "go-nsupdate --tsig-algorithm=hmac-sha512" + tsigSecret = os.Getenv("TIG_SECRET") + cmd += " --tig-secret=\"" + tsigSecret + "\"" + cmd += " -i wlo1 " + me.statusOS.GetHostname() + log.Log(NET, "nsupdate() RUN:", cmd) + + for s, t := range me.ipmap { + if (t.IsReal()) { + if (t.ipv6) { + log.Log(NET, "nsupdate() found real AAAA =", s, "on iface", t.iface.Name) + } + } + } +} +*/ diff --git a/linuxstatus/old/rtnetlink.go b/linuxstatus/old/rtnetlink.go new file mode 100644 index 0000000..29f1153 --- /dev/null +++ b/linuxstatus/old/rtnetlink.go @@ -0,0 +1,26 @@ +package main + +import ( + "github.com/jsimonetti/rtnetlink" + "go.wit.com/log" +) + +// List all interfaces +func Example_listLink() { + // Dial a connection to the rtnetlink socket + conn, err := rtnetlink.Dial(nil) + if err != nil { + log.Error(err, "Example_listLink() failed") + return + } + defer conn.Close() + + // Request a list of interfaces + msg, err := conn.Link.List() + if err != nil { + log.Println(err) + } + + log.Println("%#v", msg) + log.Println(SPEW, msg) +} diff --git a/linuxstatus/old/unix.go b/linuxstatus/old/unix.go new file mode 100644 index 0000000..b09481a --- /dev/null +++ b/linuxstatus/old/unix.go @@ -0,0 +1,97 @@ +// Various Linux/Unix'y things + +// https://wiki.archlinux.org/title/Dynamic_DNS + +package main + +import ( + "os" + "os/exec" + "net" + "bytes" + "fmt" + "strings" + + "go.wit.com/log" + "go.wit.com/shell" +) + +func CheckSuperuser() bool { + return os.Getuid() == 0 +} + +func Escalate() { + if os.Getuid() != 0 { + cmd := exec.Command("sudo", "./control-panel-dns") // TODO: get the actual path + cmd.Stdin = os.Stdin + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + err := cmd.Run() + if err != nil { + log.Error(err, "exit in Escalate()") + log.Exit(err) + } + } +} + +// You need permission to do a zone transfer. Otherwise: +// dig +noall +answer +multiline lab.wit.com any +// dig +all +multiline fire.lab.wit.com # gives the zonefile header (ttl vals) +func DumpPublicDNSZone(zone string) { + entries, err := net.LookupHost(zone) + if err != nil { + panic(err) + } + for _, entry := range entries { + log.Println(entry) + } +} + +func dumpIPs(host string) { + ips, err := net.LookupIP(host) + if err != nil { + log.Error(err, "dumpIPs() failed") + } + for _, ip := range ips { + log.Println(host, ip) + } +} + +/* + check if ddclient is installed, working, and/or configured + https://github.com/ddclient/ddclient +*/ +func ddclient() { +} + +/* + check if ddupdate is installed, working, and/or configured +*/ +func ddupdate() { +} + +func run(s string) string { + cmdArgs := strings.Fields(s) + // Define the command you want to run + // cmd := exec.Command(cmdArgs) + cmd := exec.Command(cmdArgs[0], cmdArgs[1:len(cmdArgs)]...) + + // Create a buffer to capture the output + var out bytes.Buffer + + // Set the output of the command to the buffer + cmd.Stdout = &out + + // Run the command + err := cmd.Run() + if err != nil { + fmt.Println("Error running command:", err) + return "" + } + + tmp := shell.Chomp(out.String()) + // Output the results + log.Info("Command Output:", tmp) + + return tmp +} |
