SftpClient using Url instead of IP as host - c#

I am using the following code to upload a file to an sFTP server. I have done a test upload using FileZilla and the file is uploaded successfully.
try
{
var client = new SftpClient(host, port, username, password);
client.Connect();
client.ChangeDirectory(workingDir);
var listDirectory = client.ListDirectory(workingDir);
foreach (var file in files)
{
var fileStream = new FileStream(file, FileMode.OpenOrCreate);
client.BufferSize = 4 * 1024; // bypass payload error large files
client.UploadFile(fileStream, Path.GetFileName(file));
Log.Info(string.Format("File [{0}] upload complete",file));
}
}
catch (Exception ex)
{
Log.Error(ex.Message);
}
But in the code above, I cannot seem to get the upload done because I get the following error:
No such host is known
And that's maybe because I am using the Url value of my host instead of IP? (I have done a test with another FtP server using IP and that also seems to be working) I'm wondering if that's the case? If so, is there a way to let SftpClient (Ssh.Net) handle the Url?

The host parameter may be an IP address or a host name that can be resolved to an IP address. It may not be a URL, that is something completely different from a technical point of view.
The fact that some full application accepts something else than a host name says little: it splits that string into separat tokens and connects to the IP address it gets returned when resolving the host name. But that does not mean that you can open any network connection to a URL or some arbitrary string. It is only possible to open a connection to an IP address.
So if you successfully test some URL like sftp://ftp.example.com/ContactImport in some "program", this does not mean that you can internally use that string as host parameter in your code. You need to use the host name that is a part of such URL, so ftp.example.com in this case, since only that can be successfully resolved to an IP address.

Related

Hortonworks webhdfs i try to list all folder it will work on Hortonworks console using curl command but not in C#

I try to list the name of the folder using webhdfs in C#. URL working fine using curl in sandbox but not in C# in my laptop
Error Message-
SocketException: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond
Code:
static void Main(string[] args)
{
Uri myUri = new Uri("http://hostname:50070/webhdfs/v1/user/hive/warehouse");
string userName = "myuser";
WebHDFSClient hdfsClient = new WebHDFSClient(myUri, userName);
string strDirectoryPath= "/user/hive/warehouse";
ArrayList l = new ArrayList();
l.Add(hdfsClient.GetDirectoryStatus(
strDirectoryPath).Result.Directories);
}
few issues:
your URI is "http://hostname:50070/webhdfs/v1/user/hive/warehouse", and your strDirectoryPath= "/user/hive/warehouse" - so your duplicating the path to be "http://hostname:50070/webhdfs/v1/user/hive/warehouse/user/hive/warehouse" which does not exist
you user "myuser" may not have permissions access to /user/hive/warehouse - check its permissions by "hdfs dfs -ls /user/hive/warehouse"
(just checking) the 'hostname' in the URI - is just for the question and not in the actual code, right? you'll need the hostname/ip of the sandbox

C# connect to remote server using Microsoft Terminal Services Active Client (RDP)

I have a piece of code that should connect to the server. The code is as following:
var rdp = new MsRdpClient8NotSafeForScripting();
rdp.Server = "192.168.0.101"; //adress
rdp.Domain = "localdomain"; //domain
rdp.UserName = "test"; //login
rdp.AdvancedSettings8.ClearTextPassword = "123456";//password
try
{
rdp.Connect();
}
catch (Exception e)
{
Console.WriteLine(e);
}
Console.WriteLine(rdp.Connected);
if (rdp.Connected != 0)
{
rdp.Disconnect();
}
Console.ReadLine();
This is supposed to "connect" to my remote server via 3389 port so that I can be able to read a file from my desktop which is called: "min.txt"
So far I have tried specifying the login data of my server but I always just get output of "0" in the console's window, regardless of whether I specify correct or incorrect login data..
My questions here are:
Why is it connecting even with wrong login data (ip, user + password)
How can I, once I've been indeed successfully connected to the server, access the min.txt file on my remote server, which is located at desktop...
Can someone help me out?
Probably you can try specifying the password like below:
MSTSClib.IMsTscNonScriptable secured = (MSTSClib.IMsTscNonScriptable)rdp.GetOcx();
secured.ClearTextPassword = “123456”;
For reference: MSDN link is here
Once connected, you can access the file like a shared network file via UNC.
Example:
System.IO.FileStream stream = System.IO.File.OpenRead("\\servername\sharedname\path\somefile.txt");
Then need to ensure that permissions are in place to access the folder.

