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