00001 using System;
00002 using System.IO;
00003 using System.Windows.Forms;
00004
00005
00006
00007
00008
00009
00010
00011 namespace Tamir.SharpSsh.jsch.examples
00012 {
00020 public class KnownHosts
00021 {
00022 public static void Main(String[] arg)
00023 {
00024 try
00025 {
00026
00027 Console.WriteLine("Please select your 'known_hosts' from the poup window...");
00028 String file = InputForm.GetFileFromUser("Choose your known_hosts(ex. ~/.ssh/known_hosts)");
00029 Console.WriteLine("You chose "+file+".");
00030
00031 JSch jsch=new JSch();
00032
00033 jsch.setKnownHosts(file);
00034
00035
00036 HostKeyRepository hkr=jsch.getHostKeyRepository();
00037
00038
00039 HostKey[] hks=hkr.getHostKey();
00040 HostKey hk;
00041 if(hks!=null)
00042 {
00043 Console.WriteLine();
00044 Console.WriteLine("Host keys in "+hkr.getKnownHostsRepositoryID()+":");
00045 for(int i=0; i<hks.Length; i++)
00046 {
00047 hk=hks[i];
00048 Console.WriteLine(hk.getHost()+" "+
00049 hk.getType()+" "+
00050 hk.getFingerPrint(jsch));
00051 }
00052 Console.WriteLine("");
00053 }
00054
00055
00056
00057
00058 Console.WriteLine("Please enter the user and host info at the popup window...");
00059 String host = InputForm.GetUserInput
00060 ("Enter username@hostname",
00061 Environment.UserName+"@localhost");
00062 String user=host.Substring(0, host.IndexOf('@'));
00063 host=host.Substring(host.IndexOf('@')+1);
00064
00065
00066 Session session=jsch.getSession(user, host, 22);
00067
00068
00069 UserInfo ui=new MyUserInfo();
00070 session.setUserInfo(ui);
00071
00072
00073 session.connect();
00074
00075
00076
00077 hk=session.getHostKey();
00078 Console.WriteLine("HostKey: "+
00079 hk.getHost()+" "+
00080 hk.getType()+" "+
00081 hk.getFingerPrint(jsch));
00082
00083
00084 Channel channel=session.openChannel("shell");
00085
00086
00087 channel.setInputStream(Console.OpenStandardInput());
00088 channel.setOutputStream(Console.OpenStandardOutput());
00089
00090
00091 channel.connect();
00092
00093 Console.WriteLine("-- Shell channel is connected using the {0} cipher",
00094 session.getCipher());
00095
00096
00097 while(!channel.isClosed())
00098 {
00099 System.Threading.Thread.Sleep(500);
00100 }
00101
00102
00103 channel.disconnect();
00104 session.disconnect();
00105 }
00106 catch(Exception e)
00107 {
00108 Console.WriteLine(e);
00109 }
00110 }
00111
00115 public class MyUserInfo : UserInfo
00116 {
00120 private String passwd;
00121
00125 public String getPassword(){ return passwd; }
00126
00130 public bool promptYesNo(String str)
00131 {
00132 return InputForm.PromptYesNo(str);
00133 }
00134
00138 public String getPassphrase(){ return null; }
00139
00143 public bool promptPassphrase(String message){ return true; }
00144
00148 public bool promptPassword(String message)
00149 {
00150 passwd=InputForm.GetUserInput(message, true);
00151 return true;
00152 }
00153
00157 public void showMessage(String message)
00158 {
00159 InputForm.ShowMessage(message);
00160 }
00161 }
00162 }
00163 }