Menu

unknownHostKey Error

Help
Anonymous
2012-04-14
2013-04-25
  • Anonymous

    Anonymous - 2012-04-14

    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);
                    }
                }
    
     
  • Anonymous

    Anonymous - 2012-04-14

    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.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.