Skip to content

Instantly share code, notes, and snippets.

@efrecon
Created January 13, 2018 12:49
Show Gist options
  • Save efrecon/a0955e155b96dfb74c95b59009c77f21 to your computer and use it in GitHub Desktop.
Save efrecon/a0955e155b96dfb74c95b59009c77f21 to your computer and use it in GitHub Desktop.
Restart LuCI-based OpenWRT router from internal host
#!/usr/bin/env tclsh
package require http
array set options {
-root "http://192.168.1.1/"
-user "user"
-password ""
}
array set options $argv
set tok [::http::geturl [string trimright $options(-root) /]/cgi-bin/luci \
-query [::http::formatQuery username $options(-user) password $options(-password)]]
if { [::http::ncode $tok] == 200 } {
array set meta [::http::meta $tok]
foreach itm [split [string map [list "; " \0] $meta(Set-Cookie)] "\0"] {
set equal [string first "=" $itm]
set k [string trim [string range $itm 0 [expr {$equal-1}]]]
set v [string trim [string range $itm [expr {$equal+1}] end]]
set auth($k) $v
}
puts "Logged in at $auth(path)"
set rebootURL [string trimright $options(-root) /]/[string trim $auth(path) /]/user/system/reboot?reboot=1
set tok [::http::geturl $rebootURL -headers [list "Cookie" "sysauth=$auth(sysauth); "]]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment