0 votes
by (120 points)
edited

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
by (58.9k points)
edited

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

by (120 points)
edited

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

by (120 points)
edited

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

by (120 points)
edited

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

0 votes
by (58.9k points)
edited

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
by (58.9k points)
edited

Hello

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

by (120 points)
edited

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

by (144k points)
edited

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!

...