public clsSFTP() { #if (DEBUG) m_rec_sftp = (from s in Program.dbData3060.Tblsftp where s.Navn == "TestHD35" select s).First(); //m_rec_sftp = (from s in Program.dbData3060.Tblsftp where s.Navn == "Test" select s).First(); //m_rec_sftp = (from s in Program.dbData3060.Tblsftp where s.Navn == "Produktion" select s).First(); #else m_rec_sftp = (from s in Program.dbData3060.Tblsftp where s.Navn == "Produktion" select s).First(); #endif m_sftp = new SFtp(); bool success = m_sftp.UnlockComponent("HAFSJOSSH_6pspJCMP1QnW"); if (!success) { throw new Exception(m_sftp.LastErrorText); } m_sftp.ConnectTimeoutMs = 5000; m_sftp.IdleTimeoutMs = 15000; success = m_sftp.Connect(m_rec_sftp.Host, int.Parse(m_rec_sftp.Port)); if (!success) { throw new Exception(m_sftp.LastErrorText); } Chilkat.SshKey key = new Chilkat.SshKey(); string privKey = m_rec_sftp.Certificate; if (privKey == null) { throw new Exception(m_sftp.LastErrorText); } key.Password = m_rec_sftp.Pincode; success = key.FromOpenSshPrivateKey(privKey); if (!success) { throw new Exception(m_sftp.LastErrorText); } success = m_sftp.AuthenticatePk(m_rec_sftp.User, key); if (!success) { throw new Exception(m_sftp.LastErrorText); } // After authenticating, the SFTP subsystem must be initialized: success = m_sftp.InitializeSftp(); if (!success) { throw new Exception(m_sftp.LastErrorText); } }
private void btnConnect_Click(object sender, EventArgs e) { _sftp = new SFtp(); // Any string automatically begins a fully-functional 30-day trial. var success = _sftp.UnlockComponent("Anything for 30-day trial"); if (success != true) { WriteLog(_sftp.LastErrorText); return; } // Set some timeouts, in milliseconds: _sftp = new SFtp { ConnectTimeoutMs = 15000, IdleTimeoutMs = 15000 }; // Connect to the SSH server. // The standard SSH port = 22 const int port = 22; success = _sftp.Connect(tbIP.Text, port); if (success != true) { WriteLog(_sftp.LastErrorText); return; } // Authenticate with the SSH server. Chilkat SFTP supports // both password-based authenication as well as public-key // authentication. This example uses password authenication. success = _sftp.AuthenticatePw(tbUser.Text, tbPassword.Text); if (success != true) { WriteLog(_sftp.LastErrorText); return; } // After authenticating, the SFTP subsystem must be initialized: success = _sftp.InitializeSftp(); if (success != true) { WriteLog(_sftp.LastErrorText); } else { WriteLog("Connect successfully"); GetListDirectories(); } }
public void UploadViaSftp(string zipFileName, string sftpUrl, string userName, string password) { SFtp sftp = new SFtp(); // Any string automatically begins a fully-functional 30-day trial. bool success; success = sftp.UnlockComponent("HURRIYSSH_XAN0ZUeq9En4"); if (success != true) { throw new ApplicationException(sftp.LastErrorText); } // Set some timeouts, in milliseconds: sftp.ConnectTimeoutMs = 60000; sftp.IdleTimeoutMs = 300000; // Connect to the SSH server. // The standard SSH port = 22 // The hostname may be a hostname or IP address. int port; string hostname; hostname = sftpUrl; port = 22; success = sftp.Connect(hostname, port); if (success != true) { throw new ApplicationException(sftp.LastErrorText); } // Authenticate with the SSH server. Chilkat SFTP supports // both password-based authenication as well as public-key // authentication. This example uses password authenication. success = sftp.AuthenticatePw(userName, password); if (success != true) { throw new ApplicationException(sftp.LastErrorText); } // After authenticating, the SFTP subsystem must be initialized: success = sftp.InitializeSftp(); if (success != true) { throw new ApplicationException(sftp.LastErrorText); } // Open a file on the server for writing. // "createTruncate" means that a new file is created; if the file already exists, it is opened and truncated. string handle; handle = sftp.OpenFile(Path.GetFileName(zipFileName), "writeOnly", "createTruncate"); if (handle == null) { throw new ApplicationException(sftp.LastErrorText); } // Upload from the local file to the SSH server. success = sftp.UploadFile(handle, zipFileName); if (success != true) { throw new ApplicationException(sftp.LastErrorText); } // Close the file. success = sftp.CloseHandle(handle); if (success != true) { throw new ApplicationException(sftp.LastErrorText); } }
public static SFtp GetConnection(Connection connection) { if (connection == null) { throw new InvalidOperationException("Source Connection Is NULL"); } if (string.IsNullOrEmpty(connection.Server)) { throw new InvalidOperationException("Source Connection Server Details Not Found"); } if (string.IsNullOrEmpty(connection.User)) { throw new InvalidOperationException("Source Connection User Details Not Found"); } if (string.IsNullOrEmpty(connection.Password)) { throw new InvalidOperationException("Source Connection Password Details Not Found"); } if (connection.Port <= 0) { connection.Port = 22; } if (connection.ConnectionTimeOutMiliSeconds <= 5000) { connection.ConnectionTimeOutMiliSeconds = 5000; } if (connection.IdleTimeoutMiliSeconds <= 5000) { connection.IdleTimeoutMiliSeconds = 5000; } var sftp = new SFtp(); var success = sftp.UnlockComponent("Anything for 30-day trial"); if (success != true) { throw new InvalidOperationException(sftp.LastErrorText); } sftp.ConnectTimeoutMs = connection.ConnectionTimeOutMiliSeconds; sftp.IdleTimeoutMs = connection.IdleTimeoutMiliSeconds; var hostname = connection.Server; var port = connection.Port; success = sftp.Connect(hostname, port); if (success != true) { throw new InvalidOperationException(sftp.LastErrorText); } var user = connection.User; var password = connection.Password; success = sftp.AuthenticatePw(user, password); if (success != true) { throw new InvalidOperationException(sftp.LastErrorText); } success = sftp.InitializeSftp(); if (success != true) { throw new InvalidOperationException(sftp.LastErrorText); } return(sftp); }
internal SftpLogFileInfoChilkat(SftpFileSystem sftFileSystem, Uri fileUri, ILogExpertLogger logger) { _logger = logger; _sftFileSystem = sftFileSystem; Uri = fileUri; _remoteFileName = Uri.PathAndQuery; // Any string automatically begins a fully-functional 30-day trial. bool success; success = _sftp.UnlockComponent("PUT_SERIAL_HERE"); if (success != true) { _logger.LogError(_sftp.LastErrorText); MessageBox.Show(_sftp.LastErrorText); return; } int port = Uri.Port != -1 ? Uri.Port : 22; success = _sftp.Connect(Uri.Host, port); if (success != true) { _logger.LogError(_sftp.LastErrorText); MessageBox.Show(_sftp.LastErrorText); return; } success = false; bool cancelled = false; if (_sftFileSystem.ConfigData.UseKeyfile) { lock (_sshKeyMonitor) // prevent multiple password dialogs when opening multiple files at once { while (_sftFileSystem.SshKey == null) { // Load key from file, possibly encrypted by password SshKey sshKey = new SshKey(); string keyText = sshKey.LoadText(_sftFileSystem.ConfigData.KeyFile); PrivateKeyPasswordDialog dlg = new PrivateKeyPasswordDialog(); DialogResult dialogResult = dlg.ShowDialog(); if (dialogResult == DialogResult.Cancel) { cancelled = true; break; } sshKey.Password = dlg.Password; if (_sftFileSystem.ConfigData.KeyType == KeyType.Ssh) { logger.Info("Loading SSH key from " + _sftFileSystem.ConfigData.KeyFile); success = sshKey.FromOpenSshPrivateKey(keyText); } else { logger.Info("Loading Putty key from " + _sftFileSystem.ConfigData.KeyFile); success = sshKey.FromPuttyPrivateKey(keyText); } if (!success) { MessageBox.Show("Loading key file failed"); } else { _sftFileSystem.SshKey = sshKey; } } } if (!cancelled) { success = false; Credentials credentials = _sftFileSystem.GetCredentials(Uri, true, true); while (!success) { success = _sftp.AuthenticatePk(credentials.UserName, _sftFileSystem.SshKey); if (!success) { FailedKeyDialog dlg = new FailedKeyDialog(); DialogResult res = dlg.ShowDialog(); dlg.Dispose(); if (res == DialogResult.Cancel) { return; } if (res == DialogResult.OK) { break; // go to user/pw auth } // retries with disabled cache credentials = _sftFileSystem.GetCredentials(Uri, false, true); } } } } if (!success) { // username/password auth Credentials credentials = _sftFileSystem.GetCredentials(Uri, true, false); success = _sftp.AuthenticatePw(credentials.UserName, credentials.Password); if (success != true) { // first fail -> try again with disabled cache credentials = _sftFileSystem.GetCredentials(Uri, false, false); success = _sftp.AuthenticatePw(credentials.UserName, credentials.Password); if (success != true) { // 2nd fail -> abort MessageBox.Show("Authentication failed!"); //MessageBox.Show(sftp.LastErrorText); return; } } } success = _sftp.InitializeSftp(); if (success != true) { _logger.LogError(_sftp.LastErrorText); MessageBox.Show(_sftp.LastErrorText); return; } OriginalLength = _lastLength = Length; }
public clsSFTP() { clsRest objRest = new clsRest(); string strxmldata = objRest.HttpGet2(clsRest.urlBaseType.data, "sftp/" + Program.sftpName); XDocument xdoc = XDocument.Parse(strxmldata); string Status = xdoc.Descendants("Status").First().Value; if (Status != "True") { throw new Exception("Getting sftp-data for " + Program.sftpName + " failed."); } m_SftpId = xdoc.Descendants("Id").First().Value; m_SftpNavn = xdoc.Descendants("Navn").First().Value; m_Host = xdoc.Descendants("Host").First().Value; m_Port = xdoc.Descendants("Port").First().Value; m_Certificate = xdoc.Descendants("Certificate").First().Value; m_Pincode = xdoc.Descendants("Pincode").First().Value; m_User = xdoc.Descendants("User").First().Value; m_Outbound = xdoc.Descendants("Outbound").First().Value; m_Inbound = xdoc.Descendants("Inbound").First().Value; m_sftp = new SFtp(); bool success = m_sftp.UnlockComponent("HAFSJOSSH_6pspJCMP1QnW"); if (!success) { throw new Exception(m_sftp.LastErrorText); } m_sftp.ConnectTimeoutMs = 60000; m_sftp.IdleTimeoutMs = 55000; success = m_sftp.Connect(m_Host, int.Parse(m_Port)); if (!success) { throw new Exception(m_sftp.LastErrorText); } Chilkat.SshKey key = new Chilkat.SshKey(); string privKey = m_Certificate; if (privKey == null) { throw new Exception(m_sftp.LastErrorText); } key.Password = m_Pincode; success = key.FromOpenSshPrivateKey(privKey); if (!success) { throw new Exception(m_sftp.LastErrorText); } success = m_sftp.AuthenticatePk(m_User, key); if (!success) { throw new Exception(m_sftp.LastErrorText); } // After authenticating, the SFTP subsystem must be initialized: success = m_sftp.InitializeSftp(); if (!success) { throw new Exception(m_sftp.LastErrorText); } }