using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Reflection;
using System.Text;
using System.Web;
namespace Huseyint.ResetModem
{
class Program
{
static void Main(string[] args)
{
if (args.Length == 1 && args[0].Equals("/?"))
{
PrintHelp();
}
else
{
Dictionary<string, string> arguments = GetArgs(args);
string ip = arguments.ContainsKey("/ip") ? arguments["/ip"] : "192.168.2.1";
string password = arguments.ContainsKey("/password") ? arguments["/password"] : "";
string airtiesUrl = string.Format("http://{0}/cgi-bin/webcm", ip);
password = HttpUtility.UrlEncode(password);
string loginCmd = "getpage=..%2Fhtml%2Fhome%2Findex.html&errorpage=..%2Floginm.html" +
"&var%3Apagename=home&var%3Aerrorpagename=home&var%3Amenu=home&va" +
"r%3Amenutitle=Home&var%3Apagetitle=Home&var%3Apagemaster=home&=%" +
"3C%3F+error+found+%3F%3E&login%3Acommand%2Fusername=&login%3Acom" +
"mand%2Fpassword=" + password;
string resetCmd = "logic%3Acommand%2Freboot=";
// Login
MakePost(airtiesUrl, loginCmd);
// Reset
MakePost(airtiesUrl, resetCmd);
Console.WriteLine("Reset signal sent successfully to AirTies modem at {0}", ip);
}
}
private static void PrintHelp()
{
AssemblyName assemblyName = Assembly.GetExecutingAssembly().GetName();
Console.WriteLine("AirTies ADSL Modem Reset Utility {0}", assemblyName.Version);
Console.WriteLine("Copyright (C) Hüseyin Tüfekçilerli 2008 - http://huseyint.com/");
Console.WriteLine("");
Console.WriteLine("Syntax: {0} [Options]", assemblyName.Name);
Console.WriteLine("Options:");
Console.WriteLine("\t/ip:IPAddress\t\tIP address of the modem");
Console.WriteLine("\t/password:Password\tYour modem's password");
Console.WriteLine("");
Console.WriteLine("Both parameters are optional. Sample usage:");
Console.WriteLine("");
Console.WriteLine("\t{0} /ip:192.168.2.1 /password:123456", assemblyName.Name);
Console.WriteLine("\t{0} /password:123456", assemblyName.Name);
Console.WriteLine("\t{0} /ip:192.168.2.1", assemblyName.Name);
Console.WriteLine("\t{0}", assemblyName.Name);
Console.WriteLine("");
}
static Dictionary<string, string> GetArgs(string[] args)
{
Dictionary<string, string> retVal = new Dictionary<string, string>();
foreach (string arg in args)
{
int colon = arg.IndexOf(':');
if (colon >= 0)
{
string key = arg.Substring(0, colon).Trim();
string value = arg.Substring(colon + 1).Trim();
retVal.Add(key, value);
}
}
return retVal;
}
static void MakePost(string url, string postData)
{
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
request.AllowAutoRedirect = false;
request.Method = "POST";
ASCIIEncoding encoding = new ASCIIEncoding();
byte[] postDataBytes = encoding.GetBytes(postData);
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = postDataBytes.Length;
using (Stream newStream = request.GetRequestStream())
{
newStream.Write(postDataBytes, 0, postDataBytes.Length);
}
}
}
}