Skip to content

Instantly share code, notes, and snippets.

@YDKK
Last active June 15, 2016 14:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save YDKK/4de3ff60811c94a9262efb709dc23940 to your computer and use it in GitHub Desktop.
Save YDKK/4de3ff60811c94a9262efb709dc23940 to your computer and use it in GitHub Desktop.
無線LANルータ(WN-AC1167DGR)を再起動させるやつ
using System;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using HtmlAgilityPack;
using ResetWlan.Properties;
namespace ResetWlan
{
class Program
{
public static void Main(string[] args)
{
CheckConfig();
Reboot().Wait();
}
static async Task Reboot()
{
using (var hc = new HttpClient())
{
#if !DEBUG
try
{
#endif
var byteArray = Encoding.ASCII.GetBytes($"{Settings.Default.ID}:{Settings.Default.Pass}");
hc.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));
const string url = "http://192.168.0.2/tlreset.htm";
var html = await hc.GetStringAsync(url);
var r = new Regex(@"<script language=""javascript"">\ndocument\.write\('(.*?)'\)\n</script>", RegexOptions.Singleline);
html = r.Replace(html, "$1");//csrfトークンを書き出す
var doc = new HtmlDocument();
HtmlNode.ElementsFlags.Remove("form");//これをしないとformがノードにならない
doc.LoadHtml(html);
var form = doc.DocumentNode.ChildNodes.Descendants().First(x => x.Attributes["name"]?.Value == "RebootForm");
var param = form.ChildNodes.Where(x => x.Name == "input").ToDictionary(i => i.Attributes["name"].Value, i => i.Attributes["value"]?.Value);
param["functionIndex"] = "1";//再起動
var content = new FormUrlEncodedContent(param);
var responce = await hc.PostAsync(url, content);
if (responce.IsSuccessStatusCode)
{
Console.WriteLine($"{DateTime.Now.ToLongDateString()}{DateTime.Now.ToLongTimeString()} 再起動に成功しました");
Console.WriteLine(responce.ReasonPhrase);
}
else
{
throw new Exception(responce.ReasonPhrase);
}
#if !DEBUG
}
catch (Exception e)
{
Console.WriteLine($"{DateTime.Now.ToLongDateString()}{DateTime.Now.ToLongTimeString()} 再起動に失敗しました");
Console.WriteLine(e.Message);
}
#endif
}
}
private static void CheckConfig()
{
if (Settings.Default.ID == "")
{
Console.Write("Input ID:");
Settings.Default.ID = Console.ReadLine();
}
if (Settings.Default.Pass == "")
{
Console.Write("Input Pass:");
Settings.Default.Pass = Console.ReadLine();
}
Settings.Default.Save();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment