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();
        }
    }

}