Tag Archive for server

Simple SQL Server Diff Tool

using System;
using System.Collections.Generic;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using System.Linq;

namespace Sdiff
{
    internal class Program
    {
        /// <param name = "args">arg[0] old schema connection string, arg[1] target schema connection string</param>
        /// <summary>
        ///   Tells you what to do with old schema to get it into the shape of the new
        /// </summary>
        private static void Main(string[] args)
        {
            var diffs = new List<Diff>();

            var oldContext = new IsDataContext(args[0]);
            var newContext = new IsDataContext(args[1]);

            var newTableNames = newContext.Tables.Select(x => x.Name).ToList();
            var oldTableNames = oldContext.Tables.Select(x => x.Name).ToList();

            AddDiffs(diffs, oldTableNames.Except(newTableNames), DiffKind.TableDelete);
            AddDiffs(diffs, newTableNames.Except(oldTableNames), DiffKind.TableCreate);

            var commonTableNames = newTableNames.Intersect(oldTableNames);
            foreach (var tableName in commonTableNames)
            {
                var newColumns = newContext.Columns.Where(x => x.TableName == tableName).ToList();
                var oldColumns = oldContext.Columns.Where(x => x.TableName == tableName).ToList();

                var newColumnNames = newColumns.Select(x => x.FullName);
                var oldColumnNames = oldColumns.Select(x => x.FullName);

                AddDiffs(diffs, oldColumnNames.Except(newColumnNames), DiffKind.ColumnDelete);
                AddDiffs(diffs, newColumnNames.Except(oldColumnNames), DiffKind.ColumnCreate);

                var commonColumnNames = newColumnNames.Intersect(oldColumnNames);
                foreach (var commonColumnName in commonColumnNames)
                {
                    var newDataType = newColumns.Where(x => x.FullName == commonColumnName).Select(x => x.DataType).Single();
                    var oldDataType = oldColumns.Where(x => x.FullName == commonColumnName).Select(x => x.DataType).Single();
                    if (oldDataType != newDataType) diffs.Add(new Diff(commonColumnName + " " + oldDataType + " -> " + newDataType, DiffKind.ColumnDataTypeChange));
                }
            }

            WriteSection(diffs.Where(x => x.Kind == DiffKind.TableDelete), "Tables to delete");
            WriteSection(diffs.Where(x => x.Kind == DiffKind.TableCreate), "Tables to create");
            WriteSection(diffs.Where(x => x.Kind == DiffKind.ColumnDelete), "Columns to delete");
            WriteSection(diffs.Where(x => x.Kind == DiffKind.ColumnCreate), "Columns to create");
            WriteSection(diffs.Where(x => x.Kind == DiffKind.ColumnDataTypeChange), "Columns to modify");

            Console.ReadKey();
        }

        private static void AddDiffs(List<Diff> diffs, IEnumerable<string> names, DiffKind diffKind)
        {
            diffs.AddRange(names.Select(name => new Diff(name, diffKind)));
        }


        private static void WriteSection(IEnumerable<Diff> diffs, string title)
        {
            if (!diffs.Any()) return;
            Console.WriteLine();
            Console.WriteLine(title);
            Console.WriteLine(string.Empty.PadLeft(title.Length, '-'));
            foreach (var name in diffs.Select(x => x.Name).OrderBy(x => x))
            {
                Console.WriteLine(name);
            }
        }

        private class Diff
        {
            public Diff(string name, DiffKind kind)
            {
                Name = name;
                Kind = kind;
            }

            public string Name { get; private set; }
            public DiffKind Kind { get; private set; }
        }

        internal enum DiffKind
        {
            TableDelete,
            TableCreate,
            ColumnDelete,
            ColumnCreate,
            ColumnDataTypeChange
        }
    }


    public class IsDataContext : DataContext
    {
        public Table<Column> Columns;
        public Table<Table> Tables;
        public IsDataContext(string connection) : base(connection) {}
    }

    [Table(Name = "INFORMATION_SCHEMA.TABLES")]
    public class Table
    {
        [Column(Name = "TABLE_NAME")] public string Name;
    }

