public void Dispose() { m_scpAdminClient?.Dispose(); m_sshUserClient?.Dispose(); m_scpUserClient?.Dispose(); m_scpAdminClient?.Dispose(); }
public void Disconnect(bool cleanup = true) { if (cleanup) { SshClient.RunCommand(Prepend + "cd ..; rm -r " + DirName).Execute(); } else { string dest = "FunctionalTest-" + Info.Host; var res = SshClient.RunCommand(Prepend + "cd ..; mv " + DirName + " " + dest).Execute(); } if (m_ssh != null) { m_ssh.Disconnect(); m_ssh.Dispose(); m_ssh = null; } if (m_scp != null) { m_scp.Disconnect(); m_scp.Dispose(); m_scp = null; } }
public void Dispose() { _shellWriterStream.Dispose(); _shellStream.Dispose(); _sshClient.Dispose(); _scpClient.Dispose(); _timer.Dispose(); }
public void Dispose() { sshClient?.Disconnect(); sshClient?.Dispose(); scpClient?.Disconnect(); scpClient?.Dispose(); }
public void Dispose() { if (Protocol == SSHTransferProtocol.SCP) { ScpClt.Dispose(); } if (Protocol == SSHTransferProtocol.SFTP) { SftpClt.Dispose(); } }
/// <summary> /// Dispose /// </summary> public void DisposeSCPClient() { try { if (scpClient != null) { scpClient.Disconnect(); scpClient.Dispose(); } } finally { scpClient = null; } }
static void Main(string[] args) { Log.Logger = new LoggerConfiguration() .WriteTo.Console() .CreateLogger(); var host = args[0]; var name = args[1]; var pwd = args[2]; using var scpClient = new ScpClient(host, 20002, name, pwd); scpClient.Connect(); if (scpClient.IsConnected) { Log.Logger.Information("SCP服务器连接成功"); } else { Log.Logger.Error("SCP服务器连接失败"); return; } var md5s = Directory.GetFiles(Directory.GetCurrentDirectory(), "*.md5"); md5s.ToList().ForEach(_ => { scpClient.Upload(File.OpenRead(_), $"/var/www/html/files/{Path.GetFileName(_)}"); }); var fs = File.OpenRead("./Minecraft-Mod-Language-Package.zip"); scpClient.Upload(fs, "/var/www/html/files/Minecraft-Mod-Language-Modpack.zip.1"); Log.Logger.Information("上传成功"); scpClient.Dispose(); using var sshClient = new SshClient(host, 20002, name, pwd); sshClient.Connect(); if (sshClient.IsConnected) { Log.Logger.Information("SSH服务器连接成功"); } else { Log.Logger.Error("SSH服务器连接失败"); return; } using var cmd = sshClient.CreateCommand("mv /var/www/html/files/Minecraft-Mod-Language-Modpack.zip.1 /var/www/html/files/Minecraft-Mod-Language-Modpack.zip"); cmd.Execute(); var err = cmd.Error; Log.Logger.Error(err); sshClient.Dispose(); }
private void Dispose(bool disposing) { if (!disposing) { return; } if (Protocol == SSHTransferProtocol.SCP) { ScpClt.Dispose(); } if (Protocol == SSHTransferProtocol.SFTP) { SftpClt.Dispose(); } }
protected virtual void Dispose(bool disposing) { if (disposing) { if (sshClient != null) { sshClient.Disconnect(); sshClient.Dispose(); } if (scpClient != null) { scpClient.Disconnect(); scpClient.Dispose(); } } // free native resources if there are any. }
internal void DestroyScpClient(ScpClient c, [CallerMemberName] string memberName = "", [CallerFilePath] string fileName = "", [CallerLineNumber] int lineNumber = 0) { CallerInformation caller = new CallerInformation(memberName, fileName, lineNumber); if (!scp_clients.Contains(c)) { throw new ArgumentException("The ScpClient does not exist in the scp_clients dictionary."); } if (c.IsConnected) { c.Disconnect(); } c.ErrorOccurred -= ScpClient_ErrorOccurred; c.Downloading -= ScpClient_Downloading; c.Dispose(); scp_clients.Remove(c); Debug(caller, "Destroyed SCP connection to {0}.", this.HostName); }
static void Main(string[] args) { Log.Logger = new LoggerConfiguration() .WriteTo.Console() .CreateLogger(); var host = args[0]; var name = args[1]; var pwd = args[2]; using var scpClient = new ScpClient(host, 12356, name, pwd); scpClient.Connect(); if (scpClient.IsConnected) { Log.Logger.Information("SCP服务器连接成功"); } else { Log.Logger.Error("SCP服务器连接失败"); return; } var fs = File.OpenRead("./Minecraft-Mod-Language-Package.zip"); scpClient.Upload(fs, "/var/www/html/files/Minecraft-Mod-Language-Modpack.zip.1"); Log.Logger.Information("上传成功"); scpClient.Dispose(); using var sshClient = new SshClient(host, 12356, name, pwd); sshClient.Connect(); if (sshClient.IsConnected) { Log.Logger.Information("SSH服务器连接成功"); } else { Log.Logger.Error("SSH服务器连接失败"); return; } using var cmd = sshClient.CreateCommand("mv /var/www/html/files/Minecraft-Mod-Language-Modpack.zip.1 /var/www/html/files/Minecraft-Mod-Language-Modpack.zip"); cmd.Execute(); var err = cmd.Error; Log.Logger.Error(err); sshClient.Dispose(); }
static int Main(string[] args) { Log.Logger = new LoggerConfiguration() .WriteTo.Console() .CreateLogger(); var command = new RootCommand() { new Option <string>( "--version", getDefaultValue: (() => "1.12.2"), description: "Set upload version."), new Option <string>( "--host", getDefaultValue: (() => ""), description: "Set the host."), new Option <string>( "--name", description: "Set user name."), new Option <string>( "--password", description: "Set password.") }; command.Description = "Config of uploader"; command.Handler = CommandHandler.Create <string, string, string, string>((version, host, name, password) => { using var scpClient = new ScpClient(host, 20002, name, password); scpClient.Connect(); if (scpClient.IsConnected) { Log.Logger.Information("SCP服务器连接成功"); } else { Log.Logger.Error("SCP服务器连接失败"); return; } var md5s = Directory.GetFiles(Directory.GetCurrentDirectory(), "*.md5"); md5s.ToList().ForEach(_ => { scpClient.Upload(File.OpenRead(_), $"/var/www/html/files/{Path.GetFileName(_)}"); }); var fs = File.OpenRead($"./Minecraft-Mod-Language-Package-{version}.zip"); switch (version) { case "1.12.2": scpClient.Upload(fs, "/var/www/html/files/Minecraft-Mod-Language-Modpack.zip.1"); break; case "1.16": scpClient.Upload(fs, "/var/www/html/files/Minecraft-Mod-Language-Modpack-1-16.zip.1"); break; default: break; //不应该 } Log.Logger.Information("上传成功"); scpClient.Dispose(); using var sshClient = new SshClient(host, 20002, name, password); sshClient.Connect(); if (sshClient.IsConnected) { Log.Logger.Information("SSH服务器连接成功"); } else { Log.Logger.Error("SSH服务器连接失败"); return; } switch (version) { case "1.12.2": var cmd1 = sshClient.CreateCommand("mv /var/www/html/files/Minecraft-Mod-Language-Modpack.zip.1 /var/www/html/files/Minecraft-Mod-Language-Modpack.zip"); cmd1.Execute(); var err1 = cmd1.Error; Log.Logger.Error(err1); break; case "1.16": var cmd = sshClient.CreateCommand("mv /var/www/html/files/Minecraft-Mod-Language-Modpack-1-16.zip.1 /var/www/html/files/Minecraft-Mod-Language-Modpack-1-16.zip"); cmd.Execute(); var err = cmd.Error; Log.Logger.Error(err); break; default: break; //不应该 } sshClient.Dispose(); }); return(command.Invoke(args)); }
/// <summary> /// Create the connection. The connection is not actually made until it is actually requested. /// </summary> /// <param name="host"></param> /// <param name="userName"></param> public SSHConnection(string host, string userName) { UserName = userName; MachineName = host; // Fetch the username and password. string password = FetchUserCredentials(host, userName); if (password == null) { throw new ArgumentException(string.Format("Please create a generic windows credential with '{0}' as the target address, '{1}' as the username, and the password for remote SSH access to that machine.", host, userName)); } // Create the connection, but do it lazy so we don't do anything if we aren't used. _client = new Lazy <SshClient>(() => { var c = new SshClient(host, userName, password); try { ConnectSSHRobustly(c); return(c); } catch (Exception e) { c.Dispose(); throw new SSHConnectFailureException($"Unable to make the connection to {host}.", e); } }); // Create the scp client for copying things _scp = new Lazy <ScpClient>(() => { var c = new ScpClient(host, userName, password); try { ConnectSSHRobustly(c); c.ErrorOccurred += (sender, error) => _scpError = error.Exception; return(c); } catch { c.Dispose(); throw; } }); // And create a shell stream. Initialize to find the prompt so we can figure out, later, when // a task has finished. _shell = new AsyncLazy <ShellStream>(async() => { var s = _client.Value.CreateShellStream("Commands", TerminalWidth, 200, 132, 80, 240 * 200); try { _prompt = await ExtractPromptText(s, _client.Value); } catch { s.Dispose(); throw; } return(s); }, AsyncLazyFlags.RetryOnFailure); }
public void Close() { if (_ssh != null) _ssh.Dispose(); if (_scp != null) _scp.Dispose(); if (_sftp != null) _sftp.Dispose(); }
private static Task HandleOneHostAsync(ConnectionInfo connectionInfo, IList <string> pathList) { // 构造在主线程构造,防止在多个线程连接多个节点,信息冲突 if (CommandLine.IsVerbose) { Console.WriteLine($"正在连接 {connectionInfo.Host}"); } ScpClient scpClient = new ScpClient(connectionInfo); SshClient sshClient = new SshClient(connectionInfo); scpClient.Connect(); sshClient.Connect(); if (CommandLine.IsVerbose) { Console.WriteLine($"连接成功 {connectionInfo.Host}"); } return(Task.Factory.StartNew(() => { foreach (string s in pathList) { string parent = Path.GetDirectoryName(s); if (CommandLine.IsVerbose) { Console.WriteLine($"正在转输 {s} 至 {connectionInfo.Host}"); } // 文件操作 if (File.Exists(s)) { FileInfo fileInfo = new FileInfo(s); sshClient.RunCommand($"mkdir -p {parent}"); scpClient.Upload(fileInfo, s); Console.WriteLine($"文件完成:远程主机{connectionInfo.Host} 远程路径{s} 大小 {fileInfo.Length} 字节"); } // 文件夹操作 else if (Directory.Exists(s)) { DirectoryInfo directoryInfo = new DirectoryInfo(s); sshClient.RunCommand($"mkdir -p {parent}"); if (CommandLine.ExistArgument <DeleteFolderArgument>()) { sshClient.RunCommand($"rm -rf {s}"); Console.WriteLine($"删除文件夹:远程主机{connectionInfo.Host} 远程路径{s}"); } scpClient.Upload(directoryInfo, s); Console.WriteLine($"文件夹完成:远程主机{connectionInfo.Host} 远程路径{s}"); } } Task.Factory.StartNew(() => { sshClient.Dispose(); if (CommandLine.IsVerbose) { Console.WriteLine($"SSH连接断开:{connectionInfo.Host}"); } }); Task.Factory.StartNew(() => { scpClient.Dispose(); if (CommandLine.IsVerbose) { Console.WriteLine($"SCP连接断开:{connectionInfo.Host}"); } }); })); }