Use SignalR with IP Address instead of computer name

I have a SignalR service setup to run self hosted in a Windows Service.
When I enter this address:
http://mycomputer.mydomain.net:8789/signalr/signalr/negotiate
I get some xml (for negotiation) displayed in the browser (showing that hit the service correctly.)
But if I enter this address (that has mycomputer.mydomain.net's ip address in place of the computer name):
http://10.92.15.6:8789/signalr/signalr/negotiate
or this
http://localhost:8789/signalr/signalr/negotiate
I get this error:
Bad Request - Invalid Hostname
HTTP Error 400. The request hostname is invalid.
I have tried also using:
http://10.92.15.6.mydomain.net:8789/signalr/signalr/negotiate
But I just get:
This site can’t be reached
10.92.15.6.mydomain.net’s server DNS address could not be found.
Is there someway to be able to make SignalR work using an IP Address instead of the computer name?
I had to change my startup from this:
string url = "http://" + machineName + ".mydomain.net:8789";
server = WebApp.Start<Startup>(url);
to this:
string url = "http://*:8789";
var startOptions = new StartOptions(url);
server = WebApp.Start<Startup>(startOptions);

.NET WebClient works on my localhost but not on Azure

I'm trying to FTP a file from Azure up to some FTP destination.
When I do it locally, it's working great. Ok ...
I publish this webjob up to azure and it's works great until it tries to do the FTP bit.
The following error is shown:
[12/21/2015 03:18:03 > 944464: INFO] 2015-12-21 03:18:03.6127| ERROR| Unable to connect to the remote server. Stack Trace: at System.Net.WebClient.UploadFile(Uri address, String method, String fileName)
[12/21/2015 03:18:03 > 944464: INFO] at System.Net.WebClient.UploadFile(String address, String method, String fileName)
And this is my simple code...
public void UploadFile(FileInfo fileInfo)
{
try
{
using (var client = new WebClient())
{
var destination = new Uri($"ftp://{_server}/{fileInfo.Name}");
client.Credentials = new NetworkCredential(_username, _password);
client.UploadFile(destination, "STOR", fileInfo.FullName);
}
}
catch (Exception exception)
{
// snipped.
throw;
}
}
So it's pretty damn simple.
Could this be some weird PASSIVE/ACTIVE issue? Like, how our office internet is simple while azure's setup is complex and ... as such ... it just cannot create the FTP connection?
Now - this is where things also get frustrating. Originally I was using a 3rd party FTP library and that DID work .. but some files were only getting partially uploaded .. which is why i'm trying to use WebClient instead. So I know that my azure webjob "website" thingy can FTP up. (no blocked ports, etc).
Any clues to what I can do, here?
Following Kristensen's simple example, I could do it. But I'm not sure about the format of your FTP url destination.
Did you try:
var destination = new Uri(string.Format("ftp://{0}/{1}", _server,fileInfo.Name));
Assuming _server is defined somewhere, I don't see it in your code and that your credentials have permissions to write on that location.

Url works when pinging but causes UriFormatException

I have a mail server I'm trying to connect to with exchange web services. If I ping the server, it works, but it gets a UriFormatException when provided in code.
Urls that work in command prompt but fail in c#
myserver.mydomain.com
myserver
192.168.100.1 (my server's ip)
Urls that can be parsed into URIs but fail to be pinged
http://myserver.mydomain.com
http://192.168.100.1
I also tried adding \\ to the beginning but had no luck.
We do have a bit of a weird setup with connecting to our domain when on-network that I believe is what is causing http://myserver.mydomain.com to fail in ping. How can I turn the base url (without the http://) into a string that will be valid for a c# Uri?
Code:
var serverUrl = "myserver.mydomain.com"; //base string I'd like to use
_exchange.Url = new Uri(serverUrl); //causes UriFormatException: Invalid URI: The format of the URI could not be determined.
To consturct Uri from host name use UriBuilder:
var builder = new UriBuilder();
builder.Host = "myserver.mydomain.com";
var uri = builder.Uri;
Note that what you call "uri" (myserver.mydomain.com) is actually "host name" or "DNS name" which is what get resolved to IP and than used to Ping. "http://myserver.mydomain.com" is absolute Uri for particular protocol (HTTP).

Categories