I'm working on windows service that uploads files to sftp server using SSH.NET. Problem is when I run code responsible for sending file from windows console app everything works fine, but when exact the same code is running from windows service i recive exception when connecting to SFTP : "A connection can not be made because the target computer actively refuses it"
I have SFTP server running on the same machine for testing.
public void Send(string host, string userName, int port, string password, string filePath)
{
var connectionInfo = new ConnectionInfo(host, userName, new PasswordAuthenticationMethod(userName, password));
try
{
var sftpClient = new SftpClient(connectionInfo);
sftpClient.Connect(); // exception occurs there
using (FileStream fileStream = File.OpenRead(filePath))
{
sftpClient.UploadFile(fileStream, $"{Path.GetFileName(filePath)}", true);
}
sftpClient.Disconnect();
sftpClient.Dispose();
}
catch (Exception exception)
{
Logger.Log($"{host} | {password} | {port} | {userName}");
Logger.Log(exception.Message);
Logger.Log(exception.StackTrace);
}
}
Does anyone have an idea what is going on? I think that can be some configuration problem because like I said the same code runs fine from console app.
Related
I am trying to read and write data in a Linux machine from another server machine which is Windows OS using a console application exe file, since the Linux machine is accessible only from the server windows machine.
I have read json data from the .json file and updated it using the code mentioned below.
Now I want to write the same in the .json file by overwriting file with updated data and saving it.
try
{
AuthenticationMethod method = new PasswordAuthenticationMethod(appSettings.Value.Username, appSettings.Value.Password);
ConnectionInfo connection = new ConnectionInfo(appSettings.Value.Host, appSettings.Value.Username, method);
var client = new SshClient(connection);
client.Connect();
if (!client.IsConnected)
{
Console.WriteLine("Not Connected...");
}
else
{
Console.WriteLine("Connected...");
var readCommand = client.RunCommand("cat /AAA/BBB/CCC/DDD/main_copy.json");
//Console.WriteLine(readCommand.Result);
dynamic jsonObj = Newtonsoft.Json.JsonConvert.DeserializeObject(readCommand.Result);
jsonObj["Load_Frequency"] = "Monthly";
string output = Newtonsoft.Json.JsonConvert.SerializeObject(jsonObj, Newtonsoft.Json.Formatting.Indented);
//File.WriteAllText("settings.json", output);--here I need to write code that run Linux command for overwriting .json file with updated data.
//var writeCommand = client.RunCommand("mkdir \"/home/user/ssh_output\"");
Thread.Sleep(100000);
}
}
I tried below command:
var writeCommand = client.RunCommand("cat " + output + " > /AAA/BBB/CCC/DDD/main_copy.json");
But it is showing No such file or directory when I execute this command directly in the putty.
Can you help to write linux command for that.
I have Http server running on my PC, I can use localhost or 192.168.1.69 talk to the server.
I have a tablet in the same wifi as the PC, when I do 192.168.1.69:8080/hello
I don't get any response.
I tried set up port forwarding on router, added .exe to firewall trust, also tried use public ip(that open router login page) with port number. but they doesn't help,
public HttpServer(int port)
{
if (!HttpListener.IsSupported)
{
// Requires at least a Windows XP with Service Pack 2
throw new NotSupportedException(
"The Http Server cannot run on this operating system.");
} // end if HttpListener is not supported
_httpListener = new HttpListener();
_httpListener.Prefixes.Add("http://localhost:" + port + "/");
_httpListener.Prefixes.Add("http://192.168.1.69:" + port + "/");
_resourceLocator = new Locator();
}
public void Start()
{
if (!_httpListener.IsListening)
{
_httpListener.Start();
_running = true;
_connectionThread = new Thread(new ThreadStart(this.ConnectionThreadStart));
_connectionThread.Start();
}
}
In my C# project I have included ChilkatDotNet2.dll in order to connect to a SFTP server. For the moment, I have tried to implemt a simple connection method like the following:
Chilkat.Ftp2 F = null;
F = (Chilkat.Ftp2)new Chilkat.Ftp2();
F.Hostname = "sftp.domain.com";
F.Username = "username";
F.Password = "password";
F.Port = 22;
F.AuthTls = true;
if (!(F.Connect())) {
MainFrm.set_AlertMessage(F.ConnectFailReason);
}
and it always fails connecting and I always get the error 200 which means
Connected, but failed to receive greeting from FTP server.
according to Chilkat documentation.
What am I missing? I have tried to connect to a simple ftp server test (without SSL/TLS) and it connects correctly so I think I am missing something.
The credentials I was given are correct as I tried to connect to the SFTP server with Filezilla.
Thank you.
UPDATE
Chilkat.SFtp v_SFTP = null;
v_SFTP = (Chilkat.SFtp)new Chilkat.SFtp();
if (v_SFTP.Connect("sftp.domain.com", 22) {
if (v_SFTP.AuthenticatePw("username", "password")) {
IDVariant v_SUCCESS = null;
bool v_SUCCESS = v_SFTP.InitializeSftp();
}
}
else {
MainFrm.set_AlertMessage(v_SFTP.LastErrorText);
}
F = (Chilkat.Ftp2)new Chilkat.Ftp2();
Chilkat.Ftp2 is an FTP client. It supports the FTP protocol. SFTP is a completely different protocol which just happens to have a similar name.
If you want to connect to an SFTP server using Chilkat, look at Chilkat.SFtp.
I am trying to build a tool on the server which will open the browser on the client. note that the server and the client are in the same domain in our company.
everything works good, but when i try this to open the browswer on the client machine
string theProcessToRun = popupURL + "?CallerID=" + callerID;
string remoteIdentifier = string.Format(#"\\{0}\root\cimv2:Win32_Process", ipAddress);
ManagementClass mc = new ManagementClass(remoteIdentifier);
mc.InvokeMethod("Create", new[] { theProcessToRun });
as you see, i am using #"\\{0}\root\cimv2:Win32_Process", ipAddress, which ** I don't** know if it is the correct way because i am using windows client so i don't know what is this root has to do here
i am having this exception
Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))
on this line
mc.InvokeMethod("Create", new[] { theProcessToRun });
may you help please
I have a small program (C# Windows Form) that will access the printer services and print a barcode label. This program only works when I install it and run it on the clients server. When I try running it from my PC I get an error.
Here is what I have:
public static bool PrintBin(string trans, string qty)
{
string m1 = string.Format("{0}|{1}|{2}|{3}", trans, qty, PrinterName, ConnString);
string m2 = string.Empty;
bool status = true;
try
{
TcpClient tcpSocket = new TcpClient(PrinterServer, 65000);
NetworkStream streamToServer = tcpSocket.GetStream();
// create a streamWriter and use it to write a string to the server
System.IO.StreamWriter writer =
new System.IO.StreamWriter(streamToServer);
writer.WriteLine(m1);
writer.Flush();
// Read response
System.IO.StreamReader reader =
new System.IO.StreamReader(streamToServer);
m2 = reader.ReadLine();
streamToServer.Close();
}
catch
{
m2 = "Can not find scanner printing service.";
status = false;
}
It gets stuck and errors out on the TcpClient line.
Thank you
* UPDATE *
Here is the error message i get:
"A connection attemp failed because the connected party did not properly respond after a period of time, or established connection failed because a connected host has failed to respond 172.18.10.22:65000 at System.Net.Sockets.TcpClient (String hostname, Int32 port)"
Thanks