How to get the sftp server name the clinet is connecting to

0 votes
asked Jan 26, 2012 by keshav77 (120 points)
edited Jan 31, 2012

Hi I have used the below lines of to post files.

Here is the line of code which I am using to retrieve the Server which the application is connecting to .

SFTPServerName = sftp.Session.ServerName;

but it is returning the SFTPURL but not the name of the server.

Kindly help me in resolving the issue.

public bool SFtpTheFile(string fileName, string sFtpServerurl, string sFtpDirectory, string userName, string password)

    {
        bool isSuccess = false;
        try

        {
            Sftp sftp = new Sftp();
            sftp.Connect(sFtpServerurl );
            sftp.Login(userName, password);
            sftp.PutFiles(fileName, sFtpDirectory, SftpBatchTransferOptions.Default, SftpActionOnExistingFiles.OverwriteAll);
            isSuccess = true;
            sftp.RemoteEndPoint.
            GetServerName = sftp.Session.ServerName;
            sftp.Disconnect();

        }
        catch(Exception ex)
        {
            isSuccess = false;
            ErrorMessage = ex.Message;
        }
        return isSuccess;
    }
Applies to: Rebex SFTP

3 Answers

0 votes
answered Jan 26, 2012 by Tomas Knopp (58,580 points)
edited Jan 30, 2012

Hello

the ServerName is a property which contains the argument from the Connect() method.

If you are looking for translating with DNS, please try

using System.Net;    
IPHostEntry entry = Dns.GetHostEntry(serverName);
Console.WriteLine(entry.HostName);

Hope it helps,

with kind regards Tomas Knopp Rebex.NET

commented Jan 27, 2012 by keshav77 (120 points)
edited Jan 27, 2012

It doesn't worked for me still that is returning the same.

I will explain you my requirement.

SFTP is setup in SFTP Server Farm.

The client urls are provided to connect and post the files. In the SFTP Farm environment, If from a SFTP client is connecting and posting their files I have a requirement to find out which server it is connecting to. Hope the additional information will help me getting a resolution. Thanks in advance

commented Jan 29, 2012 by keshav77 (120 points)
edited Jan 29, 2012

Thanks for your help. The commands are not recognized I get operation timed ou.We need the sever name for finding the issues when posting a file to sftp environment. I need to check the Server Enviroment

commented Jan 30, 2012 by keshav77 (120 points)
edited Jan 30, 2012

The server environment is Windows Server 2008 R2 Standard SP1 64-bit

0 votes
answered Jan 27, 2012 by Tomas Knopp (58,580 points)
edited Jan 27, 2012

It didn't work, because the SFTP server farm appears to the client as one server. With your SFTP client you connect to the farm and the rest is business of the farm. So even if you might be assigned to one of the servers that form the farm, the SFTP still returns the name of the whole farm, because it doesn't know anything else.

So the SFTP protocol itself cannot help you, but SFTP runs over SSH, which could do what you want.

One soulution that might work is to run command on the server you are connected to. (If the server allows it, of course.) You can use the SftpCommandRunner helper class.

A sample of use:

         Sftp client = new Sftp();
         client.Connect(…);
         client.Login(…);

         string hostname = SftpCommandRunner.RunCommand(client, "hostname -f");
         string ipaddress = SftpCommandRunner.RunCommand(client, "hostname -i");
         Console.WriteLine(hostname);
         Console.WriteLine(ipaddress);

However, the commands are definitely OS dependant. The example would work on servers with unix systems on it.

By the way, why do you want to do it at all?

0 votes
answered Jan 30, 2012 by Tomas Knopp (58,580 points)
edited Jan 31, 2012

Hello

ok try to use the command echo %COMPUTERNAME% or alternatively hostname.

commented Jan 31, 2012 by keshav77 (120 points)
edited Jan 31, 2012

Still No luck.I am getting timed out exception only.

commented Jan 31, 2012 by Lukas Pokorny (88,510 points)
edited Jan 31, 2012

In that case, please try adding the following line to the RunCommand method (from SftpCommandRunner helper class) to produce a log that contains the communication between the client and the server:

        ...
        channel = sftp.Session.OpenSession();

        // add the following line:
        sftp.Session.LogWriter = new Rebex.FileLogWriter(@"c:\temp\sshexeclog.txt", Rebex.LogLevel.Verbose);
        // (change the log path as needed)

        channel.RequestExec(command);
        ...

Then, please mail the log to support@rebex.net for analysis. We should be able to tell what is going on!

...