Monday, September 19, 2011

Connecting MySql over SSH using C#

I am sure many of you are looking for C# code to connect to MySql server over SSH.
I hope you are aware of SSH , if not please google SSH and know more about the SSH in details.


.Net does not have its own inbuilt methods or classes for connecting to MySql over SSH.
Hence you will need to download few open source dlls for the implementing your code to connect to MqSql over SSH.


I am going to use sharpSSH here. Thanks to Tamir for writing the code and sharing with the world.
You can download the library from here. Also you will need the secret key file in openSSH format to connect. You can get the key in openSSH format generated from puttygen.


Here is the code for MySql Connection over SSH using C#

using System;

using System.Collections;
using MySql.Data.MySqlClient;
using Tamir.SharpSsh.jsch;


namespace MySQLSSHConnection
{
class Program
{
static void Main(string[] args)
{
 MySqlConnection myConnection = null;
 Session sshSession = null;
 try
 {
  JSch jsch = new JSch();
                
  //Setting the secret key file
  jsch.addIdentity(@"D:\...\key-file");
  sshSession = jsch.getSession("username", hostname, 22);
                                
  Hashtable config = new Hashtable();
  config.Add("StrictHostKeyChecking", "No");            
  sshSession.setConfig(config);


  //Setting the SSH connection
  sshSession.connect();
                
  //Forwarding the remote port to local port over the connected session
  sshSession.setPortForwardingL(3306, "localhost", 3306);


  //Connecting to Mysql 
  string connstring = "Server=localhost;Port=3306;database=Account;Uid=****;Password=****";
  myConnection = new MySqlConnection(connstring);
  myConnection.Open();                
  Console.WriteLine("Open OK");
  Console.ReadLine();                
 }
 catch(Exception ex)
 {
   Console.WriteLine(ex.ToString());
   Console.ReadLine();
 }
 finally
 {
  //Closing database connection
  myConnection.Close();


  //Closing SSH connection
  sshSession.disconnect();
 }
}
}
}


Hope this post is helpful.



Tuesday, January 18, 2011

Automating IP address assignment to NICs

My workplace has two internet lines wired and wireless and me roaming all over office for meetings, discussions with my laptop greedy for bandwidth. Initially I use to set my IP address manually and reset them back to point to DHCP servers hopping over wireless network and plugging LAN cables. Its really boring to go to IP address window in my Vista and set the IP address. I am sure many of us had spending time (time=money) setting and resetting the IP addresses. So let me show you how you can Automate setting up your IP.

Windows comes with command line tool Netsh which lets you set the network details.
So lets start setting up LAN adapter with IPv4.
Go to your command prompt and type in
netsh interface ipv4 set address name = "Local Area Connection" source=static address= {ip you intend to assign to your machine} mask=255.255.255.0 gateway={your gateway ip address} gwmetric=1
Here is what you are doing
"nestsh interface ipv4 " we are going to set the IPV4 address and not IPV6.
"set address " ordering nestsh command to set the ip address.
"name="Local Area Connection" " Ordering netsh command to set the ip address of adapter with name as Local Area Connection which is your wired adapter on your machine.
"source= static" ordering nestsh to set the ip address manually and not from dhcp
"address=" your intended ipv4 address
"mask=" your subnet mask ip
"gateway=" your gateway ip address. The address of your or proxy
"gwmetric=1" its a metric for default gateway and should be set if you are specifying gateway

Press enter and you have set your Ip address. But all is not over yet. You may also want to set your primary and secondary dns addresses
So lets set our primary dns address. Type in

netsh interface ipv4 set dnsserver name = "Local Area Connection" source = static address= {your primary dns address}

once again we are telling netsh to set primary dns server with ipv4 ip address for interface named "Local Area Connection" manually

Lets set our secondary dns address
netsh interface ipv4 add dnsserver name = "Local Area Connection" address= {your secondary dns
address}

here we are telling netsh to add secondary dns server address.

You can create such scripts in file with .bat extension and whenever you want to set the addresses just run the scripts with elevated rights