Created
July 18, 2017 06:17
-
-
Save TJC/ddd66b7329fed81141c6ae1a4ec10a6e to your computer and use it in GitHub Desktop.
Embarrassingly quick and dirty Go program to reboot a TP-Link TD-8840 ADSL modem.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| // Embarrassingly quick and dirty Go program to reboot a TP-Link TD-8840 ADSL modem. | |
| // Will possibly work on other devices with slight modifications. | |
| import ( | |
| "github.com/ziutek/telnet" | |
| "log" | |
| "os" | |
| "time" | |
| // "fmt" | |
| ) | |
| const timeout = 5 * time.Second | |
| func checkErr(err error) { | |
| if err != nil { | |
| log.Fatalln("Error:", err) | |
| } | |
| } | |
| func expect(t *telnet.Conn, d ...string) { | |
| checkErr(t.SetReadDeadline(time.Now().Add(timeout))) | |
| checkErr(t.SkipUntil(d...)) | |
| } | |
| func sendln(t *telnet.Conn, s string) { | |
| checkErr(t.SetWriteDeadline(time.Now().Add(timeout))) | |
| buf := make([]byte, len(s)+1) | |
| copy(buf, s) | |
| buf[len(s)] = '\n' | |
| _, err := t.Write(buf) | |
| checkErr(err) | |
| } | |
| func briefDelay() { | |
| time.Sleep(100 * time.Millisecond) | |
| } | |
| func longDelay() { | |
| time.Sleep(2000 * time.Millisecond) | |
| } | |
| func main() { | |
| if len(os.Args) != 2 { | |
| log.Printf("Usage: %s HOST", os.Args[0]) | |
| return | |
| } | |
| dst := os.Args[1] | |
| t, err := telnet.Dial("tcp", dst + ":23") | |
| checkErr(err) | |
| // fmt.Println("connected..") | |
| t.SetUnixWriteMode(true) | |
| expect(t, "username:") | |
| briefDelay() | |
| sendln(t, "admin") | |
| // fmt.Println("sent username..") | |
| expect(t, "password:") | |
| briefDelay() | |
| sendln(t, "admin") | |
| // fmt.Println("sent password..") | |
| longDelay() | |
| sendln(t, "dev reboot") | |
| longDelay() | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment