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.
stringsshHost=txtSSHHost.Text;intsshPort=Convert.ToInt16(txtSSHPort.Text);stringsshUsername=txtSSHUsername.Text;stringsshPassword=txtSSHPassword.Text;stringlocalHost=txtLocalhost.Text;intlocalPort=Convert.ToInt16(txtLocalPort.Text);intremotePort=Convert.ToInt16(txtDestinationPort.Text);stringERRORS="";if(ERRORS==""){try{varjsch=newJSch();Sessionsession=jsch.getSession(sshUsername,sshHost,sshPort);session.setPassword(sshPassword);//UserInfoui=newMyUserInfo();//session.setUserInfo(ui);session.connect();//unknownhostkeyHostKeyhkey=session.getHostKey();jsch.setKnownHosts(hkey.getHost());session.setPortForwardingL(localPort,localHost,remotePort);}catch(Exceptionex){MessageBox.Show("Unable to tunnel ports:\n\n"+ex.Message+"\n"+ex.StackTrace);}}
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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.
publicpartialclassForm1:Form{boolsshConnected=false;Sessionsession;publicForm1(){InitializeComponent();}privatevoidbtnTunnelPort_Click(objectsender,EventArgse){stringsshHost=txtSSHHost.Text;intsshPort=Convert.ToInt16(txtSSHPort.Text);stringsshUsername=txtSSHUsername.Text;stringsshPassword=txtSSHPassword.Text;stringlocalHost=txtLocalhost.Text;intlocalPort=Convert.ToInt16(txtLocalPort.Text);intremotePort=Convert.ToInt16(txtDestinationPort.Text);stringERRORS="";if(ERRORS==""){try{varjsch=newJSch();session=jsch.getSession(sshUsername,sshHost,sshPort);UserInfoui=newMyUserInfo();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(Exceptionex){MessageBox.Show("Unable to tunnel ports:\n\n"+ex.Message+"\n"+ex.StackTrace);}}}privatevoidForm1_FormClosing(objectsender,FormClosingEventArgse){if(sshConnected){session.disconnect();}}}publicclassMyUserInfo:UserInfo{privateString_passwd;publicStringgetPassword(){return_passwd;}publicboolpromptYesNo(Stringstr){returntrue;}publicStringgetPassphrase(){returnnull;}publicboolpromptPassphrase(Stringmessage){returnfalse;}publicboolpromptPassword(Stringmessage){returntrue;}publicvoidshowMessage(Stringmessage){InputForm.ShowMessage(message);}}
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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.