SshHostKeyConfirmation Event
The event occurs when host key verification during establishing connection failed.
The event handler receives an argument of type SshHostKeyConfirmationEventArgs containing data related to this event. The following SshHostKeyConfirmationEventArgs properties provide information specific to this event.
Property | Description |
---|
Confirmation | Gets or sets the value, indicating whether to continue connecting after this event. |
Host | Gets the name of the host, we are connecting with. |
KeyType | Gets the server public key type. |
Md5Fingerprint | Gets the string representation of the server public key fingerprint, generated with MD5 hash algorithm |
Port | Gets the port, we are connecting with. |
Sha1Fingerprint | Gets the string representation of the server public key fingerprint, generated with SHA1 hash algorithm |
Warning | Gets the warning message string. |
static void Main(string[] args) {
PgSqlConnection conn = new PgSqlConnection("host=server;database=test;user id=postgres;");
conn.ConnectionTimeout = 300;
conn.SshOptions.AuthenticationType = SshAuthenticationType.Password;
conn.SshOptions.Host = "testHost";
conn.SshOptions.Port = 22;
conn.SshOptions.User = "testUser";
conn.SshOptions.Password = "test";
// set host key verification options
conn.SshOptions.StrictHostKeyChecking = true;
conn.SshOptions.HostKey = @"D:\Test\HostKey.pub"; // file with incorrect key
conn.SshHostKeyConfirmation += new SshHostKeyConfirmationHandler(conn_SshHostKeyConfirmation);
try {
conn.Open();
Console.WriteLine("\nConnected successfully");
}
catch (Exception ex) {
Console.WriteLine("\nConnection failed: {0}", ex.Message);
}
Console.ReadKey();
}
static void conn_SshHostKeyConfirmation(object sender, SshHostKeyConfirmationEventArgs e) {
Console.WriteLine("--------- Warning message --------");
Console.WriteLine(e.Warning);
Console.WriteLine("---------Host Key information--------");
Console.WriteLine("Host - {0}", e.Host);
Console.WriteLine("Port - {0}", e.Port);
Console.WriteLine("Key Type - {0}", e.KeyType);
Console.WriteLine("Md5Fingerprint - {0}", e.Md5Fingerprint);
Console.WriteLine("Sha1Fingerprint - {0}", e.Sha1Fingerprint);
Console.WriteLine("-------------------------------------");
Console.Write("Continue?: ");
if (Console.ReadKey().Key == ConsoleKey.Y)
e.Confirmation = SshHostKeyConfirmation.Confirm;
else
e.Confirmation = SshHostKeyConfirmation.NotConfirm;
}
Module Module1
Sub Main()
Dim conn As PgSqlConnection = New PgSqlConnection("host=server;database=test;user id=postgres;")
conn.ConnectionTimeout = 300
' set ssh options
conn.SshOptions.AuthenticationType = SshAuthenticationType.Password
conn.SshOptions.Host = "testHost"
conn.SshOptions.Port = 22
conn.SshOptions.User = "testUser"
conn.SshOptions.Password = "test"
' set host key verification options
conn.SshOptions.StrictHostKeyChecking = True
conn.SshOptions.HostKey = "D:\Test\HostKey.pub"; ' file with incorrect key
AddHandler conn.SshHostKeyConfirmation, AddressOf conn_SshHostKeyConfirmation
Try
conn.Open()
Console.WriteLine(vbCrLf + "Connected successfully")
Catch ex As Exception
Console.WriteLine(vbCrLf + "Connection failed: {0}", ex.Message)
End Try
Console.ReadKey()
End Sub
Sub conn_SshHostKeyConfirmation(ByVal sender As Object, ByVal e As SshHostKeyConfirmationEventArgs)
Console.WriteLine("--------- Warning message --------")
Console.WriteLine(e.Warning)
Console.WriteLine("---------Host Key information--------")
Console.WriteLine("Host - {0}", e.Host)
Console.WriteLine("Port - {0}", e.Port)
Console.WriteLine("Key Type - {0}", e.KeyType)
Console.WriteLine("Md5Fingerprint - {0}", e.Md5Fingerprint)
Console.WriteLine("Sha1Fingerprint - {0}", e.Sha1Fingerprint)
Console.WriteLine("-------------------------------------")
Console.Write("Continue?: ")
If (Console.ReadKey().Key = ConsoleKey.Y) Then
e.Confirmation = SshHostKeyConfirmation.Confirm
Else
e.Confirmation = SshHostKeyConfirmation.NotConfirm
End If
End Sub
End Module