Last active
March 19, 2019 14:26
-
-
Save giuliohome/7ccc6f127ad4bca4eb71c1e7fecec6d5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
open Renci.SshNet | |
open System.IO | |
open System.Configuration | |
open System | |
let config = ConfigurationManager.AppSettings | |
let timestamp = "sftp_log_" + DateTime.UtcNow.Date.ToString("yyyy_MM_dd") + ".txt" | |
let logPath = Path.Combine(config.["SharedFolder"],timestamp) | |
let sw = new StreamWriter(logPath,true) | |
let printerAgent = MailboxProcessor.Start(fun inbox-> | |
// the message processing function | |
let rec messageLoop() = async{ | |
// read a message | |
let! msg = inbox.Receive() | |
// process a message | |
sw.WriteLine("{0}: {1}", DateTime.UtcNow.ToShortTimeString(), msg) | |
printfn "%s" msg | |
// loop to top | |
return! messageLoop() | |
} | |
// start the loop | |
messageLoop() | |
) | |
type FtpNames = {Remote:string; Local:string } | |
type Node = | |
| FtpFile of FtpNames | |
| FtpDir of FtpNames | |
/// FSharp Async wrapper for SSH.NET SFTP | |
type SftpClient with | |
member x.ListDirectoryAsync path = | |
Async.FromBeginEnd((fun(iar,state) -> | |
x.BeginListDirectory(path, iar, state)), x.EndListDirectory) | |
member x.DownloadFileAsync path output = | |
Async.FromBeginEnd((fun(iar,state) -> | |
x.BeginDownloadFile(path, output, iar, state)), x.EndDownloadFile) | |
member x.UploadFileAsync input path = | |
Async.FromBeginEnd((fun(iar,state) -> | |
x.BeginUploadFile(input, path, iar, state)), x.EndUploadFile) | |
|> Async.Catch | |
member x.SynchronizeDirectoriesAsync sourcePath destinationPath searchPattern = | |
Async.FromBeginEnd((fun(iar,state) -> | |
x.BeginSynchronizeDirectories( | |
sourcePath, destinationPath, searchPattern, iar, state)), | |
x.EndSynchronizeDirectories) | |
|> Async.Catch | |
let downloadFile (path:string) (client:SftpClient) (remote:string) = | |
async { | |
if not <| File.Exists path then | |
use fileStream = File.OpenWrite(path) | |
sprintf "Downloading %s..." path |> printerAgent.Post | |
do! client.DownloadFileAsync remote fileStream | |
sprintf "Downloaded %s (%i bytes)" path fileStream.Length |> printerAgent.Post | |
fileStream.Close() | |
else printfn "File %s alreadfy exists" path | |
} | |
let rec downloadDir (local:string) (client:SftpClient) (workDir:string) = | |
async { | |
let! listDirectory = client.ListDirectoryAsync workDir | |
match listDirectory with | |
| empty when empty |> Seq.length = 0 -> sprintf "empty directory: %s" workDir |> printerAgent.Post | |
| _ -> | |
do! | |
seq { | |
for file in listDirectory do | |
let path = local + file.FullName.Replace("/","\\") | |
if file.IsDirectory then | |
if not <| Directory.Exists(path) then | |
Directory.CreateDirectory(path) |> ignore | |
sprintf "Dir %s" path |> printerAgent.Post | |
yield downloadDir local client file.FullName | |
else | |
yield downloadFile path client file.FullName | |
} | |
|> Async.Parallel |> Async.Ignore | |
} | |
let sftpExample local host port username (password:string) = | |
async { | |
use client = new SftpClient(host, port, username, password) | |
client.Connect() | |
sprintf "Connected to %s\nroot dir list" host |> printerAgent.Post | |
do! downloadDir local client "" | |
sprintf "Done, disconnecting now" |> printerAgent.Post | |
client.Disconnect() | |
} |> Async.RunSynchronously | |
[<EntryPoint>] | |
let main argv = | |
try | |
sftpExample config.["SharedFolder"] config.["SFTPFolder"] 22 "usr" "pswd" |> ignore | |
with | |
| ex -> | |
ex.Message |> printerAgent.Post | |
printfn "%s" ex.Message | |
sw.Close() | |
sw.Dispose() | |
0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment