I've seen other threads about this error, but I am having this error randomly. Out of 30 connects, 12 got this error. Trying to understand why this is, and what possible solutions are.
using (SftpClient client = new SftpClient(sftpHost, sftpPort, sftpUser, sftpPassword))
{
client.Connect();
}
throws this exception:
Renci.SshNet.Common.SshConnectionException: Server response does not contain SSH protocol identification
This might sound trivial, but I have tried the Renci.SshNet.Async package and it worked with out any issue. The following could be a reason/s for the problem with solution or alternative solution.
Renci.SshNet.Async might have bug or issues that appears in specific environment or in specific combination. Some mentioned same problems here, no solution https://github.com/sshnet/SSH.NET/issues/377
Some has experienced same problem and mentioned a solutions, one is to retry n number of times Renci.SshNet : "server response does not contain ssh protocol identification"
The problem is not with your client but more or less with your Host. It can be many things or issues.
Finally I would try using WinSCP which is also well recognized library for .net (nuget). I have tried it my self as well and have never experienced any issues so far. I suggest you to get it and try it as alternative to your current solution, and see if it helps. If WinSCP works then point 1 is valid, if you experience same issue with WinSCP then point 3 is valid. This way we can make conclusions and narrow down to the problem.
Here examples provided by WinSCP https://winscp.net/eng/docs/library_examples.
My Test example is:
This is just playground example to see if things are running, when used in production, please modify it. Further more thanks to #MartinPrikry adding this note: If you have set SshHostKeyFingerprint correctly, then do not set GiveUpSecurityAndAcceptAnySshHostKey.
public static int WinSCPListDirectory()
{
try
{
var sessionOptions = new SessionOptions
{
Protocol = Protocol.Sftp,
HostName = "xxx.xxx.xxx.xxx",
UserName = "username",
Password = "password",
SshHostKeyFingerprint = "ssh-rsa 2048 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx",
GiveUpSecurityAndAcceptAnySshHostKey = true
};
using (var session = new Session())
{
session.Open(sessionOptions);
var directory = session.ListDirectory("/var/www");
foreach (RemoteFileInfo fileInfo in directory.Files)
{
Console.WriteLine(
"{0} with size {1}, permissions {2} and last modification at {3}",
fileInfo.Name, fileInfo.Length, fileInfo.FilePermissions,
fileInfo.LastWriteTime);
}
}
return 0;
}
catch (Exception e)
{
Console.WriteLine("Error: {0}", e);
return 1;
}
}
This example should return a list of directories
Related links:
https://winscp.net/eng/docs/library_session_listdirectory
OpenSource .NET library for connecting to SFTP server?
No one knows the exact cause for this behavior to be honest.
As a possible fix, Some people suggest creating a counter loop for retrying to connect, suggesting it solved their problem:
int attempts = 0;
do
{
try
{
client.Connect();
}
catch (Renci.SshNet.Common.SshConnectionException e)
{
attempts++;
}
} while (attempts < _connectiontRetryAttempts && !client.IsConnected);
Another suggestion is to add the IP address into the hosts.allow file on the server, so you could check in that direction as well.
As I said, there is no possible explanation for the behavior altogether.
Related
I am trying to open a web page using the default browser when someone hits an API endpoint.
I have this working on my local test machine:
[HttpGet("Http/{classId}")]
public void OpenWebLink(Guid classId)
{
string target = "http://astrolab.meeting.trl.edu/class/details.aspx?classId=" + classId;
System.Diagnostics.Process.Start("C:\\Program Files\\Mozilla Firefox\\firefox.exe", target);
}
But when I publish to a server that has IIS, it can't find firefox.exe
The problem is, I had to put the full path to firefox just to get it to work on my machine. If I didn't include the path like that I'd get this error:
System.Diagnostics.Process.Start Win32Exception: 'The system cannot find the file specified.'
I also tried this:
[HttpGet("Http")]
public void OpenWebLink(Guid classId)
{
try
{
var ps = new ProcessStartInfo("http://astrolab.meeting.trl.edu/class/details.aspx?classId=" + classId;)
{
Verb = "open"
};
Process.Start(ps);
}
catch (Win32Exception w32Ex)
{
throw w32Ex;
}
}
But it still fails when I hit the endpoint on the IIS server with this:
System.ComponentModel.Win32Exception (2): The system cannot find the file specified.
Is there a way to set it up so that it will find the default browser on any machine?
Thanks!
It's as if dotnet developers are unaware of other operating systems besides Windows...
#Blindy's answer only works on Windows, but nowhere did they indicate this. And even if #SkyeBoniwell's question mentions IIS which implies Windows, neither the title of the question, nor the body, nor the tags explicitly mention Windows. As such, keeping in mind stackoverflow answers are meant to be used by everyone and not only the OP of a question, a correct answer to this thread should theoretically be os-agnostic. In practice, it should take into account as many operating systems as possible, i.e. at the very least the main three ones.
Here is a solution that works for 99% of end-user systems:
public static void OpenBrowser(string url)
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
Process.Start(new ProcessStartInfo(url) { UseShellExecute = true });
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
Process.Start("xdg-open", url);
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
Process.Start("open", url);
}
else
{
// throw
}
}
taken from here
Either use ShellExecute to launch your url (the more correct way), or pipe it through explorer (the lazier, less portable way).
Process.Start(new()
{
UseShellExecute = true,
FileName = "http://google.ca",
});
Process.Start("explorer.exe", "http://google.ca");
I'm getting this error message, MQRC_HOST_NOT_AVAILABLE, when connecting to IBMMQ.
I'm relatively new to working with IBMMQ, but have been handed this project and need to get our .NET WCF code talking to MQ.
Our code currently looks like this:
Hashtable queueManagerProps = new Hashtable();
queueManagerProps.Add(MQC.HOST_NAME_PROPERTY, mqhost);
queueManagerProps.Add(MQC.CHANNEL_PROPERTY, mqchannel);
queueManagerProps.Add(MQC.PORT_PROPERTY, ConfigurationManager.AppSettings["MQPort"].ToString());
queueManagerProps.Add(MQC.TRANSPORT_PROPERTY, MQC.TRANSPORT_MQSERIES_MANAGED);
try
{
mqQMgr = new MQQueueManager(qmname, queueManagerProps);
}
catch (Exception ex)
{
throw new Exception(String.Format("Failed to connect to MQ Queue Manager {0}, channel {1} on host {2} on port {3}. Check inner exception for root cause.", qmname, mqchannel, mqhost, ConfigurationManager.AppSettings["MQPort"].ToString()), ex);
}
I have confirmed that (1) all the properties contain values, and (2) the values match the setup of our MQ server.
I also looked in the AMQERR01.LOG log file and there have been no entries since we set up the queue earlier this month.
Why is my new MQQueueManager() called throwing this exception? Is there any chance this could be a permissions issue, or is it definitely network / connectivity / configuration related?
Found the problem. I had the host name misspelt.
The moral of the story is don't overlook the fundamentals, even when working with a technology you don't understand.
Admins: please feel free to delete this question if you don't find it useful.
I've been trying to navigate through a SAS Project's output datasets using the SAS.EG.Scripting library for C#, but I keep getting an empty collection of datasets, even though the dataset is correctly generated on SAS Server.
I tried to follow the steps explained on this article, which is the only resource about SAS automation I've found researching the web: Not Just for Scheduling: Doing More with SASĀ® Enterprise GuideĀ® Automation.
The code I've wrote so far is below:
public static void RunSASProject()
{
SAS.EG.Scripting.Application EGApp = new SAS.EG.Scripting.Application();
EGApp.SetActiveProfile("almarci");
SAS.EG.Scripting.Project EGProject = (SAS.EG.Scripting.Project)EGApp.New();
SAS.EG.Scripting.Code oCode = (SAS.EG.Scripting.Code)EGProject.CodeCollection.Add();
try
{
oCode.Server = "SASCORP";
oCode.UseApplicationOptions = false;
oCode.GenSasReport = false;
oCode.Name = "Testing";
oCode.Text = "LIBNAME SRC '/home/cau004/aj/dccvoj/sotcpc/giad/workgroup/Apoio'; DATA SRC.CARS; SET SASHELP.CARS; OUTPUT; RUN;";
oCode.Run();
oCode.Log.SaveAs(#"C:\Users\almarci\Desktop\SAS\LogSAS" + DateTime.Now.ToString("ddmmyyyy HHmmss") + ".log");
oCode.TaskCode.SaveAs(#"C:\Users\almarci\Desktop\SAS\TaskSAS" + DateTime.Now.ToString("ddmmyyyy HHmmss") + ".txt");
SAS.EG.Scripting.OutputDatasets outputDatasets = (SAS.EG.Scripting.OutputDatasets)oCode.OutputDatasets;
foreach (SAS.EG.Scripting.OutputData outputData in outputDatasets)
{
Console.Write(outputData.Name.ToString());
}
}
catch (Exception ex)
{
Console.WriteLine("\n" + ex.Message.ToString());
}
finally
{
EGApp.Quit();
}
}
The basic steps it performs are the following:
1) Instantiate an Application object;
2) Define the profile that will be used for connecting to the server;
3) Create a new project;
4) Add a Code object to the recently created project;
5) Set up the Code object properties (Name, Text - which is the SAS command that will be executed);
6) Run the Code object;
7) Save the Code log and command text on .txt files;
8) Iterate through Code's OutputDatasets collection. This is where I get the strange behavior, since even though the code run successfully, the collection items's count is set to zero.
Anyone has already faced this kind of problem? Have I forgot to write some key word on SAS command or to set some property of the objects involved?
Appreciate any help.
I wrote the paper that you referenced.
There were some automation-related fixes in SAS Enterprise Guide in 4.3, post-release. You didn't say which version you have here, but this issue is best tracked with SAS Technical Support who can advise on hotfixes.
Also, for a higher concentration of SAS expertise, consider posting such questions to the SAS Enterprise Guide discussion forum.
This is my code to connect and send a file to a remote SFTP server.
public static void SendDocument(string fileName, string host, string remoteFile, string user, string password)
{
Scp scp = new Scp();
scp.OnConnecting += new FileTansferEvent(scp_OnConnecting);
scp.OnStart += new FileTansferEvent(scp_OnProgress);
scp.OnEnd += new FileTansferEvent(scp_OnEnd);
scp.OnProgress += new FileTansferEvent(scp_OnProgress);
try
{
scp.To(fileName, host, remoteFile, user, password);
}
catch (Exception e)
{
throw e;
}
}
I can successfully connect, send and receive files using CoreFTP. Thus, the issue is not with the server. When I run the above code, the process seems to stop at the scp.To method. It just hangs indefinitely.
Anyone know what might my problem be? Maybe it has something to do with adding the key to the a SSH Cache? If so, how would I go about this?
EDIT: I inspected the packets using wireshark and discovered that my computer is not executing the Diffie-Hellman Key Exchange Init. This must be the issue.
EDIT: I ended up using the following code. Note, the StrictHostKeyChecking was turned off to make things easier.
JSch jsch = new JSch();
jsch.setKnownHosts(host);
Session session = jsch.getSession(user, host, 22);
session.setPassword(password);
System.Collections.Hashtable hashConfig = new System.Collections.Hashtable();
hashConfig.Add("StrictHostKeyChecking", "no");
session.setConfig(hashConfig);
try
{
session.connect();
Channel channel = session.openChannel("sftp");
channel.connect();
ChannelSftp c = (ChannelSftp)channel;
c.put(fileName, remoteFile);
c.exit();
}
catch (Exception e)
{
throw e;
}
Thanks.
I use Tamir.SharpSSH - latest version 1.1.1.13
This has a class SFTP. You can use this class directly to do SFTP instead of using JSch, Session class.
Quick Sample here:
127.0.0.1 - Server IP
/SFTPFolder1/SFTPFolder2 - Server Location Where I want my files to go
Sftp sftpClient = new Sftp("127.0.0.1", "myuserName", "MyPassword");
sftpClient.Connect();
sftpClient.Put(#"C:\Local\LocalFile.txt", "/SFTPFolder1/SFTPFolder2");
Let me know if you have any issues.
Without looking at your log files it is hard to tell what the issue is.
However keep in mind that SCP is not SFTP - they are completely different protocols that run over SSH. It is possible that your SFTP does not actually support SCP - not all SFTP servers do. CoreFTP may be using SFTP.
Our commercial package, edtFTPnet/PRO, might also be worth trying, if only as an alternative to try to get a different client working against your server.
I am working on an application which reads eventlogs(Application) from remote machines. I am making use of EventLog class in .net and then iterating on the Log entries but this is very slow. In some cases, some machines have 40000+ log entries and it takes hours to iterate through the entries.
what is the best way to accomplish this task? Are there any other classes in .net which are faster or in any other technology?
Man, I feel your pain. We had the exact same issue in our app.
Your solution has a branch depending on what server version you're running on and what server version your "target" machine is running on.
If you're both on Vista or Windows Server 2008, you're in luck. You should look at System.Diagnostics.Eventing.Reader.EventLogQuery and System.Diagnostics.Eventing.Reader.EventLogReader. These are new in .net 3.5.
Basically, you can build a query in XML and ship it over to run on the remote computer. Maybe you're just searching for events of a specific type, or maybe just new events from a specific point in time. The search runs on the remote machine, and then you just get back the matching events. The new classes are much faster than the old .net 2.0 way, but again, they are only supported on Vista or Windows Server 2008.
For our app when the target is NOT on Vista/Win2008, we downloaded the raw .evt file from the remote system, and then parsed the file using its binary format. There are several sources of data about the event log format for .evt files (pre-Vista), including link text and an article I recall on codeproject.com that had some c# code.
Vista and Windows Server 2008 machines use a new .evtx format that is a new format, so you can't use the same binary parsing approach across all versions. But the new EventLogQuery and EventLogReader classes are so fast that you won't have to. It's now perfectly speedy to just use the built-in classes.
Event Log Reader is horribly slow... too slow. WTF Microsoft?
Use LogParser 2.2 - Search for C# and LogParser on the Internet (or you can use the log parser commands from the command line). I don't want to duplicate the work already contributed by others.
I pull the log from the remote system by having the log exported as an EVTX file. I then copy the file from the remote system. This process is really quick - even with a network that spans the planet (I had issues with having the log exported to a network resource). Once you have it local, you can do your searches and processing.
There are multiple reasons for having the EVTX - I won't get into the reasons why we do this.
The following is a working example of the code to save a copy of the log as an EVTX:
(Notes: "device" is the network host name or IP. "LogName" is the name of the log desired: "System", "Security", or "Application". outputPathOnRemoteSystem is the path on the remote computer, such as "c:\temp\%hostname%.%LogName%.%YYYYMMDD_HH.MM%.evtx".)
static public bool DumpLog(string device, string LogName, string outputPathOnRemoteSystem, out string errMessage)
{
bool wasExported = false;
string errorMessage = "";
try
{
System.Diagnostics.Eventing.Reader.EventLogSession els = new System.Diagnostics.Eventing.Reader.EventLogSession(device);
els.ExportLogAndMessages(LogName, PathType.LogName, "*", outputPathOnRemoteSystem);
wasExported = true;
}
catch (UnauthorizedAccessException e)
{
errorMessage = "Unauthorized - Access Denied: " + e.Message;
}
catch (EventLogNotFoundException e)
{
errorMessage = "Event Log Not Found: " + e.Message;
}
catch (EventLogException e)
{
errorMessage = "Export Failed: " + e.Message + ", Log: " + LogName + ", Device: " + device;
}
errMessage = errorMessage;
return wasExported;
}
A good Explanation/Example can be found on MSDN.
EventLogSession session = new EventLogSession(Environment.MachineName);
// [System/Level=2] filters out the errors
// Where "Log" is the log you want to get data from.
EventLogQuery query = new EventLogQuery("Log", PathType.LogName, "*[System/Level=2]");
EventLogReader reader = new EventLogReader(query);
for (EventRecord eventInstance = reader.ReadEvent();
null != eventInstance;
eventInstance = reader.ReadEvent())
{
// Output or save your event data here.
}
When waiting 5-20 minutes with the old code this one does it in less than 10 seconds.
Maybe WMI can help you:
WMI with C#
Have you tried using the remoting features in powershell 2.0? They allow you to execute cmdlets (like ones to read event logs) on remote machines and return the results (as objects, of course) to the calling session.
You could place a Program at those machines that save the log to file and sends it to your webapplication i think that would be alot faster as you can do the looping local but im not sure how to do it so i cant ive you any code :(
I recently did such thing via WCF callback interface however my clients interacted with the server through WCF and adding a WCF Callback was easy in my project, full code with examples is available here
Just had the same issue and want to share my solution. It makes a search through application, system and security eventlogs from 260 seconds (using EventLog) about a 100 times faster (using EventLogQuery).
And this in a way where it is possible to check if the event message contains a pattern or any other check without the requirement of FormatDescription().
My trick is to use the same mechanism as PowerShells Get-WinEvent does and then pass it through the result check.
Here is my code to find all events within last 4 days where the event message contains a filter pattern.
string[] eventLogSources = {"Application", "System", "Security"};
var messagePattern = "*Your Message Search Pattern*";
var timeStamp = DateTime.Now.AddDays(-4);
var matchingEvents = new List<EventRecord>();
foreach (var eventLogSource in eventLogSources)
{
var i = 0;
var query = string.Format("*[System[TimeCreated[#SystemTime >= '{0}']]]",
timeStamp.ToUniversalTime().ToString("o"));
var elq = new EventLogQuery(eventLogSource, PathType.LogName, query);
var elr = new EventLogReader(elq);
EventRecord entryEventRecord;
while ((entryEventRecord = elr.ReadEvent()) != null)
{
if ((entryEventRecord.Properties)
.FirstOrDefault(x => (x.Value.ToString()).Contains(messagePattern)) != null)
{
matchingEvents.Add(entryEventRecord);
i++;
}
}
}
Maybe that the remote computers could do a little bit of computing. So this way your server would only deal with relevant information. It would be a kind of cluster using the remote computer to do some light filtering and the server would the the analysis part.