A small question.. I have used sharpSSH to connect to SFTP server with "StrictHostKeyChecking" set to "yes". But, everytime I try to connect to a new SFTP server for the first time it asks me whether I want to trust a particular server finger print or not. Is there a way I can skip this step and accept it programmitically since I want minimal user interaction with this code? Also, I do not want to set "strictHostKeyChecking" to "no". Is there any workaround. I was thinking of adding the fingerprint in the Known_hosts file manually but I think I cannot do so as the finger prints are stored in encrypted format in known_hosts file. Can you please suggest some work-around for this?
My code goes as follows:
Tamir.SharpSsh.jsch.Session mySession = myJsch.getSession(_UserName,_ftpURL, _Port);
mySession.setPassword(_Password);
Hashtable config = new Hashtable();
config.Add("StrictHostKeyChecking", "yes");
mySession.setConfig(config);
byte[] fingerPrint_bytes = new byte[fingerPrint.Length * sizeof(char)];
System.Buffer.BlockCopy(fingerPrint.ToCharArray(), 0, fingerPrint_bytes, 0, fingerPrint_bytes.Length);
UserInfo ui=new MyUserInfo();
mySession.setUserInfo(ui);
myJsch.getHostKeyRepository().add(_ftpURL, fingerPrint_bytes, ui);
mySession.connect();
Related
I'm creating a socket application which is able to receive strings from a websocket within a web page. I have been able to successfully connect the Websocket to my C# program but when ever the webpage sends a string to the program it seems to be encrypted or hashed in some way.
For example if the webpage sends "Test" the program would then output "???9uu?\". I'm obviously missing a step here and I'm not sure what I should searching for to resolve this issue. I'm guessing the string has to be decrypted or put trough a specific function with the TCP key in order to get the actual string?
The code below is the section responsible for receiving the strings from the HTML, (Both "Data" and "MyWriter" output the same string):
while (true)
{
CollectedBytes = new byte[128];
stream.Read(CollectedBytes, 0, CollectedBytes.Length);
string Data = Encoding.ASCII.GetString(CollectedBytes, 0, CollectedBytes.Length);
Output.Speak("Message: " + Data);
StringWriter MyWriter = new StringWriter();
HttpUtility.HtmlDecode(Data, MyWriter);
Output.Speak("Message: " + MyWriter.ToString());
// The word "Test" should output here
// But instead "???9uu?\" is.
}
I'm assuming that I'm missing a simple step but I've looked everywhere and can't seem to find anything to help me!! If anyone can give me guidance on what I should do that would be great :)
Thanks in advance.
Are you trying to decode data manually without using any WebSocket library? If so, you must know that the payload part of WebSocket frames from clients is masked. See RFC 6455, 5.3. Client-to-Server Masking.
I'm using this sample exactly (except I changed tchar to char everywhere):
https://msdn.microsoft.com/en-us/library/windows/desktop/aa365601%28v=vs.85%29.aspx
I'm using the following c# code to send a medium sized (=400kb) file to the c++ server:
var client = new NamedPipeClientStream(#"SamplePipe");
client.Connect();
StreamWriter writer = new StreamWriter(client);
string bla = File.ReadAllText(#"D:\Request.txt");
writer.Write(bla);
This is the file I'm sending:
http://www.gutenberg.org/cache/epub/45745/pg45745.txt
It never sends data beyond this passage: "of us stand as high as a tall man, and a measure four feet from bill to tail. Thur" (around 170kb of 400kb)
(I dont understand where the 'ur' comes from, the next word would be 'There')
The last thing that happens is a WAIT_IO_COMPLETION as far as I can tell and then it just stays forever on the writer.Write(bla) line. But this may be circumstancial.
What's going on? I have also tried some of the other samples and they all seem to have problems with larger files.
Ok I think I figured it out:
Your C++ sample has the PipeTransmissionMode set to 'Message' (PIPE_TYPE_MESSAGE and PIPE_READMODE_MESSAGE) while your C# NamedPipeClientStream is using the PipeTransmissionMode 'Byte' by default.
So change PIPE_TYPE_MESSAGE to PIPE_TYPE_BYTE and PIPE_READMODE_MESSAGE to PIPE_READMODE_BYTE.
Check this CreateNamedPipe function page on MSDN for more information to make sure your PipeServer is properly setup.
I try to implement a custom Wopi host in C# that can handle the Cobalt Protocol using the CobaltCore assembly.
But I didn't found any documentation for CobaltCore.dll
Object browser is a little helpful..
Please provide some details if someone had similar issue.
How I should use Cobalt to decipher the messages?
For word editing implementation go here:
Can I just use Office Web Apps Server
// fsshttpb payload, basically decode from base64 encoded
byte[] test1 = System.Convert.FromBase64String("DAALAJzPKfM5lAabBgIAAO4CAABaBBYADW1zd29yZAd3YWN6AggA1RyhD3cBFgIGAAMFABoEIAAL3Do4buY4RJXm4575cgEiigICAAALAawCAFUDAQ==");
// create an atom object from the fsshttp input
AtomFromByteArray atomRequest = new AtomFromByteArray(test1);
RequestBatch requestBatch = new RequestBatch();
requestBatch.DeserializeInputFromProtocol(atomRequest);
// now you can inspect requestBatch to view the decoded objects
edit:
Here is a sample implementation using CobaltCore. Pretty much a combination of my answers about WOPI/FSSHTTP on this website in one project.
https://github.com/thebitllc/WopiBasicEditor
Also implementing the Cobalt approach to edits and like Julia it stops at a "cant edit screen" even after lockingstore callbacks including co-author etc.
What I have found however is the log system for OWA reveals quite considerable detail about what the OWA server is attempting to do.
C:\ProgramData\Microsoft\OfficeWebApps\Data\Logs\ULS
I can see from these logs it complains about a missing access token, by providing
&access_token=1&access_token_ttl=0
to the end of the wopi url this error goes away.
I also tested many of the file info fields and was able to see how the OWA server caches information. If we keep changing the cfi.Version
FileInfo info = new FileInfo("C:\\WOPI OWA WORD EDITOR\\OWA_Source_Documents\\" + fi.Name);
cfi.Version = info.LastWriteTimeUtc.ToString("s");
we get a fresh cached item each time we change the files contents via normal word.
These also affect the View mode for Word and I suspect will lock us out of the word edit mode but since I don't have that working I cant tell yet.
cfi.SupportsCoauth = true; // all three (3) needed to see the edit in browser menu in view mode .
cfi.SupportsCobalt = true; // all three (3) needed to see the edit in browser menu in view mode .
cfi.SupportsFolders = true; // all three (3) needed to see the edit in browser menu in view mode .
cfi.SupportsLocks = true;
cfi.SupportsScenarioLinks = false;
cfi.SupportsSecureStore = true;
cfi.SupportsUpdate = true;
This one locks out the word edit function and unless you update the version of the file it will stay locked even if you change it back to false.
cfi.WebEditingDisabled = false;
Roger Hogg
thanks to thebitllc for the correct approach to getting back the file.
System.IO.FileStream _FileStream = new System.IO.FileStream("C:\\WOPI OWA WORD EDITOR\\OWA_Updated_Documents\\output.docx", System.IO.FileMode.Create, System.IO.FileAccess.Write);
GenericFdaStream myCobaltStream = new GenericFda(cobaltFile.CobaltEndpoint, null).GetContentStream();
myCobaltStream.CopyTo(_FileStream);
_FileStream.Close();
I've been trying for a while now to set up an ftp connection with our mainframe via c#. So far I've been able to connect and upload a file to the mainframe, which is great, but the file has the wrong record length and is set up as variable, instead of fixed record length. After quite a bit a research I have come to the same conclusion as most people, that the ftpwebresponse function will not allow "qoute SITE" commands to be issued to the mainframe. If i'm wrong please don't hesitate to correct me, heres the code i'm using:
private void ftpButton_Click(object sender, EventArgs e)
{
string ftphost = "ftpSite";
string user = "myUser";
string pwd= "myPass";
string ftpfileName = "test.file2";
string inputfilePath="d:\\documents and settings\\gheff\\My Documents\\Visual Studio 2005\\Projects\\biWeeklyJob\\BiWeekly\\bin\\Debug\\file2.TXT";
string ftpfullpath = String.Format("ftp://{0}//'{1}'", ftphost,ftpfileName);
try
{
FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create(ftpfullpath);
ftp.Credentials = new NetworkCredential(user,pwd);
ftp.KeepAlive = true;
ftp.UseBinary = false; //Use ascii.
ftp.Method = WebRequestMethods.Ftp.UploadFile;
FileStream fs = File.OpenRead(inputfilePath);
byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length);
fs.Close();
Stream ftpstream = ftp.GetRequestStream();
ftpstream.Write(buffer, 0, buffer.Length);
ftpstream.Close();
}
catch (WebException ex)
{
String status = ((FtpWebResponse)ex.Response).StatusDescription;
throw new Exception(status);
}
}
So undeterred, I tried to think of another approach, I have successfully used the command line to upload a file, change the file properties and also changed the filetype so I can send JCL jobs to the mainframe. I am now tyring to implement this into my existing c# application, but proving harder than I thought.
I am by no means an expert in c#, but can use answers to then go and do more research to gain a better understanding.
I have seen this piece of code C# cmd excute commands, but looking around it seems that only one command can be issued from this.
So my question is, I know it took a while to get there, is it possible to run the follow commands on the command prompt without the use of a batch file as i'm looking some feedback as the process runs?
open "cmd.exe"
type "ftp"
type "FTPserver"
Type "USERNAME"
type "PASSWORD"
Then once connection has been established
then run pre-defined commands i.e upload files, upload and run JCL jobs.
I think what i'm looking for is somthing that will write text to cmd.exe but keep the session?
I hope this makes sense.
Any help would be greatly appreciated.
Thanks
So here is code you are asking for nevertheless I doubt that it solve your problem.
var cmd= new Process();
cmd.StartInfo.FileName = "cmd.exe";
cmd.StartInfo.UseShellExecute = false;
cmd.StartInfo.RedirectStandardInput = true;
cmd.Start();
using (var stdin = cmd.StandardInput)
{
stdin.WriteLine("ftp");
stdin.WriteLine("FTPserver");
stdin.WriteLine("USERNAME");
stdin.WriteLine("PASSWORD");
}
cmd.WaitForExit();
cmd.Close();
Try this cmd:
FTP -s:ftpcommandfilename url
sense the follow mode can run one command only, you can write the ftp cmd in a file and use this cmd to execute them.
WriteLine("FTP -s:ftpcommandfilename url");
By the way, I am also finding the multi-ftp-cmd-line-in-code mode, if you have got it, sharing?
I used this CodeProject project a few years ago for a simple application that needed to send some files. The thing that may help you is that it uses a socket to talk to the other server. You can send raw FTP commands which should sidestep the problems you're having with the FtpWebRequest object, without having to "shell out" and use the ftp command line program.
Another approach you could try is to submit a jcl job to allocate the dataset with the record length, block size, et al, that your process needs.
Once that job complets, "put" your data into the dataset you just allocated correctly. The z/OS ftp server should pick up the attributes from your existing dataset.
Have you considered using a commercial FTP client that supports a COM interface and is able to send raw protocol commands? This might not be economical if you are writing software to redistribute but if you are only trying to automate a recurring task for an internal IT project it could definately be worth it in terms of your time as a developer. Robo-FTP might be a good choice for connecting to a mainframe because it has built in ASCII to EBCDIC translation.
i am working on a pc 192.168.2.200
i have made a simple C# Windows application on 192.168.2.200
I want to Create a Text File on D: of a Network PC whose IP is 192.168.2.201 and UserName is abc and Password is 123 by C# code in Windows Application
How i Will Create file on Network PC
any one can Help me
System.IO.File.Create(#"D:\myfile.txt");
OR
System.IO.File.WriteAllText(#"D:\myfile.txt","Hello this is my File");
Windows application or not, you'd use the same technique. If you don't care what the format is, but want to use it as an input file that you read/write from, then you might want to look into XmlSerializer, along with a custom class that you write (with username/password/IP properties).
Here is a simple tutorial on XML serialization:
http://www.switchonthecode.com/tutorials/csharp-tutorial-xml-serialization
If you want a custom format, and only want to write to it (such as a log file), then you can do this:
var file = System.IO.File.Create("file.txt");
var writer = new System.IO.StreamWriter(file);
writer.WriteLine("fjkldsaf");
Or use the overload for StreamWriter that takes a filename:
var writer = new System.IO.StreamWriter("otherfile.txt");
writer.WriteLine("some text");
Keep in mind that writing a password in clear text onto your hard drive is not very secure (same with clear-text over the network, though I know you're not asking about that).
Make sure you either call Dispose or Close on your file streams when you're done with them. You can stick them in a using block to do this automatically (even if an exception is accidentally thrown in your file writing code).
using(var writer = new System.IO.StreamWriter("otherfile.txt"))
{
writer.WriteLine("some text");
}