I made a script for my own router, I can't tell you how YOUR router would restart, but I can walk you through what I did and why.
First you need to find out what your web browser is saying to the router that makes it reboot.
So I loaded up chrome and pressed F12. Then I went to the "network" section, set it to record, and not delete the log (so that when I went between pages it kept the information regarding what I had submitted).
Then I rebooted my router and here are the results in chrome:
https://imgur.com/i13hJ7s Submitted data can be easily found because it's marked as "Other" in the "Initiator" column, so I copied all these to notepad to see what was up.
https://imgur.com/LIg6AdN the relevant data was this:
Request URL: http://192.168.1.1/ Authorization: Basic bmljZXRyeTpjeXBoZXJwdW5r= Request URL: http://192.168.1.1/ADVANCED_home.htm Authorization: Basic bmljZXRyeTpjeXBoZXJwdW5r= Cookie: XSRF_TOKEN=1965091793 Request URL: http://192.168.1.1/newgui_adv_home.cgi?id=e2f5db0866ad833229c81fa163acf55d7d157a49a248f01bbd54dd9fe5c12741 Authorization: Basic bmljZXRyeTpjeXBoZXJwdW5r= Referer: http://192.168.1.1/ADVANCED_home2.htm
First I noticed that my router was using basic authorization. (Note, that string is a username and password and can be easily decoded so don't share those)
Second I had to go to another page to find the reboot option (
http://192.168.1.1/ADVANCED_home.htm)
Third the actual reboot, when I finally clicked the button, used the following form data:
https://imgur.com/5vR7rA1 buttonSelect = '2' wantype = 'dhcp' wanifname = 'eth0' wan_status = 'up' enable_apmode = '0' wwan_wan_type = '1' wwan_IMSI = '' wwan_IMEI = ''
That's all good; but the data for that submission contained an unusual string in the "RequestURL"
Request URL: http://192.168.1.1/newgui_adv_home.cgi?id=e2f5db0866ad833229c81fa163acf55d7d157a49a248f01bbd54dd9fe5c12741
This turned out to be my timestamped session ID, and it's unique every time.
So I searched through the sources in chrome to find out where that was.
https://imgur.com/EV51ZO0 It's being given to me by a file called ADVANCED_home2.htm
https://imgur.com/xID5Bvh I need to fetch that file in order to parse it for the data.
Also, I'm going to add a web session in case there are cookies in this that need to be captured. (This wasn't actually necessary on my router, but it may be relevant to others)
My script so far is:
$WSession = New-Object Microsoft.PowerShell.Commands.WebRequestSession $Username = "admin" $Password = (ConvertTo-SecureString -String "password" -AsPlainText -Force) $Cred = New-Object System.Management.Automation.PSCredential ($Username, $Password) $URL = "http://192.168.1.1/" Invoke-Webrequest -Uri $URL -Credential $Cred $URL1 = "http://192.168.1.1/ADVANCED_home2.htm" $Page1 = Invoke-WebRequest -Uri $URL1 -Credential $Cred -WebSession $WSession
At this point, I could parse the "RawContent" field ($Page1.RawContent) for relevant url, (using xml parsing or regular expressions) then create an array with the form data we saw earlier. Then I can submit that using Invoke-Webrequest
But it turned out there was an easier way for my router's web interface.
If I look at the members on $Page1
$page1 | Get-Member TypeName: Microsoft.PowerShell.Commands.HtmlWebResponseObject Name MemberType Definition ---- ---------- ---------- Dispose Method void Dispose(), void IDisposable.Dispose() [...irrelevant stuff] Forms Property Microsoft.PowerShell.Commands.FormObjectCollection Forms {get;} [...irrelevant stuff] RawContent Property string RawContent {get;set;} RawContentLength Property long RawContentLength {get;} RawContentStream Property System.IO.MemoryStream RawContentStream {get;}
I can see that the Invoke-WebRequest happens to have parsed and separated the page's data in a way that might be helpful. In particular, the FORMS member.
$Page1.Forms | fl *
Id : Method : post Action : newgui_adv_home.cgi? id=e2f5db0866ad833229c81fa163acf55d7d157a49a248f01bbd54dd9fe5c12741 Fields : {[buttonSelect, 1], [wantype, dhcp], [wanifname, eth0], [wan_status, down]...} Id : Method : get Action : Fields : {[curlang, English]}
Notice that newgui_adv_home.cgi is the URL that I submitted a form to before to reboot my router.
And if I check the fields:
$Page1.forms[0].Fields Key Value --- ----- buttonSelect 1 wantype dhcp wanifname eth0 wan_status up enable_apmode 0 wwan_wan_type 1 wwan_IMSI wwan_IMEI
It has all the same data except that buttonSelect was 2 to reboot, not 1.
Since invoke-webrequest has already parsed the data I need, I don't bother myself. I create my variables.
$URL = "http://192.168.1.1/" + $Page1.Forms[0].Action $Form = $Page1.Forms[0].Fields $Form.buttonSelect=2
I post that form using invoke-webrequest, and save the response for troubleshooting. My script is now:
$WSession = New-Object Microsoft.PowerShell.Commands.WebRequestSession $Username = "admin" $Password = (ConvertTo-SecureString -String "password" -AsPlainText -Force) $Cred = New-Object System.Management.Automation.PSCredential ($Username, $Password) $URL1 = "http://192.168.1.1/ADVANCED_home2.htm" $Page1 = Invoke-WebRequest -Uri $URL1 -Credential $Cred -WebSession $WSession $URL = "http://192.168.1.1/" + $Page1.Forms[0].Action $Form = $Page1.Forms[0].Fields $Form.buttonSelect=2 $Page2 = Invoke-WebRequest -Uri $URL -Credential $cred -Method Post -Body $Form -WebSession $WSession
Running this script with the right username, password, and IP address will reboot a number of netgear routers. I could probably use this same process to develop a script for just about any router.