Last active
May 24, 2019 01:56
-
-
Save lennybacon/11138168 to your computer and use it in GitHub Desktop.
Remote control supermicro super server via ATEN IPMI in C#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <summary> | |
/// Ipmi remote interface to control supermicro super server via ATEN IPMI. | |
/// </summary> | |
public class Ipmi | |
{ | |
private readonly string _hostName; | |
private readonly CookieContainer _cookieContainer; | |
private const string LoginUrlFormat = | |
"https://{0}/cgi/login.cgi"; | |
private const string PowerUrlFormat = | |
"https://{0}/cgi/ipmi.cgi?POWER_INFO.XML=(1%2C{1})&time_stamp={2}&_="; | |
private const string AcceptHeaderValue = | |
"text/html," + | |
"application/xhtml+xml," + | |
"application/xml;q=0.9," + | |
"image/webp," + | |
"*/*" + | |
";q=0.8"; | |
private const string UserAgentHeaderValue = | |
"Mozilla/5.0 (Windows NT 6.3; WOW64) " + | |
"AppleWebKit/537.36 (KHTML, like Gecko) " + | |
"Chrome/34.0.1847.116 " + | |
"Safari/537.36"; | |
/// <summary> | |
/// Initializes a new instance of the <see cref="Ipmi"/> class. | |
/// </summary> | |
/// <param name="hostName">Name of the host.</param> | |
public Ipmi(string hostName) | |
{ | |
_hostName = hostName; | |
_cookieContainer = new CookieContainer(); | |
} | |
#region Login() | |
/// <summary> | |
/// Logins the specified user name. | |
/// </summary> | |
/// <param name="userName">Name of the user.</param> | |
/// <param name="password">The password.</param> | |
/// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns> | |
public bool Login( | |
string userName, | |
string password) | |
{ | |
_cookieContainer.Add( | |
new Cookie("langSetFlag", "0") { Domain = _hostName } | |
); | |
_cookieContainer.Add( | |
new Cookie("language", "English") { Domain = _hostName } | |
); | |
userName = System.Web.HttpUtility.UrlEncode(userName); | |
password = System.Web.HttpUtility.UrlEncode(password); | |
return ProcessWebOperation( | |
_cookieContainer, | |
_hostName, | |
string.Format( | |
CultureInfo.InvariantCulture, LoginUrlFormat, | |
_hostName | |
), | |
request => | |
{ | |
request.ContentType = "application/x-www-form-urlencoded"; | |
request.Method = "POST"; | |
request.ServicePoint.Expect100Continue = false; | |
var postString = @"name=" + userName + "&pwd=" + password; | |
var postBytes = Encoding.UTF8.GetBytes(postString); | |
request.ContentLength = postBytes.Length; | |
using (var stream = request.GetRequestStream()) | |
{ | |
stream.Write(postBytes, 0, postBytes.Length); | |
} | |
} | |
); | |
} | |
#endregion | |
#region TurnOn() | |
/// <summary> | |
/// Turns the on. | |
/// </summary> | |
/// <returns><c>true</c> if turned on, <c>false</c> otherwise.</returns> | |
public bool TurnOn() | |
{ | |
return ProcessWebOperation( | |
_cookieContainer, | |
_hostName, | |
string.Format( | |
CultureInfo.InvariantCulture, | |
PowerUrlFormat, | |
_hostName, | |
1, | |
BuildTimeStamp() | |
) | |
); | |
} | |
#endregion | |
#region TurnOff() | |
/// <summary> | |
/// Turns the off. | |
/// </summary> | |
/// <returns><c>true</c> if turned off, <c>false</c> otherwise.</returns> | |
public bool TurnOff() | |
{ | |
return ProcessWebOperation( | |
_cookieContainer, | |
_hostName, | |
string.Format( | |
CultureInfo.InvariantCulture, | |
PowerUrlFormat, | |
_hostName, | |
5, | |
BuildTimeStamp() | |
) | |
); | |
} | |
#endregion | |
#region ProcessWebOperation() | |
private static bool ProcessWebOperation( | |
CookieContainer cookieContainer, | |
string hostName, | |
string url, | |
Action<HttpWebRequest> requestAction = null) | |
{ | |
HttpWebResponse response = null; | |
try | |
{ | |
var request = | |
BuildHttpWebRequest( | |
cookieContainer, | |
hostName, | |
url | |
); | |
if (requestAction != null) | |
{ | |
requestAction(request); | |
} | |
response = (HttpWebResponse)request.GetResponse(); | |
} | |
catch (WebException ex) | |
{ | |
if (ex.Status == WebExceptionStatus.ProtocolError) | |
{ | |
response = (HttpWebResponse)ex.Response; | |
Console.WriteLine(response.StatusCode); | |
} | |
else | |
{ | |
ReportError(ex.ToString()); | |
return false; | |
} | |
} | |
catch (Exception ex) | |
{ | |
ReportError(ex.ToString()); | |
return false; | |
} | |
finally | |
{ | |
if (response != null) | |
{ | |
response.Close(); | |
} | |
} | |
return true; | |
} | |
#endregion | |
#region BuildTimeStamp() | |
private static string BuildTimeStamp() | |
{ | |
var date = DateTime.Now; | |
var utcDate = date.ToUniversalTime(); | |
var timeZoneName = | |
date.IsDaylightSavingTime() | |
? TimeZone.CurrentTimeZone.DaylightName | |
: TimeZone.CurrentTimeZone.StandardName; | |
var timeStamp = | |
utcDate.ToString("D").Substring(0, 3) + ""; | |
timeStamp += | |
utcDate.ToString("M").Substring(0, 3) + " "; | |
timeStamp += | |
utcDate.Day + " " + utcDate.Year + " "; | |
timeStamp += | |
utcDate.ToString("HH:mm:ss") + " GMT"; | |
timeStamp += date.ToString("zzz"); | |
timeStamp += | |
" (" + | |
timeZoneName + | |
")"; | |
return timeStamp; | |
} | |
#endregion | |
#region BuildHttpWebRequest() | |
private static HttpWebRequest BuildHttpWebRequest( | |
CookieContainer cookieContainer, | |
string hostName, | |
string url) | |
{ | |
var request = (HttpWebRequest)WebRequest.Create(url); | |
request.CookieContainer = cookieContainer; | |
request.KeepAlive = true; | |
request.Headers.Set(HttpRequestHeader.CacheControl, "max-age=0"); | |
request.Accept = AcceptHeaderValue; | |
request.Headers.Add("Origin", "https://" + hostName + ""); | |
request.UserAgent = UserAgentHeaderValue; | |
request.Referer = "https://" + hostName + "/"; | |
request.Headers.Set(HttpRequestHeader.AcceptEncoding, "gzip,deflate,sdch"); | |
request.Headers.Set(HttpRequestHeader.AcceptLanguage, "en-US,en;q=0.8"); | |
return request; | |
} | |
#endregion | |
#region ReportError() | |
private static void ReportError(string message) | |
{ | |
var color = Console.ForegroundColor; | |
Console.ForegroundColor = ConsoleColor.Red; | |
Console.WriteLine(message); | |
Console.ForegroundColor = color; | |
} | |
#endregion | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment