public static void CopyFileFromRemoteToLocal(string host, string user, string password, string localPath, string remotePath) { using (SftpClient client = new SftpClient(host, user, password)) { client.KeepAliveInterval = TimeSpan.FromSeconds(60); client.ConnectionInfo.Timeout = TimeSpan.FromMinutes(180); client.OperationTimeout = TimeSpan.FromMinutes(180); client.Connect(); bool connected = client.IsConnected; // RunCommand(host, user, password, "sudo chmod 777 -R " + remotePath); var file = File.OpenWrite(localPath); client.DownloadFile(remotePath, file); file.Close(); client.Disconnect(); } }
private void PollHandler() { var exchange = new Exchange(_processor.Route); var ipaddress = _processor.UriInformation.GetUriProperty("host"); var username = _processor.UriInformation.GetUriProperty("username"); var password = _processor.UriInformation.GetUriProperty("password"); var destination = _processor.UriInformation.GetUriProperty("destination"); var port = _processor.UriInformation.GetUriProperty<int>("port"); var interval = _processor.UriInformation.GetUriProperty<int>("interval", 100); var secure = _processor.UriInformation.GetUriProperty<bool>("secure"); do { Thread.Sleep(interval); try { if (secure) { SftpClient sftp = null; sftp = new SftpClient(ipaddress, port, username, password); sftp.Connect(); foreach (var ftpfile in sftp.ListDirectory(".")) { var destinationFile = Path.Combine(destination, ftpfile.Name); using (var fs = new FileStream(destinationFile, FileMode.Create)) { var data = sftp.ReadAllText(ftpfile.FullName); exchange.InMessage.Body = data; if (!string.IsNullOrEmpty(data)) { sftp.DownloadFile(ftpfile.FullName, fs); } } } } else { ReadFtp(exchange); } } catch (Exception exception) { var msg = exception.Message; } } while (true); }
private static void CreateDirectoriesRecursively(string directory, SftpClient sftp) { if (sftp.Exists(directory) == false) { // try creating one above, in case we need it: var directoryAbove = FlatRedBall.IO.FileManager.GetDirectory(directory, FlatRedBall.IO.RelativeType.Relative); CreateDirectoriesRecursively(directoryAbove, sftp); sftp.CreateDirectory(directory); } }
private void sendSFTP(string filePath, string fileName) { SftpClient sftp = new SftpClient("194.2.93.194", "userece", "AdminPOCNUC01"); sftp.Connect(); using (FileStream filestream = File.OpenRead(filePath)) { sftp.UploadFile(filestream, "/"+fileName, null); sftp.Disconnect(); } }
public bool TryGetFileFromFtp(string fileName, DateTime lastUpdated) { if (!DirectoryUtil.VerifyDirectory(fileName)) { return false; } var fileToDownload = fileName; Log.Debug(string.Format("Attempting to download file " + fileToDownload)); try { Log.Debug("Opening FTP Connection to " + Hostname); using (var client = new SftpClient(Hostname, Username, Password)) { client.Connect(); Log.Debug(string.Format("Connection to {0} opened.", Hostname)); var fileUpdated = client.GetLastWriteTime(fileName); Log.Debug(string.Format("File {0} was last modified on {1}.", fileName, DateUtil.ToIsoDate(fileUpdated))); if (fileUpdated <= lastUpdated) { Log.Info(string.Format("Did not download file {0}, it was last modified {1} and we last processed it on {2}.", fileName, DateUtil.ToIsoDate(fileUpdated), DateUtil.ToIsoDate(lastUpdated)), this); return false; } var outputPath = string.Format("{0}\\{1}", UserCsvImportSettings.CsvFolderPath, fileName); Log.Debug(string.Format("Downloading file {0} and saving to path {1}.", fileName, outputPath)); using (var fileStream = new FileStream(outputPath, FileMode.Create, FileAccess.Write)) { client.DownloadFile(fileName, fileStream); Log.Debug("File successfully written to " + fileStream.Name); } Log.Debug("File Download complete."); Log.Debug("Updating timestamp."); File.SetLastWriteTime(outputPath, fileUpdated); Log.Debug("File timestamp set to " + fileUpdated.ToString()); } } catch (Exception e) { Log.Error("File did not download successfully.", this); Log.Error(string.Format("Could not download file {0} from {1} using user {2}.", fileToDownload, Hostname, Username), e, this); return false; } return true; }
public static IEnumerable<SftpFile> GetList(string host, string folder, string userName, string password) { using (var sftp = new SftpClient(host, userName, password)) { sftp.ErrorOccurred += Sftp_ErrorOccurred; sftp.Connect(); var toReturn = sftp.ListDirectory(folder).ToList(); sftp.Disconnect(); return toReturn; } }
/// <summary> /// List all entries of a folder. /// </summary> /// <param name="path">The folder path to list</param> /// <param name="ctk"></param> /// <returns> /// All entries found (files, folders, symlinks) /// </returns> public async Task <IEnumerable <FtpEntry> > ListDirectoryAsync(string path = "./", CancellationToken ctk = default(CancellationToken)) { await Task.Yield(); var ftpPath = path.GetFtpPath(); _logger.Trace("Listing directory: {0} -> {1}", path, ftpPath); using (var cts = new CancellationTokenSource(TimeSpan.FromMinutes(5))) using (var linked = CancellationTokenSource.CreateLinkedTokenSource(ctk, cts.Token)) { var result = new List <FtpEntry>(); using (var sFtpClient = new Renci.SshNet.SftpClient(new ConnectionInfo(Host, Port, Credentials.UserName, new PasswordAuthenticationMethod(Credentials.UserName, Credentials.Password)))) { sFtpClient.Connect(); var rawLs = await sFtpClient.ListDirectoryAsync(path); _logger.Trace("Starting parsing response for path {0}", path); result = _parse(rawLs, linked.Token); } linked.Token.ThrowIfCancellationRequested(); return(result); } }
public GangsSFTPDriver(string host, int port, string username, string password, string mountPoint) : base(mountPoint, "SFTP") { this.host = host; this.port = port; this.sftpClient = new SftpClient(host, port, username, password); }
private async Task DownloadFilesWithSftpAsync(Endpoint endpoint, string decodedPass) { var client = new SftpClient(endpoint.Url, endpoint.Username, decodedPass); client.Connect(); SftpFile[] filesOnServer = (await this.ListRemoteDirectoriesAsync(client, endpoint.RemoteDirectory)).Where( x => !this.excludedListOfFiles.Any(y => x.Name.Equals(y))).ToArray(); var primaryLocalDirectory = endpoint.Destinations.FirstOrDefault(x => x.Type == DestinationType.Primary); var archiveLocalDirectory = endpoint.Destinations.FirstOrDefault(x => x.Type == DestinationType.Archive); var primaryAndArchiveAreNotExisting = primaryLocalDirectory == null || archiveLocalDirectory == null; if (primaryAndArchiveAreNotExisting) { progress.ReportLine("You haven't provided the primary or archive folder"); return; } var filteredLocalFiles = FilterLocalFiles(filesOnServer, GetFilesListInFolder(primaryLocalDirectory.Name)); await this.GetFilesAsync(filteredLocalFiles, endpoint.RemoteDirectory, primaryLocalDirectory.Name, client).ContinueWith(x => client.Disconnect()); this.ArchiveFiles(GetFilesListInFolder(primaryLocalDirectory.Name), primaryLocalDirectory, archiveLocalDirectory); }
private void checkFingerpint(ConnectionInfo con, string knownHostsPath) { _knownHosts = new KnownHostStore(knownHostsPath); using (Renci.SshNet.SftpClient client1 = new Renci.SshNet.SftpClient(con)) { client1.HostKeyReceived += (sender, eventArgs) => { eventArgs.CanTrust = CanTrustHost(client1.ConnectionInfo.Host, eventArgs); }; try { client1.Connect(); this.fingerprint = true; } catch (Exception) { this.error.setError("SF012", "unknown host"); this.channel = null; this.fingerprint = false; } finally { client1.Disconnect(); } } }
public SshFactory(String host, String username, String password) { _connection = new SshClient(host, username, password); _connection.Connect(); _sftp = new SftpClient(_connection.ConnectionInfo); _sftp.Connect(); }
/// <summary> /// List the files inside a given directory /// </summary> /// <param name="client">The Sftp client</param> /// <param name="dirPath">The directory path</param> /// <param name="silentDownload">Download the files without listing everything</param> /// <returns>The file list collection</returns> public static IEnumerable <String> ListFiles(this RenciSftpClient client, string dirPath, SftpFilter filter, Boolean silentDownload = false) { List <String> files = new List <String> (); IEnumerable <String> result; var entries = client.ListDirectory(dirPath); if (silentDownload) { Console.WriteLine("Downloading {0} entries...", entries.Count()); } foreach (var entry in entries.Where(x => !x.IsDirectory)) { if (filter.IsUnixFileValid(entry.FullName)) { files.Add(entry.FullName); } } result = files; foreach (var subDirPath in entries.Where(x => x.IsDirectory && filter.IsUnixDirValid(x))) { Console.WriteLine(subDirPath); result = result.Union(ListFiles(client, subDirPath.FullName, filter, silentDownload)); } return(result); }
public SFTPHelper() { if (client == null) { client = new SftpClient(Host, Port, SFtpUserID, SFtpPassword); } }
public SFTP(FTPAccount account) { this.FTPAccount = account; if (FTPAccount.UserName.Contains("@")) { FTPAccount.UserName = FTPAccount.UserName.Substring(0, FTPAccount.UserName.IndexOf('@')); } if (!string.IsNullOrEmpty(FTPAccount.Password) && (string.IsNullOrEmpty(FTPAccount.Keypath))) { client = new SftpClient(FTPAccount.Host, FTPAccount.Port, FTPAccount.UserName, FTPAccount.Password); } else if (string.IsNullOrEmpty(FTPAccount.Password) && (File.Exists(FTPAccount.Keypath)) && (string.IsNullOrEmpty(FTPAccount.Passphrase))) { client = new SftpClient(FTPAccount.Host, FTPAccount.Port, FTPAccount.UserName, new PrivateKeyFile(FTPAccount.Keypath)); } else if (string.IsNullOrEmpty(FTPAccount.Password) && (File.Exists(FTPAccount.Keypath)) && (!string.IsNullOrEmpty(FTPAccount.Passphrase))) { client = new SftpClient(FTPAccount.Host, FTPAccount.Port, FTPAccount.UserName, new PrivateKeyFile(FTPAccount.Keypath, FTPAccount.Passphrase)); } else { //Need to do something here... DebugHelper.WriteLine("Can't instantiate a SFTP client..."); IsInstantiated = false; return; } IsInstantiated = true; }
public TransferPluginSftp (ConnectionData data) : base (data) { if (data.Password != null) this.sftp = new SftpClient (data.Host, data.Port, data.User, data.Password); else this.sftp = new SftpClient (data.Host, data.Port, data.User, data.PrivateKey); }
public static SftpSession AddToSftpSessionCollection(SftpClient sftpclient, SessionState pssession) { //Set initial variables var obj = new SftpSession(); var sftpSessions = new List<SftpSession>(); var index = 0; // Retrive existing sessions from the globla variable. var sessionvar = pssession.PSVariable.GetValue("Global:SFTPSessions") as List<SftpSession>; // If sessions exist we set the proper index number for them. if (sessionvar != null && sessionvar.Count > 0) { sftpSessions.AddRange(sessionvar); // Get the SessionId of the last item and count + 1 SftpSession lastSession = sftpSessions[sftpSessions.Count - 1]; index = lastSession.SessionId + 1; } // Create the object that will be saved obj.SessionId = index; obj.Host = sftpclient.ConnectionInfo.Host; obj.Session = sftpclient; sftpSessions.Add(obj); // Set the Global Variable for the sessions. pssession.PSVariable.Set((new PSVariable("Global:SFTPSessions", sftpSessions, ScopedItemOptions.AllScope))); return obj; }
public SftpForm(ConnectionInfo connectionInfo) { InitializeComponent(); this.connectionInfo = connectionInfo; this.sftpClient = new SftpClient(this.connectionInfo); this.sftpClient.Connect(); refresh(); }
public override void Dispose() { if (_sftp != null) { _sftp.Dispose(); _sftp = null; } }
public SSHCommunication(string server, string user, string password) { Server = server; User = user; Password = password; sftp = new SftpClient(Server, User, Password); sshClient = new SshClient(Server, User, Password); }
public SftpClient( SftpClientOptions options, ITempFileStreamFactory tempFileStreamFactory, ILoggerFactory loggerFactory) { _tempFileStreamFactory = tempFileStreamFactory ?? throw new ArgumentNullException(nameof(tempFileStreamFactory)); if (loggerFactory == null) { throw new ArgumentNullException(nameof(loggerFactory)); } _logger = loggerFactory.CreateLogger <SftpClient>(); if (options == null) { throw new ArgumentNullException(nameof(options)); } var timeout = TimeSpan.FromSeconds(options.RetryTimeoutSec); var policyBase = Policy .Handle <SocketException>() .Or <SshConnectionException>() .Or <SshException>() .Or <ProxyException>(); _retryAsyncPolicy = policyBase .WaitAndRetryAsync( options.RetryCount, i => timeout, (ex, innerTimeout, retryCount, context) => { _logger.LogWarning( $"Error occured while uploading to CDN. Retry # {retryCount} will started after {innerTimeout.TotalSeconds} sec"); }); _retryPolicy = policyBase .WaitAndRetry( options.RetryCount, i => timeout, (ex, innerTimeout, retryCount, context) => { _logger.LogWarning( $"Error occured while uploading to CDN. Retry # {retryCount} will started after {innerTimeout.TotalSeconds} sec"); }); var authMethods = ComposeAuthMethods(options); var connectionInfo = new ConnectionInfo( host: options.SshServer, port: options.SshPort, username: options.SshLogin, authenticationMethods: authMethods); _sftpClient = new Renci.SshNet.SftpClient(connectionInfo); }
public static void UploadFileWithOpenConnection(string localFileToUpload, string targetFile, SftpClient sftp) { var directory = FlatRedBall.IO.FileManager.GetDirectory(targetFile, FlatRedBall.IO.RelativeType.Relative); CreateDirectoriesRecursively(directory, sftp); using (var file = File.OpenRead(localFileToUpload)) { sftp.UploadFile(file, targetFile, canOverride: true); } }
public static void UploadFile(string localFileToUpload, string host, string targetFile, string userName, string password) { using (var sftp = new SftpClient(host, userName, password)) { sftp.OperationTimeout = new TimeSpan(0, 0, seconds: 40); sftp.Connect(); UploadFileWithOpenConnection(localFileToUpload, targetFile, sftp); sftp.Disconnect(); } }
public void beginCracking() { log("beginning cracking process.."); var connectionInfo = new PasswordConnectionInfo (xml.Config.host, xml.Config.port, "root", xml.Config.Password); using (var sftp = new SftpClient(connectionInfo)) { using (var ssh = new SshClient(connectionInfo)) { PercentStatus("Establishing SSH connection", 5); ssh.Connect(); PercentStatus("Establishing SFTP connection", 10); sftp.Connect(); log("Cracking " + ipaInfo.AppName); PercentStatus("Preparing IPA", 25); String ipalocation = AppHelper.extractIPA(ipaInfo); using (var file = File.OpenRead(ipalocation)) { log("Uploading IPA to device.."); PercentStatus("Uploading IPA", 40); sftp.UploadFile(file, "Upload.ipa"); } log("Cracking! (This might take a while)"); PercentStatus("Cracking", 50); String binaryLocation = ipaInfo.BinaryLocation.Replace("Payload/", ""); String TempDownloadBinary = Path.Combine(AppHelper.GetTemporaryDirectory(), "crackedBinary"); var crack = ssh.RunCommand("Clutch -i 'Upload.ipa' " + binaryLocation + " /tmp/crackedBinary"); log("Clutch -i 'Upload.ipa' " + binaryLocation + " /tmp/crackedBinary"); log("cracking output: " + crack.Result); using (var file = File.OpenWrite(TempDownloadBinary)) { log("Downloading cracked binary.."); PercentStatus("Downloading cracked binary", 80); try { sftp.DownloadFile("/tmp/crackedBinary", file); } catch (SftpPathNotFoundException e) { log("Could not find file, help!!!!!"); return; } } PercentStatus("Repacking IPA", 90); String repack = AppHelper.repack(ipaInfo, TempDownloadBinary); PercentStatus("Done!", 100); log("Cracking completed, file at " + repack); } } }
public SFTP(Uri targetServer,string password) { _sftp = new SftpClient(targetServer.Host,targetServer.Port, targetServer.UserInfo, password); log.InfoFormat("SFTP - Connecting to {0}", targetServer); _sftp.Connect(); log.InfoFormat("SFTP - Changing dir to {0}", targetServer.LocalPath); _sftp.ChangeDirectory(targetServer.LocalPath); }
public static void DeleteRemoteDirectory(string host, string directory, string username, string password) { using (var sftp = new SftpClient(host, username, password)) { sftp.Connect(); sftp.DeleteDirectory(directory); sftp.Disconnect(); } }
public void Connect() { if(Protocol == SSHTransferProtocol.SCP) { ScpClt = new ScpClient(Host, Port, User, Password); ScpClt.Connect(); } if (Protocol == SSHTransferProtocol.SFTP) { SftpClt = new SftpClient(Host, Port, User, Password); SftpClt.Connect(); } }
public static void CopyFileFromLocalToRemote(string host, string user, string password, string localPath, string remotePath) { using (SftpClient client = new SftpClient(host, user, password)) { client.KeepAliveInterval = TimeSpan.FromSeconds(60); client.ConnectionInfo.Timeout = TimeSpan.FromMinutes(180); client.OperationTimeout = TimeSpan.FromMinutes(180); client.Connect(); bool connected = client.IsConnected; // RunCommand(host, user, password, "sudo chmod 777 -R " + remotePath); FileInfo fi = new FileInfo(localPath); client.UploadFile(fi.OpenRead(), remotePath + fi.Name, true); client.Disconnect(); } }
/// <summary> /// authenticates and returns an sftp client connection /// via ssh.NET /// </summary> /// <returns> /// the sftp client /// </returns> private Renci.SshNet.SftpClient GetSftpClient() { if (_client == null || !_client.IsConnected) { var connectionInfo = new ConnectionInfo( _host, _username, new PasswordAuthenticationMethod(_username, _password)); _client = new Renci.SshNet.SftpClient(connectionInfo); _client.Connect(); } return(_client); }
public async Task UploadFileAsync(string path, byte[] content, CancellationToken ctk = default) { using (var cts = new CancellationTokenSource(TimeSpan.FromMinutes(5))) using (var linked = CancellationTokenSource.CreateLinkedTokenSource(ctk, cts.Token)) { using (var sFtpClient = new Renci.SshNet.SftpClient(new ConnectionInfo(Host, Port, Credentials.UserName, new PasswordAuthenticationMethod(Credentials.UserName, Credentials.Password)))) { sFtpClient.Connect(); using (var ms = new MemoryStream(content)) { await sFtpClient.UploadAsync(ms, path); } } } }
/// <summary> /// Defines a SFTP Client transaction /// </summary> /// <param name="_cred">The SSH transaction credentials</param> /// <param name="task">The Transaction task</param> public static void SFTPTransactionVoid(SiteCredentials _cred, Action <Renci.SshNet.SftpClient> task) { using (var client = new Renci.SshNet.SftpClient(_cred.Host, _cred.Port, _cred.User, _cred.Password)) { try { client.Connect(); task(client); client.Disconnect(); } catch (System.Exception exc) { Console.WriteLine(exc.Message); } } }
/// <summary> /// Sync output of current compilation to <paramref name="dir"/> /// </summary> /// <param name="dir"></param> /// <returns></returns> private bool SyncTo(string dir) { // Copy files over using (var sftp = new SftpClient(Machine, Port, Username, Password)) { sftp.Connect(); if (!sftp.IsConnected) { return false; } // Perform recursive copy of all the folders under `dir`. This is required // as the sftp client only synchronize directories at their level only, no // subdirectory. var dirs = new Queue<DirectoryInfo>(); dirs.Enqueue(new DirectoryInfo(dir)); var parentPath = new UDirectory(dir); while (dirs.Count != 0) { var currentDir = dirs.Dequeue(); var currentPath = new UDirectory(currentDir.FullName); foreach (var subdir in currentDir.EnumerateDirectories()) { dirs.Enqueue(subdir); } // Get the destination path by adding to `Location` the relative path of `dir` to `currentDir`. var destination = UPath.Combine(new UDirectory(Location.ItemSpec), currentPath.MakeRelative(parentPath)); Log.LogMessage("Synchronizing " + currentPath + " with " + destination.FullPath); // Try to create a remote directory. If it throws an exception, we will assume // for now that the directory already exists. See https://github.com/sshnet/SSH.NET/issues/25 try { sftp.CreateDirectory(destination.FullPath); Log.LogMessage("Creating remote directory " + destination.FullPath); } catch (SshException) { // Do nothing, as this is when the directory already exists } // Synchronize files. foreach (var file in sftp.SynchronizeDirectories(currentPath.FullPath, destination.FullPath, "*")) { Log.LogMessage("Updating " + file.Name); } } return true; } }
internal BasicRemoteOperations(IDeploymentContext context, IHost host) { Context = context; Sftp = new SftpClient(host.Hostname, host.Username, host.Password) { OperationTimeout = TimeSpan.FromMinutes(2), ConnectionInfo = {Timeout = TimeSpan.FromMinutes(2)}, BufferSize = 1024*16 }; Sftp.Connect(); Shell = new SshClient(host.Hostname, host.Username, host.Password); Shell.Connect(); ShellStream = Shell.CreateShellStream( String.Format("{0}@{1}", host.Username, host.Hostname), 0, 0, 0, 0, 1024); }
private void InternalConnect(string host, int port, string username, string password, string workingDirectory) { if (_isConnected) { // Change the working directory only if we use ScpClient. if (_scpClient != null) { ChangeWorkingDirectory(workingDirectory); } return; } // Restart timer _stopWatch.Restart(); _lastElapsedMilliseconds = 0; // Start connection ... PrintTime($"Connecting to {username}@{host}:{port} ..."); _sshClient = new SshClient(host, port, username, password); _sshClient.Connect(); try { // Use SFTP for file transfers var sftpClient = new SftpClient(host, port, username, password); sftpClient.Connect(); _sftpClient = sftpClient; } catch (Exception ex) { // Use SCP if SFTP fails PrintTime($"Error: {ex.Message} Is SFTP supported for {username}@{host}:{port}? We are using SCP instead!"); _scpClient = new ScpClient(host, port, username, password); _scpClient.Connect(); } var _connectionInfo = _sshClient.ConnectionInfo; PrintTime($"Connected to {_connectionInfo.Username}@{_connectionInfo.Host}:{_connectionInfo.Port} via SSH and {(_sftpClient != null ? "SFTP" : "SCP")}"); _isConnected = true; ChangeWorkingDirectory(workingDirectory); }
/// <summary> /// Defines a SFTP Client transaction /// </summary> /// <param name="_cred">The SSH transaction credentials</param> /// <param name="task">The Transaction task</param> /// <returns>The transaction result</returns> public static Object SFTPTransaction(SiteCredentials _cred, Func <Renci.SshNet.SftpClient, Object> task) { Object result = null; using (var client = new Renci.SshNet.SftpClient(_cred.Host, _cred.Port, _cred.User, _cred.Password)) { try { client.Connect(); result = task(client); client.Disconnect(); } catch (System.Exception exc) { Console.WriteLine(exc.Message); } } return(result); }
/// <summary> /// Downloads a directory /// </summary> /// <param name="client">The Sftp client</param> /// <param name="file">The directory used to download its files</param> /// <param name="filter">The download filter</param> /// <param name="silentDownload">Download the files without listing everything</param> public static void Download(this RenciSftpClient client, SiteCredentials credentials, MappedPath dir, SftpFilter filter, Boolean replace, Boolean silentDownload = false) { var files = SftpUtils.ListFiles(client, dir.GetFullRemotePath(), filter, silentDownload); string fileName, serverCopy; FileInfo cC, wC; AuraSftpClient.SSHTransactionVoid(credentials, (Action <AuraSftpClient>)((AuraSftpClient c) => { foreach (String remoteFile in files) { fileName = remoteFile.Replace((string)dir.GetFullRemotePath(), "").Substring(1).Replace("/", "\\"); serverCopy = Path.Combine((string)dir.GetFullServerCopy(), fileName); //Cache copy cC = new FileInfo(serverCopy); //The path to the working copy wC = new FileInfo(Path.Combine((string)dir.GetFullProjectCopy(), fileName)); DownloadFile(c, remoteFile, cC, wC, replace, silentDownload); } })); }
/// <summary> /// Test the credentials connection /// </summary> /// <param name="_cred">The credential connections</param> /// <param name="errMsg">The error message</param> /// <returns>True if the credentials are valid to connect to the host</returns> public static Boolean TestConnection(SiteCredentials _cred, out string errMsg) { Boolean result = true; errMsg = String.Empty; using (var client = new Renci.SshNet.SftpClient(_cred.Host, _cred.Port, _cred.User, _cred.Password)) { try { client.Connect(); client.Disconnect(); } catch (System.Exception exc) { errMsg = Message.MSG_ERR_NO_CONN + ".\n" + exc.Message; result = false; } } return(result); }
/******** EXTERNAL OBJECT PUBLIC METHODS - END ********/ private void SetupChannelSftp(SftpOptions options, bool useKey) { List <AuthenticationMethod> method = new List <AuthenticationMethod>(); if (useKey) { PrivateKeyFile keyFile = new PrivateKeyFile(options.KeyPath, options.KeyPassword); method.Add(new PrivateKeyAuthenticationMethod(options.User, keyFile)); } else { method.Add(new PasswordAuthenticationMethod(options.User, options.Password)); } ConnectionInfo con = new ConnectionInfo(options.Host, options.Port, options.User, method.ToArray()); if (options.AllowHostKeyChecking) { if (SecurityUtils.compareStrings("", options.KnownHostsPath)) { this.error.setError("SF009", "Options misconfiguration, known_hosts path is empty but host key checking is true"); return; } checkFingerpint(con, options.KnownHostsPath); if (this.fingerprint) { this.channel = new Renci.SshNet.SftpClient(con); } } else { this.channel = new Renci.SshNet.SftpClient(con); } }
RenciSftpClient InstantiateClient() { var connectionInfo = new ConnectionInfo(Options.Host, Options.UserName, new PasswordAuthenticationMethod(Options.UserName, Options.Password)); var client = new RenciSftpClient(connectionInfo); client.Connect(); if (!client.ConnectionInfo.IsAuthenticated) { throw new Exception("SFTP: Could not authenticate"); } if (!string.IsNullOrWhiteSpace(Options.RemoteWorkingDirectory)) { client.ChangeDirectory(Options.RemoteWorkingDirectory); if (client.WorkingDirectory != Options.RemoteWorkingDirectory) { throw new Exception("SFTP: Do not match"); } } return(client); }
public override async Task Connect(bool reconnecting = false) { Notifications.ChangeTrayText(reconnecting ? MessageType.Reconnecting : MessageType.Connecting); Log.Write(l.Debug, "{0} client...", reconnecting ? "Reconnecting" : "Connecting"); ConnectionInfo connectionInfo; if (Controller.IsPrivateKeyValid) { connectionInfo = new PrivateKeyConnectionInfo(Controller.Account.Host, Controller.Account.Port, Controller.Account.Username, new PrivateKeyFile(Controller.Account.PrivateKeyFile, Controller.Account.Password)); } else { connectionInfo = new PasswordConnectionInfo(Controller.Account.Host, Controller.Account.Port, Controller.Account.Username, Controller.Account.Password); } connectionInfo.Encoding = this.Charset; connectionInfo.AuthenticationBanner += (o, x) => Log.Write(l.Warning, x.BannerMessage); _sftpc = new Renci.SshNet.SftpClient(connectionInfo); _sftpc.HostKeyReceived += (o, x) => { var fingerPrint = x.FingerPrint.GetCertificateData(); // if ValidateCertificate handler isn't set, accept the certificate and move on if (ValidateCertificate == null || Settings.TrustedCertificates.Contains(fingerPrint)) { Log.Write(l.Client, "Trusted: {0}", fingerPrint); x.CanTrust = true; return; } var e = new ValidateCertificateEventArgs { Fingerprint = fingerPrint, Key = x.HostKeyName, KeySize = x.KeyLength.ToString() }; // Prompt user to validate ValidateCertificate?.Invoke(null, e); x.CanTrust = e.IsTrusted; }; var caughtException = default(Exception); await Task.Run(() => { try { _sftpc.Connect(); } catch (SshAuthenticationException ex) { ex.LogException(); caughtException = new AuthenticationException(ex.Message, ex.InnerException); } catch (SshConnectionException ex) { ex.LogException(); caughtException = new CertificateDeclinedException(ex.Message, ex.InnerException); } catch (Exception ex) { ex.LogException(); caughtException = ex; } }); if (caughtException != default(Exception)) { throw caughtException; } _sftpc.ErrorOccurred += async(o, e) => { if (!IsConnected) { Notifications.ChangeTrayText(MessageType.Nothing); } OnConnectionClosed(new ConnectionClosedEventArgs { Text = e.Exception.Message }); if (e.Exception is SftpPermissionDeniedException) { Log.Write(l.Warning, "Permission denied error occured"); } if (e.Exception is SshConnectionException) { await Reconnect(); } }; Controller.HomePath = WorkingDirectory; if (IsConnected) { if (!string.IsNullOrWhiteSpace(Controller.Paths.Remote) && !Controller.Paths.Remote.Equals("/")) { WorkingDirectory = Controller.Paths.Remote; } } Log.Write(l.Debug, "Client connected sucessfully"); Notifications.ChangeTrayText(MessageType.Ready); if (Settings.IsDebugMode) { LogServerInfo(); } }
public SecureCopier(ResourceNode node) { Log.Info("SCP: Establishing connection to node " + node.ResourceName + "." + node.NodeName); var nodeAddress = node.NodeAddress; // todo : OR ExecutionUrl????!!!!! var addrParts = node.NodeAddress.Split(':'); int port = DEFAULT_SCP_PORT; if (addrParts.Length == 2) { int.TryParse(addrParts[1], out port); nodeAddress = addrParts[0]; } // if resource asks us for password interactively: var interactiveAuthMethod = new KeyboardInteractiveAuthenticationMethod(node.Credentials.Username); interactiveAuthMethod.AuthenticationPrompt += delegate(object sender, Renci.SshNet.Common.AuthenticationPromptEventArgs e) { foreach (var prompt in e.Prompts) { Log.Debug("Interactive request by resource node " + node.NodeName + ": '" + prompt.Request + "'"); if (prompt.Request.ToLowerInvariant().Contains("password")) { Log.Debug("Responding by password"); prompt.Response = node.Credentials.Password; } } }; ConnectionInfo connectionInfo; if (!String.IsNullOrWhiteSpace(node.Credentials.CertFile)) { connectionInfo = new ConnectionInfo(nodeAddress, port, node.Credentials.Username, new PrivateKeyAuthenticationMethod( node.Credentials.Username, new PrivateKeyFile(node.Credentials.CertFile, node.Credentials.Password) ) ); } else { connectionInfo = new ConnectionInfo(nodeAddress, port, node.Credentials.Username, new PasswordAuthenticationMethod(node.Credentials.Username, node.Credentials.Password), interactiveAuthMethod ); } try { _sftpClient = new SftpClient(connectionInfo); _sftpClient.Connect(); _scp = null; } catch (Exception e) { Log.Warn("Unable to use sftp. Rolling bask to SCP for resource node " + node.ResourceName + "." + node.NodeName + ": " + e.ToString()); _sftpClient = null; _scp = new Scp(nodeAddress, node.Credentials.Username, node.Credentials.Password); _scp.Connect(port); } }
public SftpClientConnection(string host, NetworkCredential credentials, int port = 2222) : base(host, credentials) { Port = port; _client = _getSFtpClient(); }
public SftpClientConnection(Uri uri, NetworkCredential credentials) : base(uri, credentials) { _client = _getSFtpClient(); }
public void ftpConnect() { ftp = new SftpClient(connectionInfo); ftp.Connect(); }
public SftpClientConnection(FtpConfig ftpConfig) : base(ftpConfig) { _client = _getSFtpClient(); }
public void CreateConnection() { try { if (string.IsNullOrWhiteSpace(_connectivitySettings.PrivateKeyPath)) { #region guard clause if (string.IsNullOrWhiteSpace(_connectivitySettings.Host) || string.IsNullOrWhiteSpace(_connectivitySettings.UserName) || string.IsNullOrWhiteSpace(_connectivitySettings.Password)) { Exception connectivityException = new Exception($"One or more connectivity-settings are incorrect: Host={_connectivitySettings.Host}, UserName={_connectivitySettings.UserName}, password=*******"); _logger.LogError(connectivityException, connectivityException.Message); throw connectivityException; } #endregion guard clause // connect using just username/password: _sftpClient = new Renci.SshNet.SftpClient( _connectivitySettings.Host, _connectivitySettings.Port, _connectivitySettings.UserName, _connectivitySettings.Password); } else { #region guard clause if (string.IsNullOrWhiteSpace(_connectivitySettings.Host) || string.IsNullOrWhiteSpace(_connectivitySettings.UserName) || String.IsNullOrWhiteSpace(_connectivitySettings.Password) || string.IsNullOrWhiteSpace(_connectivitySettings.PrivateKeyPath)) { Exception connectivityException = new Exception($"One or more connectivity-settings are incorrect: Host={_connectivitySettings.Host}, UserName={_connectivitySettings.UserName}, privateKeyPath={_connectivitySettings.PrivateKeyPath}"); _logger.LogError(connectivityException, connectivityException.Message); throw connectivityException; } #endregion guard clause // connect using just username/private key file/password as pass-phrase: _logger.LogInformation($"Will log on with keyfile {_connectivitySettings.PrivateKeyPath}, with the pass-phrase given."); VerifyPrivateKeyFileIsReadable(_connectivitySettings.PrivateKeyPath); VerifyPrivateKeyIsNotPuttyFormat(_connectivitySettings.PrivateKeyPath); PrivateKeyFile keyFile = new PrivateKeyFile(File.OpenRead(_connectivitySettings.PrivateKeyPath), @_connectivitySettings.Password); _sftpClient = new Renci.SshNet.SftpClient( _connectivitySettings.Host, _connectivitySettings.Port, _connectivitySettings.UserName, keyFile); } _logger.LogInformation($"Will connect to {_connectivitySettings.Host}, port {_connectivitySettings.Port}..."); // Retry, waiting a specified duration between each retry var policy = Policy .Handle <SocketException>() .WaitAndRetry(DEFAULT_NUMBER_OF_CONNECTION_RETRIES, retryNumber => TimeSpan.FromSeconds(retryNumber * DEFAULT_WAIT_SECONDS), (ex, tsp) => { // method to call on retries. // TODO: implement logging here. System.Diagnostics.Debug.WriteLine($"Trying again in {tsp}"); }); policy.Execute(() => _sftpClient.Connect()); } catch (Exception exception) when(exception.Message == @"Invalid private key file.") // Catch specific key file error, redirect to possible solution. { // The SSH.Net component supports RSA and DSA private key in both OpenSSH and SSH.COM format. // Some tools, such as PuTTYgen, generates key-files that need to be converted to another format in order to work with SSH.Net. Exception innerException = new Exception($@"Invalid key file, possible https://stackoverflow.com/questions/43915130/does-ssh-net-accept-only-openssh-format-of-private-key-if-not-what-are-the-res issue.", exception); // wrap exception in own specific exception. DotNetSftpClientException dotNetSftpClientException = new DotNetSftpClientException("DotNetSftpClient error, see inner Exception for details", innerException); throw dotNetSftpClientException; } catch (Exception exception) { // wrap exception in own specific exception. DotNetSftpClientException dotNetSftpClientException = new DotNetSftpClientException("DotNetSftpClient error, see inner Exception for details", exception); throw dotNetSftpClientException; } }
public SftpClient() : base() { this.channel = null; this.fingerprint = false; this.whiteList = null; }
public Response UploadSFTP(DataExtensionImport dataExtensionImport) { var response = new Response { Success = true, Warning = false }; try { const int port = 22; const string host = "ftp.s6.exacttarget.com"; const string username = "******"; const string password = "******"; const string workingdirectory = "/Import//"; using (var client = new SftpClient(host, port, username, password)) { client.Connect(); client.ChangeDirectory(workingdirectory); string extension = Path.GetExtension(dataExtensionImport.Ruta); string nombreArchivo = string.Format("{0}{1}", dataExtensionImport.Nombre, extension); using (var fileStream = new FileStream(dataExtensionImport.Ruta, FileMode.Open)) { client.BufferSize = 4 * 1024; // bypass Payload error large files client.UploadFile(fileStream, nombreArchivo); } } } catch (Exception ex) { response.Success = false; response.Message = ex.Message; } return response; }
//turn this into some kind of interface public GatherResults DownloadQtyFiles(IVendorDirectory dir) { var files = new List<File>(); using (var sftp = new SftpClient(_config.QtyFileHost, _config.QtyFilePort, _config.QtyFileUsername, _config.QtyFilePassword)) { sftp.Connect(); dir.EnsureExists(); var localFile = dir.GetNewRawFile(FileTypes.Qty); using (var stream = new FileStream(localFile.Name.GetPath(), FileMode.Create)) { sftp.DownloadFile(_config.QtyFileName, stream); } files.Add(localFile); sftp.Disconnect(); } return new GatherResults { VendorHandle = dir.VendorHandle, Files = files }; }
public void Initialize() { if (SftpClient != null) return; SftpClient = new SftpClient(Host, Port, User, Password); SftpClient.Connect(); // Try to setup a ssh connection to speedup file listing try { SshClient = new SshClient(Host, Port, User, Password); SshClient.Connect(); } catch { SshClient = null; } }
public override DTSExecResult Execute(Connections connections, VariableDispenser variableDispenser, IDTSComponentEvents componentEvents, IDTSLogging log, object transaction) { try { using (var sftp = new SftpClient(Host, Port, Username, Password)) { sftp.Connect(); using (var file = File.OpenWrite(TargetPath)) { sftp.DownloadFile(SourcePath, file); } return DTSExecResult.Success; } } catch (Exception ex) { log.Write(string.Format("{0}.Execute", GetType().FullName), ex.ToString()); return DTSExecResult.Failure; } }
private void Uploadfiles(PasswordConnectionInfo connectionInfo, IEnumerable<AchFileEntity> achFilesToUpload) { using (var sftp = new SftpClient(connectionInfo)) { try { sftp.Connect(); foreach (var achfile in achFilesToUpload) { using (var stream = new MemoryStream()) { var fileName = achfile.Name + ".ach"; var writer = new StreamWriter(stream); // writer.Write(achfile.AchFileBody); writer.Flush(); stream.Position = 0; sftp.UploadFile(stream, fileName); this.Manager.ChangeAchFilesStatus(achfile, AchFileStatus.Uploaded); this.Manager.UnLock(achfile); } } } finally { sftp.Disconnect(); } } }