Categories: Programming

Using WinSCP .NET Wrapper with Host Key Switcher – C#.NET

I haven’t been writing to my blog for a long time. Last week I’ve used  .NET wrapper for a project. Usage of .NET wrapper written for WinSCP is very easy and there is a good documentation on that. So I won’t go through integrating winscp.dll to your c#.net project.

But the exact problem is hostkey. When you connect a remote server for first time, the client application asks you if you want to connect this server — yes or no ??? (it asks this question with server’s host name). And if you say yes to this question, you can connect to this server, other way it is not possible and you get authentication error. But please differ that; it is not an authentication regarding the server, it is an authentication to defense the client side.

The problem is it is not possible saying yes to the question of “do you know this host key?” with using dll of winscp. Because of that if you did not give exactly right host key with the SessionOptions object, you are in trouble.

In the function below, I got the server hostkey in catch code block, and I checked if this is an hostkey error or not, and then if it is; I update my hostkey with the new one and try to connect the system with new parameters again.

“Session_ac()” is my function, that I call to initialize my connection, connection parameters and open the session.

private void SessionAc()
        {
            try
            {
                // Setup session options
                sessionOptions = new SessionOptions
                {
                    Protocol = Protocol.Sftp,
                    HostName = "ip_address_of_host",
                    UserName = "username",
                    Password = "password",
                    SshHostKey = "ssh-rsa " + label3.Text
                };

                //Initialize the Session object
                session = new Session();
                session.Open(sessionOptions);
                //If connection is OK, use a label as status and output the connected state.
                label2.Text = "Bağlantı Sağlandı";
            }
            catch (Exception k)
            {
                //get the exception as "k" and pass it to variable "txt". 
                string txt = k.InnerException.ToString();

                //if the error contains the words "ssh-rsa " and "Host key", we can be sure that it is
                //an error releated to Host Key issue that we mentioned.
                if (txt.Contains("ssh-rsa ") && txt.Contains("Host key"))
                {
                    //label3 is the output and carrier of its value for my hostkey. And I update the new
                    //hostkey that written in my exception k.
                    label3.Text = txt.Substring(txt.IndexOf("1024 "), label3.Text.Length);
                    //After updating my host key variable, now I can connect with new parameters.
                    SessionAc();
                }
                else
                    label2.Text = "Bağlantı Kapalı";
                    //If this error is not releated to hostkey, do nothing and update my status
                    //text as "connection is closed"
            }
        }

burakalakus

View Comments

Recent Posts

Avaya SAL Gateway

Avaya SAL Gateway (Secure Access Link) is a remote connectivity solution used by Avaya to provide remote support for its… Read More

5 years ago

Change Linux Timezone (RHEL / CentOS)

You can change Linux Timezone via bash command line easily. All Timezone information are stored in /usr/share/zoneinfo/./etc/localtime is symlink to… Read More

5 years ago

DALI Protocol (Digital Addressable Lighting Interface)

DALI is a communication protocol that is used for the automation of lighting, which is not new at all. It… Read More

5 years ago

Manchester Code Explained

Communication requires being in sync just like an orchestra. What happens when the bass guitar in band is not sync… Read More

5 years ago

Secure Server Access with SSH Key without Password

You have access to many different Linux / Unix servers during the day, you are in the IT department, remembering… Read More

8 years ago

iPad Mini in CAR explained

CAR PC in car is now old school and I decided to mount an iPad mini into the dashboard. (more…) Read More

11 years ago