/// <summary> /// Opens a <see cref="ScpStream"/> to be used for reading a file from a remote target /// </summary> /// <param name="host">The name of the remote target</param> /// <param name="user">The user to use on the remote target</param> /// <param name="password">The user's password</param> /// <param name="filePath">The full path, including the filename, on the remote target to read from</param> /// <returns>A <see cref="ScpStream"/> that can be used to read a file</returns> public static ScpStream OpenReadStream(Scp scp, string filePath) { Channel channel = null; Stream server = null; String filename = null; scp.m_cancelled = false; int filesize = 0; string cmd = null; scp.Connect(); scp.SCP_ConnectFrom(out channel, out server, filePath, false); MemoryStream stream = new MemoryStream(); try { //scp.SCP_ConnectFrom(out channel, out server, filePath, false); byte[] buf = new byte[1024]; // send '\0' scp.SCP_SendAck(server); int c = scp.SCP_CheckAck(server); //parse scp commands while ((c == 'D') || (c == 'C') || (c == 'E')) { if (scp.m_cancelled) { break; } cmd = "" + (char)c; if (c == 'E') { c = scp.SCP_CheckAck(server); //dir = Path.GetDirectoryName(dir); if (scp.Verbos) { Console.WriteLine("E"); } //send '\0' scp.SCP_SendAck(server); c = (char)scp.SCP_CheckAck(server); continue; } // read '0644 ' or '0755 ' server.Read(buf, 0, 5); for (int i = 0; i < 5; i++) { cmd += (char)buf[i]; } //reading file size filesize = 0; while (true) { server.Read(buf, 0, 1); if (buf[0] == ' ') { break; } filesize = filesize * 10 + (buf[0] - '0'); } //reading file name for (int i = 0; ; i++) { server.Read(buf, i, 1); if (buf[i] == (byte)0x0a) { filename = Util.getString(buf, 0, i); break; } } cmd += " " + filesize + " " + filename; // send '\0' scp.SCP_SendAck(server); //Receive file if (c == 'C') { if (scp.Verbos) { Console.WriteLine("Sending file modes: " + cmd); } scp.SCP_ReceiveFile(server, stream, filename, null, filesize); stream.Seek(0, 0); break; } c = scp.SCP_CheckAck(server); } } catch (Exception e) { if (scp.Verbos) { Console.WriteLine("Error: " + e.Message); } try { channel.disconnect(); } catch { } throw e; } return(new ScpStream(channel, stream, false)); }