Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

Reboot Netgear Router w/ C#

 

I just completed and tested a proof of concept C# (DotNetCore) app that will reboot my Netgear router. The purpose of this being that I can now take the exe and schedule it to reboot my router in Windows Task scheduler as no matter how good a router is they all perform better after a nightly reboot. I've tested this on an R700P but other Netgear routers should work.

 

https://github.com/andyrblank/NetgearRouterReboot

 

Let me know what you think.

 

Thanks!

Link to post
Share on other sites
15 minutes ago, andyrblank said:

no matter how good a router is they all perform better after a nightly reboot

Nope. Only routers with bad hardware or firmware need reboots.

Hand, n. A singular instrument worn at the end of the human arm and commonly thrust into somebody’s pocket.

Link to post
Share on other sites

Your use of var is too liberal. The code may be well commented but I recommend being explicit when it is not immediately apparent what the resulting type is. An example where the use of var is warranted would be code like the following.

var myList = new List<string>();

For example, you used var in one case where the return value was an integer, but int and var are the same number of characters.

var idPosition = formAction.IndexOf("id=") + 3;

You also have an unhandled exception for your HTTP request.

 

 

On a side note not really programming related, WereCatf is right and routers shouldn't need to be rebooted.

Link to post
Share on other sites
4 hours ago, Sakuriru said:

Your use of var is too liberal. The code may be well commented but I recommend being explicit when it is not immediately apparent what the resulting type is. An example where the use of var is warranted would be code like the following.


var myList = new List<string>();

For example, you used var in one case where the return value was an integer, but int and var are the same number of characters.


var idPosition = formAction.IndexOf("id=") + 3;

You also have an unhandled exception for your HTTP request.

 

 

On a side note not really programming related, WereCatf is right and routers shouldn't need to be rebooted.

Thank you! I appreciate the feedback.

 

This code was written in just about an hour last night for fun so there's definitely room for improvement. I do plan on adding command line arguments so that the parameters of the router ip, username and password can be passed in at runtime allowing for things like batch scripting (or even shell scripting in Linux since it's DotNetCore) and definitely want to add more error handling and variable checking throughout. I've even found some projects that have reverse engineered the Netgear SOAP api to talk to the router so that would be another good place to expand into and make the code more reliable. I might spin that idea out into another repository though.

 

While I understand where you're coming from on the var argument I'm going to have to agree to disagree. C# is strongly typed and the compiler won't let you use var if the type is ambiguous. Also simply hovering over the variable name in Visual Studio will tell you the variable type. So I use var for just about everything unless I need to specifically cast something from an anonymous object.

 

As far as the router conversation goes I'd like to agree but I've spent a lot of money on different consumer-grade routers* and some will last for months without needing a reboot but all perform much better and consistently with a nightly reboot and as far as I'm concerned it's a peace of mind action to perform that for me has no real consequence as I'm not using my router at 3am anyway.

 

*Consumer grade routers I've dealt with (there's even more I can't remember)

Netgear R7000P

Netgear R7200

TP Link Archer C59

Asus RT-N10P

Netgear WNDR3400

Linksys WRT54G

Linksys WRT54GS

 

Ultimately I'd like to replace my router with a pfsense PC and add some Ubiquity WAPs but that's not happening for me now.

Link to post
Share on other sites
13 hours ago, WereCatf said:

Nope. Only routers with bad hardware or firmware need reboots.

I'm curious what is this perfect router that you recommend?

Link to post
Share on other sites
23 minutes ago, andyrblank said:

While I understand where you're coming from on the var argument I'm going to have to agree to disagree. C# is strongly typed and the compiler won't let you use var if the type is ambiguous. Also simply hovering over the variable name in Visual Studio will tell you the variable type. So I use var for just about everything unless I need to specifically cast something from an anonymous object.

You're more than welcome to make your own code as obfuscated as you'd like, but don't expect other programmers to share your opinion, because you're making it harder to read when it would be trivial to make it easier to read. It's also part of the language's own official naming conventions.

Link to post
Share on other sites
4 minutes ago, Sakuriru said:

You're more than welcome to make your own code as obfuscated as you'd like, but don't expect other programmers to share your opinion, because you're making it harder to read when it would be trivial to make it easier to read. It's also part of the language's own official naming conventions.

That's a fair point. Thank you.

Link to post
Share on other sites
29 minutes ago, andyrblank said:

I'm curious what is this perfect router that you recommend?

OpenWRT can be installed on a lot of different routers or one can make one's own with e.g. Opnsense or Pfsense. The only times I need to reboot is when I install firmware-upgrades. That said, I am not telling you to go that route; if you're happy with using your project, that's all that matters.

Hand, n. A singular instrument worn at the end of the human arm and commonly thrust into somebody’s pocket.

Link to post
Share on other sites
19 minutes ago, WereCatf said:

OpenWRT can be installed on a lot of different routers or one can make one's own with e.g. Opnsense or Pfsense. The only times I need to reboot is when I install firmware-upgrades. That said, I am not telling you to go that route; if you're happy with using your project, that's all that matters.

I was curious what your setup was. I've used DDWRT and/or Tomato for many of those routers I mentioned earlier and had great success but both of those firmware options have a built-in scheduled reboot option too. I also used DDWRT with my current Router (R7000P) and it was fine until it decided to fail miserably while I was traveling and left my wife & kids without Wifi which in our house means without TV too. It didn't start working again until I came home and factory reset the router. My wife had to switch to an old backup router (the TP-Link mentioned above) until I got home. So I don't trust this particular router with open firmware now.

Link to post
Share on other sites
1 minute ago, andyrblank said:

I was curious what your setup was.

I've got 5 routers running OpenWRT (not DDWRT, since I find it to be quite bad in comparison) and my main, Internet-facing router is running Pfsense at the moment, though I'm switching it to more powerful hardware and Opnsense, when I can get around to it.

Hand, n. A singular instrument worn at the end of the human arm and commonly thrust into somebody’s pocket.

Link to post
Share on other sites

I am unaware of how good good homegrade router are. I always had business grade routers due to my work but the only time they need reboot is as @WereCatf said for firmware upgrade and when i lose electricity. I am pretty certain that there is a bunch of good homegrade router that are staying cool enough and have plenty of core/cache and ram to handle constant load endlessly.

Link to post
Share on other sites
21 minutes ago, Franck said:

I am unaware of how good good homegrade router are. I always had business grade routers due to my work but the only time they need reboot is as @WereCatf said for firmware upgrade and when i lose electricity. I am pretty certain that there is a bunch of good homegrade router that are staying cool enough and have plenty of core/cache and ram to handle constant load endlessly.

You can get get an EdgeRouter X and AC Lite WAP for like $140. That's pretty darn cheap for pretty stellar hardware. Probably a lot cheaper used off of ebay. 3-in-1s put a bad taste in my mouth.

Link to post
Share on other sites
7 minutes ago, Sakuriru said:

You can get get an EdgeRouter X and AC Lite WAP for like $140. That's pretty darn cheap for pretty stellar hardware. Probably a lot cheaper used off of ebay. 3-in-1s put a bad taste in my mouth.

Don't know these model but if they are that good yes 140$ is a very good deal. People tend to forget to try aftermarket for these things. Even local business close down event. Got 2 summers ago a SG-350X-24P that has 10 gigabit with POE for 450$ CAD. When i bought it they just had installed it not even a year before that. Anyhow it was first made in 2015 or 2016 so couldn't have been older than 2-3 years. It was over 1800$ CAD at the time.

Link to post
Share on other sites
On 10/29/2020 at 3:45 PM, Sakuriru said:

You're more than welcome to make your own code as obfuscated as you'd like, but don't expect other programmers to share your opinion, because you're making it harder to read when it would be trivial to make it easier to read. It's also part of the language's own official naming conventions.

I have tried to take some of your suggestions and have improved the code of the Project (even if we disagree on necessity of router reboots): https://github.com/andyrblank/NetgearRouterReboot

 

I have also changed the code to use command line parameters with help from the new System.CommandLine package(s): https://www.nuget.org/packages/System.CommandLine

 

Thanks again for the feedback / peer review.

Link to post
Share on other sites
On 11/3/2020 at 11:26 PM, andyrblank said:

I have tried to take some of your suggestions and have improved the code of the Project (even if we disagree on necessity of router reboots): https://github.com/andyrblank/NetgearRouterReboot

 

I have also changed the code to use command line parameters with help from the new System.CommandLine package(s): https://www.nuget.org/packages/System.CommandLine

 

Thanks again for the feedback / peer review.

Being explicit is always better than being implicit. The people reading your code are often of varying levels of skill and you don't know what things you can take for granted. I don't see anything immediately catastrophically wrong, but there is still a lot of uses of var.

 

In C# 9 it's possible to omit the type in a new expression: "var routerCommands = new RouterCommands();" -> " RouterCommands routerCommands = new();", making the case for using var apply to an even smaller number of things.

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Newegg

×