    [Table(Name = "INFORMATION_SCHEMA.COLUMNS")]
    public class Column
    {
        [Column(Name = "CHARACTER_MAXIMUM_LENGTH")] public int? CharacterMaximumLength;
        [Column(Name = "DATA_TYPE")] public string DataTypeName;
        [Column(Name = "COLUMN_NAME")] public string Name;
        [Column(Name = "TABLE_NAME")] public string TableName;

        public string DataType
        {
            get
            {
                if (!CharacterMaximumLength.HasValue) return DataTypeName;
                if (CharacterMaximumLength.Value == -1) return DataTypeName + "(MAX)";
                return DataTypeName + "(" + CharacterMaximumLength.Value + ")";
            }
        }

        public string FullName { get { return TableName + "." + Name; } }
    }
}

source

NQhost.com offers properly unmetered dedicated and virtual servers

There are several data-centers in USA and Germany. All services include unlimited traffic and no hidden fees.

source

Asynchronous ASP.Net Pages, AJAX Helper, WebRequest Passthrough

Main Class (AsyncCall.cs), place in App_Code directory or compile as a dll and place in the bin dirctory of the site:
-------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Net;
using System.Threading;
using System.IO;
using System.Collections.Specialized;

namespace AsyncStuff
{
	public static class AsyncCall
	{
		public sealed class AsyncState
		{
			internal const string QVARNAME = "ASYNCSession";

			public readonly Page CallingPage;
			internal readonly Uri Url;
			private HttpWebRequest _request;
			private HttpWebResponse _response;
			internal readonly SpecificFunction SpecificFunction;
			public readonly object[] Parameters;
			private string _text;
			internal byte[] FormContent;

			//cascaded constructor only (private)
			private AsyncState(Page page)
			{
				this.CallingPage = page;
				this.Url = GetAsyncUrl(page.Request.Url);
			}

			//off-page url
			internal AsyncState(Uri url, Page page, SpecificFunction specificFunction)
			{
				this.CallingPage = page;
				this.Url = GetAsyncUrl(url);
				this.SpecificFunction = specificFunction;
			}

			internal AsyncState(Page page, SpecificFunction specificFunction)
				: this(page)
			{
				this.SpecificFunction = specificFunction;
			}

			internal AsyncState(Page page, SpecificFunction specificFunction, params object[] parameters)
				: this(page, specificFunction)
			{
				this.Parameters = parameters;
			}

			private Uri GetAsyncUrl(Uri uri)
			{
				string url = uri.OriginalString;
				int start = url.IndexOf("?");
				if (start < 0)
				{
					start = url.Length;
					url = url.Insert(start++, "?");
				}
				else
				{
					start++;
					url = url.Insert(start, "&");
				}

				//timestamped query variable inserted to indicate that a page is running asychronously
				url = url.Insert(start, QVARNAME + "=" + DateTime.Now.ToString("MMddyyyyHHmmssffff"));
				return new Uri(url);
			}

			public HttpWebRequest Request
			{
				get { return this._request; }
				internal set { this._request = value; }
			}

			public HttpWebResponse Response
			{
				get { return this._response; }
				internal set { this._response = value; }
			}

			public string GetResponseText()
			{
				if (this._text == null)
				{
					this._text = "";
					if (this._response != null)
						this._text = (new StreamReader(this._response.GetResponseStream())).ReadToEnd();
				}
				if (this._text == "")
					return null;

				return this._text;
			}
		}

		/// <summary>
		/// A delegate function used to create a callback for the RunPageAsynchronously() function
		/// </summary>
		/// <param name="state">The state object that will be returned from the results of the asychronous call</param>
		public delegate void SpecificFunction(AsyncState state);

		/// <summary>
		/// Run an off-page url request as an asychronous request. (Note: this is a seperate session, so don't expect session variables to persist)
		/// </summary>
		/// <param name="url">Off-page URL which contents will be retreived asychronously</param>
		/// <param name="callingPage">The current System.Web.UI.Page object which is making the asychronous call (must implement IHttpAsyncHandler or use the [%@ Page Async="true"] directive)</param>
		/// <returns>true, if the page was able to run asynchronously (a value of false may indicate the %@ Page directive is not using Async="true"</returns>
		public static bool RunAsynchronously(string url, Page callingPage)
		{
			if (callingPage is IHttpAsyncHandler)
			{
				Uri absUrl = new Uri(url, UriKind.RelativeOrAbsolute);
				if (!absUrl.IsAbsoluteUri || absUrl.OriginalString.StartsWith("file"))
				{
					string resolve = callingPage.ResolveUrl(url);
					Uri pageUrl = callingPage.Request.Url;
					string newPath = pageUrl.OriginalString.Replace(pageUrl.PathAndQuery, resolve);
					absUrl = new Uri(newPath, UriKind.Absolute);

					string filePath = callingPage.MapPath(absUrl.AbsolutePath);
					FileInfo fi = new FileInfo(filePath);
					if (!fi.Exists)
						throw new Exception("File Not Found, requestsed relative path does not exist. Check the filename for spelling errors.");
				}
				AsyncState state = new AsyncState(absUrl, callingPage, new SpecificFunction(AfterOffPage));
				callingPage.AddOnPreRenderCompleteAsync(new BeginEventHandler(BeginAsyncOperation), new EndEventHandler(EndAsyncOperation), state);
				return true;
			}
			return false;
		}

		private static void AfterOffPage(AsyncState state)
		{
			if (state==null || state.Response == null || state.CallingPage==null || state.CallingPage.Response==null)
				return;

			//content rewrite (make calling page same as the page called)
			//requires IIS content rewrite pipline mode, so if exception then ignore
			try
			{
				state.CallingPage.Response.Headers.Clear();
				state.CallingPage.Response.Headers.Add(state.Response.Headers);
			}
			catch { }
			state.CallingPage.Response.ContentType = state.Response.ContentType;
			BinaryReader br = new BinaryReader(state.Response.GetResponseStream());
			int len = (int)state.Response.ContentLength;
			byte[] data = br.ReadBytes(len);
			state.CallingPage.Response.OutputStream.Write(data, 0, len);			
		
			state.CallingPage.Response.End();
		}

		/// <summary>
		/// Run the current page request as an asychronous request. (Note: this is a seperate session, so don't expect session variables to persist)
		/// </summary>
		/// <param name="callingPage">The current System.Web.UI.Page object to be processed (must implement IHttpAsyncHandler or use the [%@ Page Async="true"] directive)</param>
		/// <param name="specificFunction">A parameterless function to call on the async request page</param>
		/// <returns>true, if the page was able to run asynchronously (a value of false may indicate the %@ Page directive is not using Async="true"</returns>
		public static bool RunAsynchronously(Page callingPage, SpecificFunction specificFunction)
		{
			if ((callingPage is IHttpAsyncHandler) && !IsRunningAsync(callingPage))
			{
				AsyncState state = new AsyncState(callingPage, specificFunction);
				callingPage.AddOnPreRenderCompleteAsync(new BeginEventHandler(BeginAsyncOperation), new EndEventHandler(EndAsyncOperation), state);
				return true;
			}
			return false;
		}

		/// <summary>
		/// Run the current page request as an asychronous request. (Note: this is a seperate session, so don't expect session variables to persist)
		/// </summary>
		/// <param name="callingPage">The current System.Web.UI.Page object to be processed (must implement IHttpAsyncHandler or use the [%@ Page Async="true"] directive)</param>
		/// <param name="specificFunction">A function to call on the async request page</param>
		/// <param name="parameters">The parameters to pass to the specific function</param>
		/// <returns>true, if the page was able to run asynchronously (a value of false may indicate the %@ Page directive is not using Async="true"</returns>
		public static bool RunAsynchronously(Page callingPage, SpecificFunction specificFunction, params object[] parameters)
		{
			if ((callingPage is IHttpAsyncHandler) && !IsRunningAsync(callingPage))
			{
				AsyncState state = new AsyncState(callingPage, specificFunction, parameters);
				callingPage.AddOnPreRenderCompleteAsync(new BeginEventHandler(BeginAsyncOperation), new EndEventHandler(EndAsyncOperation), state);
				return true;
			}
			return false;
		}

		/// <summary>
		/// Tests to see if the page is in is asynchronous cycle of an asynchronous request made from the RunPageAsynchronously function
		/// </summary>
		/// <param name="page">The page to check for sychronicity</param>
		/// <returns>true if in the asychronous cycle</returns>
		public static bool IsRunningAsync(Page page)
		{
			return (page.Request.QueryString[AsyncState.QVARNAME] != null);
		}

		private static IAsyncResult BeginAsyncOperation(object sender, EventArgs e, AsyncCallback cb, object stateObj)
		{
			if (stateObj is AsyncState)
			{
				AsyncState state = (AsyncState)stateObj;
				state.Request = (HttpWebRequest)HttpWebRequest.Create(state.Url);
				
				//copy relevant header information
				state.Request.Accept = GetTextList<string>(state.CallingPage.Request.AcceptTypes, ", ", true);
				state.Request.AllowAutoRedirect = true;
				state.Request.AllowWriteStreamBuffering = true;
				state.Request.ContentType = state.CallingPage.Request.ContentType + "; " + state.CallingPage.Request.ContentEncoding.WebName;
				state.Request.Method = state.CallingPage.Request.RequestType;
				state.Request.Referer = state.CallingPage.Request.UrlReferrer.OriginalString;
				
				//copy cookies
				if (state.CallingPage.Request.Cookies.Count > 0)
				{
					state.Request.CookieContainer = new CookieContainer();
					List<string> excludedCookies = new List<string> { "ASP.NET_SessionId" };
					foreach (string key in state.CallingPage.Request.Cookies.Keys)
					{
						if (!excludedCookies.Contains(key, StringComparer.CurrentCultureIgnoreCase))
						{
							HttpCookie cookie = state.CallingPage.Request.Cookies[key];
							Cookie copy = new Cookie(cookie.Name, cookie.Value, state.Url.AbsolutePath, state.Url.Host);
							state.Request.CookieContainer.Add(copy);
						}
					}
				}
				
				//copy form variables
				if (state.CallingPage.Request.Form.Count > 0 && state.Request.Method.Equals("POST", StringComparison.CurrentCultureIgnoreCase))
				{
					string pairs = GetTextDictionary(state.CallingPage.Request.Form, "=", "&", true);					
					state.FormContent = state.CallingPage.Request.ContentEncoding.GetBytes(pairs);
					state.Request.ContentLength = state.FormContent.Length;
					state.Request.BeginGetRequestStream(EndRequestStreamCallback, state);
				}

				return state.Request.BeginGetResponse(cb, stateObj);
			}
			return null;
		}

		private static void EndRequestStreamCallback(IAsyncResult ar)
		{
			if (ar.AsyncState is AsyncState)
			{
				AsyncState state = (AsyncState)ar.AsyncState;				
				BinaryWriter sw = new BinaryWriter(state.Request.EndGetRequestStream(ar));
				sw.Write(state.FormContent, 0, state.FormContent.Length);
				sw.Close();
			}
		}

		private static void EndAsyncOperation(IAsyncResult ar)
		{
			if (ar.AsyncState is AsyncState)
			{
				AsyncState state = (AsyncState)ar.AsyncState;
				if (state.Request != null)
				{
					try { state.Response = (HttpWebResponse)state.Request.EndGetResponse(ar); }
					catch (Exception ex) { throw new Exception("WebRequest Error. Remember that asynchronous calls are made from the server and not the client, so any routing done on the client (say...to a testing server) will not be in effect with this request.", ex); };

					if (state.SpecificFunction != null)
					{
						object target = state.SpecificFunction.Target;
						if (target == null)
							target = state.CallingPage;
						try { state.SpecificFunction.Method.Invoke(target, new object[] { state }); }
						catch (Exception ex) { throw new Exception("An error occured in the SpecificFunction supplied, the debug thread is not attached to the function invoked, therefore further debug information is unavailable.", ex); }
					}
				}
			}
		}

