Monday, June 29, 2015

Take SSH from asp.net

Take SSH from asp.net
We can download .rdp file to connect to some server. Following code will create an rdp file and download that on client’s machine. Opening this file, user will be able to rdp specified server.

For this we need to have putty file on client machine. Here we will put putty.exe file in some specified download folder and create a putty.bat file to execute a command to run putty. We will edit configuration in this putty.bat file and mention the server ip and port(22) number to take SSH:

ASPX Code:
Note: script part will download the putty.exe file. Here we will download 2 files in single click.
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="SSH.aspx.cs" Inherits="_SSH" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script type="text/javascript">
        function btnclick() {
            setTimeout(function () {
                document.getElementById('btnpty').click();
            }, 500);
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <table>
                <tr style="background-color: #FF9900; color: #FFF">
                    <td colspan="2">RDP
                    </td>
                </tr>
                <tr style="background-color: #ECECEC; color: #000">
                    <td>Description
                    </td>
                    <td>Link
                    </td>
                </tr>
                <tr>
                    <td>SSH:
                    </td>
                    <td>
                        <asp:LinkButton runat="server" OnClientClick="javascript:return btnclick()" OnClick="LinkButton1_Click">127.0.0.3</asp:LinkButton>
                        <a href="download/putty.exe" id="btnpty">a </a>
                    </td>
                </tr>
            </table>
        </div>
    </form>
</body>
</html>

C# Code:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _SSH : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    private void DownLoadFile(string filePath)
    {
        System.IO.FileInfo file = new System.IO.FileInfo(filePath);
        if (file.Exists)
        {
            Response.Clear();
            Response.AddHeader("Content-Disposition", ("attachment; filename=" + file.Name));
            Response.AddHeader("Content-Length", file.Length.ToString());
            Response.ContentType = "application/octet-stream";
            Response.WriteFile(file.FullName);
            Response.End();
        }
    }

    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        string filePath = Server.MapPath("~/Download/putty.bat");
        UpdateBatFile("192.168.0.2", filePath);
        DownLoadFile(filePath);
    }
    private void UpdateBatFile(string IPAddress, string filePath)
    {
        StringBuilder newFile = new StringBuilder();
        string temp = "";
        string[] fileToUpdate = File.ReadAllLines(filePath);
        foreach (string line in fileToUpdate)
        {
            if (line.Contains("putty.exe"))
            {
                temp = temp.Insert(0, "putty.exe -ssh " + IPAddress + " 22");
                newFile.Append(temp + "\r\n");
                continue;
            }
            newFile.Append(line + "\r\n");
        }

        File.WriteAllText(filePath, newFile.ToString());
    }
}

RDP/RDC from asp.net


We can download .rdp file to connect to some server. Following code will create an rdp file and download that on client’s machine. Opening this file, user will be able to rdp specified server.

Foe this we need to create an rdp file in some download folder. Create a download folder and place a dummy download rdp file:


ASPX Code:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <table>
                <tr style="background-color: #FF9900; color: #FFF">
                    <td colspan="2">RDP
                    </td>
                </tr>
                <tr style="background-color: #ECECEC; color: #000">
                    <td>Description
                    </td>
                    <td>Link
                    </td>
                </tr>
                <tr>
                    <td>RDP1:
                    </td>
                    <td>
                        <asp:LinkButton runat="server" OnClick="lnkDownload_Click">127.0.0.1</asp:LinkButton>
                    </td>
                </tr>
            </table>
        </div>
    </form>
</body>
</html>

C# Code:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void lnkDownload_Click(object sender, EventArgs e)
    {
        string fileName = "Default.rdp";
        string filePath = Server.MapPath("~/Download/" + fileName);
        LinkButton linkButton = (LinkButton)sender;
        UpdateRDPFile(linkButton.Text, filePath);
        DownLoadFile(filePath);
    }

    private void UpdateRDPFile(string IPAddress, string filePath)
    {
        StringBuilder newFile = new StringBuilder();
        string temp = "";
        string[] fileToUpdate = File.ReadAllLines(filePath);
        foreach (string line in fileToUpdate)
        {
            if (line.Contains("full address:s:"))
            {
                temp = temp.Insert(0, "full address:s:" + IPAddress);
                newFile.Append(temp + "\r\n");
                continue;
            }
            newFile.Append(line + "\r\n");
        }

        File.WriteAllText(filePath, newFile.ToString());
    }

    private void DownLoadFile(string filePath)
    {
        System.IO.FileInfo file = new System.IO.FileInfo(filePath);
        if (file.Exists)
        {
            Response.Clear();
            Response.AddHeader("Content-Disposition", ("attachment; filename=" + file.Name));
            Response.AddHeader("Content-Length", file.Length.ToString());
            Response.ContentType = "application/octet-stream";
            Response.WriteFile(file.FullName);
            Response.End();
        }
    }

}

Wednesday, June 17, 2015

C#, asp.net: Fetch filtered data(string,date,int) from Azure Storage Table using .net

In my previous post I explained how can we fetch data from Azure Storage Table. Now I am going to explain how can we fetch filtered data:
Please visit previous post to start from scratch:


static void Main(string[] args)
{
    int counter = 0;
    StringBuilder sb = new StringBuilder();

    // Retrieve the storage account from the connection string.
    CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
    CloudConfigurationManager.GetSetting("StorageConnectionString"));

    // Create the table client.
    CloudTableClient tableClient = storageAccount.CreateCloudTableClient();

    // Create the CloudTable object that represents the "TableName1" table.
    CloudTable table = tableClient.GetTableReference("TableName1");

    // Construct the query operation for all customer entities where PartitionKey!="nitin".
    string FilterCounterName = TableQuery.GenerateFilterCondition("ColumnName1", QueryComparisons.Equal, "nitin dhiman");
    string FilterTimeStamp = TableQuery.GenerateFilterConditionForDate("TIMESTAMP", QueryComparisons.GreaterThan, DateTimeOffset.Now.AddDays(-1).Date);

    string FinalFilter = TableQuery.CombineFilters(FilterCounterName, TableOperators.And, FilterTimeStamp);

    TableQuery<TableName1> query = new TableQuery<TableName1>().Where(FinalFilter);
    // Print the fields for each customer.
    foreach (TableName1 entity in table.ExecuteQuery(query))
    {
        if (counter > 500) break;
        counter++;
        Console.WriteLine("{0}, {1}, {2}, {3}, {4}, {5}, {6}",
        entity.PartitionKey,
        entity.RowKey,
        entity.CustomCloumn1,
        entity.CustomCloumn2,
        entity.CustomCloumn3,
        entity.CustomCloumnN,
        entity.TIMESTAMP);
        sb.AppendFormat("{0}, {1}, {2}, {3}, {4}, {5}, {6}",
        entity.PartitionKey,
        entity.RowKey,
        entity.CustomCloumn1,
        entity.CustomCloumn2,
        entity.CustomCloumn3,
        entity.CustomCloumnN,
        entity.TIMESTAMP);
    }
    Console.WriteLine(counter);
    Console.WriteLine(sb.ToString());

    Console.WriteLine("Done... press a key to end.");
    Console.ReadKey();
}

Congrats All Done. Run and check. Let me know if you face any issue.