I am attempting to port tunnel, if I use the MyUserInfo class provided it works fine, however I need to create the tunnel pragmatically without requesting credentials. How do I get around the " unknownHostKey" error message. My code is shown below.
string sshHost = txtSSHHost.Text; int sshPort = Convert.ToInt16(txtSSHPort.Text); string sshUsername = txtSSHUsername.Text; string sshPassword = txtSSHPassword.Text; string localHost = txtLocalhost.Text; int localPort = Convert.ToInt16(txtLocalPort.Text); int remotePort = Convert.ToInt16(txtDestinationPort.Text); string ERRORS = ""; if (ERRORS == "") { try { var jsch = new JSch(); Session session = jsch.getSession(sshUsername, sshHost, sshPort); session.setPassword(sshPassword); //UserInfo ui = new MyUserInfo(); //session.setUserInfo(ui); session.connect(); //unknown host key HostKey hkey = session.getHostKey(); jsch.setKnownHosts(hkey.getHost()); session.setPortForwardingL(localPort, localHost, remotePort); } catch (Exception ex) { MessageBox.Show("Unable to tunnel ports:\n\n" + ex.Message + "\n" + ex.StackTrace); } }
After much testing I have managed to crack this, I've posted the code here for future people who may come across the same problem.
public partial class Form1 : Form { bool sshConnected = false; Session session; public Form1() { InitializeComponent(); } private void btnTunnelPort_Click(object sender, EventArgs e) { string sshHost = txtSSHHost.Text; int sshPort = Convert.ToInt16(txtSSHPort.Text); string sshUsername = txtSSHUsername.Text; string sshPassword = txtSSHPassword.Text; string localHost = txtLocalhost.Text; int localPort = Convert.ToInt16(txtLocalPort.Text); int remotePort = Convert.ToInt16(txtDestinationPort.Text); string ERRORS = ""; if (ERRORS == "") { try { var jsch = new JSch(); session = jsch.getSession(sshUsername, sshHost, sshPort); UserInfo ui = new MyUserInfo(); session.setUserInfo(ui); session.setPassword(sshPassword); session.connect(); session.setPortForwardingL(localPort, localHost, remotePort); if (session.isConnected()) { sshConnected = true; MessageBox.Show("Connected"); } else { sshConnected = false; MessageBox.Show("Can't connect"); } } catch (Exception ex) { MessageBox.Show("Unable to tunnel ports:\n\n" + ex.Message + "\n" + ex.StackTrace); } } } private void Form1_FormClosing(object sender, FormClosingEventArgs e) { if (sshConnected) { session.disconnect(); } } } public class MyUserInfo : UserInfo { private String _passwd; public String getPassword() { return _passwd; } public bool promptYesNo(String str) { return true; } public String getPassphrase() { return null; } public bool promptPassphrase(String message) { return false; } public bool promptPassword(String message) { return true; } public void showMessage(String message) { InputForm.ShowMessage(message); } }
Log in to post a comment.
I am attempting to port tunnel, if I use the MyUserInfo class provided it works fine, however I need to create the tunnel pragmatically without requesting credentials. How do I get around the " unknownHostKey" error message. My code is shown below.
After much testing I have managed to crack this, I've posted the code here for future people who may come across the same problem.