experience for a beginner using Renci.SshNet #1289
CongZhengCZ
started this conversation in
General
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
For a beginner using Renci.SshNet in a Windows WinForms application with .NET Framework version 4.5.2, here are some coding experiences:
Handling Sequential Execution of SFTP and SSH:
When executing SFTP and then wanting to execute SSH without closing the SFTP connection, or vice versa, you may encounter the error "The remote host forcibly closed an existing connection." To avoid this, it's essential to close the current connection (either SFTP or SSH) before attempting to establish the other.
csharp
Copy code
// Close existing SFTP connection
if (sftpClient != null && sftpClient.IsConnected) sftpClient.Disconnect();
if (sftpClient != null) sftpClient.Dispose();
sftpClient = null;
// Close existing SSH connection
if (sshClient != null && sshClient.IsConnected) sshClient.Disconnect();
if (sshClient != null) sshClient.Dispose();
sshClient = null;
Graceful Closure of SSH Connection:
When closing an SSH connection to a Debian system (e.g., 192.168.2.5), ensure that the system is still running normally. If the system is in the process of restarting when the connection is closed, it may lead to issues. To mitigate this, consider waiting for the system to stabilize before closing the SSH connection.
Switching Between IPs:
When switching from connecting to IP_A to IP_B, it's crucial to first disconnect the existing SSH or SFTP connection, clear the connection keys for IP_A, and then establish a connection to IP_B.
csharp
Copy code
if (connectionInfo == null)
{
// Create SSH connection info for IP_B
connectionInfo = new ConnectionInfo(IP_B, this.username,
new PasswordAuthenticationMethod(this.username, this.password));
}
// Establish SFTP connection to IP_B
if (sftpClient == null)
{
sftpClient = new SftpClient(connectionInfo);
sftpClient.Connect();
}
Clearing Connection Keys for Repeated Commands:
Even when repeatedly executing commands on the same IP (e.g., 192.168.2.5), it's advisable to clear the connection keys before reconnecting to ensure smooth execution of commands.
csharp
Copy code
// Clear connection keys for 2.5
connectionInfo = null;
// Reconnect to 2.5
// ...
Additionally, ensure that the connection keys are cleared before executing subsequent functions to avoid SSH connection exceptions.
Sequential Execution of Functions:
When executing a series of functions (A, B, and C), where each function involves SSH or SFTP operations, make sure to clear the connection keys before executing each function to ensure smooth execution.
csharp
Copy code
// Before executing Function B
connectionInfo = null;
// Execute Function B (SFTP operations)
// ...
// Before executing Function C
connectionInfo = null;
// Execute Function C (SSH or SFTP operations)
// ...
These practices should help in managing SSH and SFTP connections in a WinForms application effectively.
以上内容实际就是下面的中文:
给使用Renci.SshNet的新手,说些代码开发经验
由于windows的winform程序的.Net Framework版本是4.5.2
所以用了Renci.SshNet的版本,2020.0.2.0,
1.
sftp执行后,接着想执行 ssh,如果不关闭sftp连接就执行ssh,
报错“远程主机强迫关闭了一个现有的连接”
同样道理,ssh执行后,接着想执行 sftp,也需要先关闭ssh的连接,再执行sftp
用ssh连接了192.168.2.5,2.5 是装了Debian系统的主机,
winform要关闭2.5 的ssh连接,最好能确保,关闭ssh连接时,2.5还在正常运行,
如果关闭连接时,2.5早就处于正在重启的状态,就会无法正常关闭 ssh连接。
ssh或sftp,连接了 IP_A ,当换了 IP_B ,这时就要先断开 ssh或sftp的连接,再清空 IP_A 的连接密钥,
跟着才能去连接 IP_B。
具体地说,ConnectionInfo 这个类就保存了 IP_A 的连接密钥,
之前定义了 ConnectionInfo connectionInfo 来保存IP_A 的连接密钥,
执行“ connectionInfo = null; ”,就能清空 IP_A 的连接密钥,
清空后,怎么重新建立 IP_B 的连接?如下代码:
if (connectionInfo == null)
{
// 创建 SSH 连接信息,这里就把 IP_B的ip地址传进来
connectionInfo = new ConnectionInfo(MainForm._StationIP, this.username,
new PasswordAuthenticationMethod(this.username, this.password));
}
if (sftpClient == null)
{
sftpClient = new SftpClient(connectionInfo);
}
我自己的使用经验就是,
即使用ssh一直连接同一个IP ,比如连接 2.5的ip,对2.5执行了很多linux的命令,执行了花了一段时间,重新再对2.5,
进行第二轮相同的linux命令,这个时候也必须先清空 2.5的连接密钥,重新连接2.5,才能进行第二轮相同的linux命令,
如果不清空 2.5的连接密钥,就会在执行第二轮的linux命令,会发生ssh连接异常。
还有其它经验,在一个按钮的响应函数里,我先执行函数A,它会上传一个deb文件到2.5,并且用ssh执行安装deb的指令,
接着执行函数B,它会用sftp,检查2.5的主机,某些重要文件是否存在,最后执行函数C,它会下载2.5主机的某个压缩包,
为了保证这三个函数一定能顺利执行,我执行函数B时,会先清空2.5的连接密钥,执行函数C时,也会先清空2.5的连接密钥,
如果不清空,偶尔就会造成,无法确保三个函数全部执行完。
Beta Was this translation helpful? Give feedback.
All reactions