		private static string GetTextList<I>(IEnumerable<I> list, string separator, bool urlEncode)
		{			
			System.Text.StringBuilder sb = new System.Text.StringBuilder();
			int index = 0;
			foreach (I item in list)
			{
				if (index > 0)
					sb.Append(separator);				
				if (urlEncode)				
					sb.Append(HttpUtility.UrlPathEncode(item.ToString()));
				else
					sb.Append(item.ToString());
				index++;
			}
			string ret = sb.ToString();
			sb.Length = 0; //destroy memory
			return ret;
		}

		private static string GetTextDictionary<K, V>(IDictionary<K, V> dictionary, string equality, string separator, bool urlEncode)
		{
			System.Text.StringBuilder sb = new System.Text.StringBuilder();
			int index = 0;
			foreach (K key in dictionary.Keys)
			{
				V value = dictionary[key];
				if (index > 0)
					sb.Append(separator);
				if (urlEncode)
					sb.Append(HttpUtility.UrlPathEncode(key.ToString()));
				else
					sb.Append(key.ToString());
				sb.Append(equality);
				if (urlEncode)
					sb.Append(HttpUtility.UrlPathEncode(value.ToString()));
				else
					sb.Append(value.ToString());

				index++;
			}
			string ret = sb.ToString();
			sb.Length = 0; //destroy memory
			return ret;
		}

		private static string GetTextDictionary(NameValueCollection dictionary, string equality, string separator, bool urlEncode)
		{
			System.Text.StringBuilder sb = new System.Text.StringBuilder();
			int index = 0;
			foreach (string key in dictionary.Keys)
			{
				string value = dictionary[key];
				if (index > 0)
					sb.Append(separator);
				if (urlEncode)
					sb.Append(HttpUtility.UrlPathEncode(key.ToString()));
				else
					sb.Append(key.ToString());
				sb.Append(equality);
				if (urlEncode)
					sb.Append(HttpUtility.UrlPathEncode(value.ToString()));
				else
					sb.Append(value.ToString());
				index++;
			}
			string ret = sb.ToString();
			sb.Length = 0; //destroy memory
			return ret;
		}



	}
}

Page Example (AsyncPage.aspx):
-------------------------------------------------------------------------------
<%@ Page Async="true" Language="C#" AutoEventWireup="true" CodeFile="AsyncPage.aspx.cs" Inherits="AsyncPage" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "<a href="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">" >http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"></a>

<html xmlns="<a href="http://www.w3.org/1999/xhtml">" >http://www.w3.org/1999/xhtml"></a>
<head runat="server">
    <title></title>
    <script type="text/javascript">
    	function AJAXCall(url, callBack, postData) {
    		//setup the callback
    		var out = callBack;
    		if (!out) {
    			out = function(text) { return; }
    		}

    		//setup the request
    		var request = null;
    		if (window.XMLHttpRequest)
    			request = new XMLHttpRequest();
    		else if (window.ActiveXObject)
    			request = new ActiveXObject("Microsoft.XMLHTTP");
    		else
    			return false;

    		//true for async..
    		request.open("POST", url, true);

    		//setup the handle of the request when the status changes
    		request.onreadystatechange = function() {
    			if (request && request.readyState == 4) {
    				//if (request.status == 200)
    				out(request.responseText);

    			}
    		}
    		//setup the request headers
    		request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

    		//send the request
    		if (postData)
    			request.send(postData);
    		else
    			request.send("");
    	}

    	function callback(data) {
    		alert('got here: ' + data);
    	}

    	function button1() {
    		AJAXCall("AsyncPage.aspx?LongFunction=1", callback, "testing=1");
    	}   	

    	function button2() {
    		AJAXCall("AsyncPage.aspx?ShortFunction=1", callback, "testing=1");
    	}

    	function button3() {
    		AJAXCall("AsyncPage.aspx?UrlFunction=1", callback, "testing=1");
    	}

    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
		<asp:Label ID="lbl1" runat="server" />
		<input type="button" id="testbutton1" value="TestLong" onclick="javascript:button1();" style="width:100px;Height:25px;" />
		<input type="button" id="testbutton2" value="TestShort" onclick="javascript:button2();" style="width:100px;Height:25px;" />
		<input type="button" id="testbutton3" value="TestUrl" onclick="javascript:button3();" style="width:100px;Height:25px;" />
    </div>
    </form>
</body>
</html>

Code Behind (AsyncPage.aspx.cs):
-------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.IO;
using System.Threading;
using AsyncStuff;

public partial class AsyncPage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {		
		if (AsyncCall.IsRunningAsync(Page))			
			CalledAsync(); //function is run when a page calls itself asychronously

		//a query variable triggers this page to run a different async function (use AJAX Call to this same url with this query variable)
		//these are single-run checked already, so you don't have to worry about running them more than once
		if (Request.QueryString["LongFunction"] != null)		
			AsyncCall.RunAsynchronously(Page, AfterLong, "Long call");

		if (Request.QueryString["ShortFunction"] != null)
			AsyncCall.RunAsynchronously(Page, AfterShort);

		if (Request.QueryString["UrlFunction"] != null)
			AsyncCall.RunAsynchronously("/AsyncTest.aspx?test=1", Page);
				
		//Normal Page load		
		lbl1.Text = "Just sitting here";
    }

	//this stuff called on the async thread
	protected void CalledAsync()
	{
		//stuff done during async
		
		if (Request.QueryString["LongFunction"]!=null)
		{
			//crunch some numbers to waste some time
			for(int i = 1; i<565535; i++)
			{
				decimal num = (decimal)DateTime.Now.Millisecond * (decimal)(new Random(DateTime.Now.Millisecond)).NextDouble();
				num += num;
			}
			Response.Write("Success");
		}
		else if (Request.QueryString["ShortFunction"] != null)
		{
			Response.Write("Short Call Was Run");
		}

		Response.End(); //do not show regular page contents
	}

	//this stuff called on the page thread after the async call returns
	protected void AfterLong(AsyncCall.AsyncState state)
	{
		//this is an example of using the response stream directly, you can use state.GetResponseText() to accomplish this same thing
		StreamReader sr = new StreamReader(state.Response.GetResponseStream());
		string pageContent = sr.ReadToEnd();
		if (pageContent.Contains("Success"))		
			Response.Write(state.Parameters[0].ToString());
		else		
			Response.Write("Failure");

		Response.End();
	}

	//this stuff called on the page thread after the async call returns
	protected void AfterShort(AsyncCall.AsyncState state)	
	{		
		string text = state.GetResponseText();
		if (text!=null)
			Response.Write(text);
		Response.End();
	}
}

source

Find which process is keeping a port busy

sudo lsof -i -P | grep PORTNUMBER

source

Server Switcher Bookmarklet

javascript:(function(){var d='localhost';var p='mydomain.com';var s=document.domain.indexOf(d)!=-1?p:document.domain.indexOf(p)!=-1?d:0;if(s)location.href=document.location.toString().replace(document.domain,s);})()

source

Find all databases and login credentials

SELECT domains.name AS domain_name,
data_bases.name AS database_name, db_users.login, accounts.password
FROM data_bases, db_users, domains, accounts
WHERE data_bases.dom_id = domains.id
AND db_users.db_id = data_bases.id
AND db_users.account_id = accounts.id
ORDER BY domain_name;

source

Get Plesk FTP account passwords w/o logging into MySQL

echo "SELECT REPLACE(sys_users.home,'/home/httpd/vhosts/','') AS domain,
sys_users.login,accounts.password FROM sys_users
LEFT JOIN accounts on sys_users.account_id=accounts.id
ORDER BY sys_users.home ASC;" | mysql -uadmin -p`cat /etc/psa/.psa.shadow` -Dpsa -t

source

Print out a list of open connections on your box and sorts them by according to IP address

netstat -atun | awk '{print $5}' | cut -d: -f1 | sed -e '/^$/d' |sort | uniq -c | sort -n

source

List unique IP addresses

netstat -nat | awk '{ print $5}' | cut -d: -f1 | sed -e '/^$/d' | uniq

source

lists all network protocol connections filtering out column 6 and sorting the unique entries by count

netstat -plan | awk '{print $6}' | sort | uniq -c

source