當前位置: 首頁>>代碼示例>>C#>>正文


C# JSch.getHostKeyRepository方法代碼示例

本文整理匯總了C#中Tamir.SharpSsh.jsch.JSch.getHostKeyRepository方法的典型用法代碼示例。如果您正苦於以下問題:C# JSch.getHostKeyRepository方法的具體用法?C# JSch.getHostKeyRepository怎麽用?C# JSch.getHostKeyRepository使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在Tamir.SharpSsh.jsch.JSch的用法示例。


在下文中一共展示了JSch.getHostKeyRepository方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。

示例1: FtpDropOff

        public virtual void FtpDropOff(string fileDirectory, string fileName, Dictionary<string, string> config)
        {
            ChannelSftp channelSftp;

            var url = config["sftpUrl"];
            var username = config["sftpUsername"];
            var password = config["sftpPassword"];
            var knownHostsFile = config["knownHostsFile"];
            var filePath = fileDirectory + fileName;

            var printxml = config["printxml"] == "true";
            if (printxml)
            {
                Console.WriteLine("Sftp Url: " + url);
                Console.WriteLine("Username: " + username);
                //Console.WriteLine("Password: " + password);
                Console.WriteLine("Known hosts file path: " + knownHostsFile);
            }

            var jsch = new JSch();

            if (printxml)
            {
                // grab the contents fo the knownhosts file and print
                var hostFile = File.ReadAllText(knownHostsFile);
                Console.WriteLine("known host contents: " + hostFile);
            }

            jsch.setKnownHosts(knownHostsFile);

            // setup for diagnostic
            // Get the KnownHosts repository from JSchs
            var hkr = jsch.getHostKeyRepository();
            var hks = hkr.getHostKey();
            HostKey hk;
            if (printxml)
            {
                // Print all knownhosts and keys
                if (hks != null)
                {
                    Console.WriteLine();
                    Console.WriteLine("Host keys in " + hkr.getKnownHostsRepositoryID() + ":");
                    foreach (var t in hks)
                    {
                        hk = t;
                        Console.WriteLine("local HostKey host: <" + hk.getHost() + "> type: <" + hk.getType() + "> fingerprint: <" + hk.getFingerPrint(jsch) + ">");
                    }
                    Console.WriteLine("");
                }
            }

            var session = jsch.getSession(username, url);
            session.setPassword(password);

            try
            {
                session.connect();

                // more diagnostic code for troubleshooting sFTP connection errors
                if (printxml)
                {
                    // Print the host key info of the connected server:
                    hk = session.getHostKey();
                    Console.WriteLine("remote HostKey host: <" + hk.getHost() + "> type: <" + hk.getType() + "> fingerprint: <" + hk.getFingerPrint(jsch) + ">");
                }

                var channel = session.openChannel("sftp");
                channel.connect();
                channelSftp = (ChannelSftp)channel;
            }
            catch (SftpException e)
            {
                throw new LitleOnlineException("Error occured while attempting to establish an SFTP connection", e);
            }
            catch (JSchException e)
            {
                throw new LitleOnlineException("Error occured while attempting to establish an SFTP connection", e);
            }

            try
            {
                if (printxml)
                {
                    Console.WriteLine("Dropping off local file " + filePath + " to inbound/" + fileName + ".prg");
                }
                channelSftp.put(filePath, "inbound/" + fileName + ".prg", ChannelSftp.OVERWRITE);
                if (printxml)
                {
                    Console.WriteLine("File copied - renaming from inbound/" + fileName + ".prg to inbound/" + fileName + ".asc");
                }
                channelSftp.rename("inbound/" + fileName + ".prg", "inbound/" + fileName + ".asc");
            }
            catch (SftpException e)
            {
                throw new LitleOnlineException("Error occured while attempting to upload and save the file to SFTP", e);
            }

            channelSftp.quit();

            session.disconnect();
//.........這裏部分代碼省略.........
開發者ID:LitleCo,項目名稱:litle-sdk-for-dotNet,代碼行數:101,代碼來源:Communications.cs

示例2: RunExample

        public static void RunExample(String[] arg)
        {
            try
            {
                //Get the "known hosts" filename from the user
                Console.WriteLine("Please select your 'known_hosts' from the poup window...");
                String file = InputForm.GetFileFromUser("Choose your known_hosts(ex. ~/.ssh/known_hosts)");
                Console.WriteLine("You chose "+file+".");
                //Create a new JSch instance
                JSch jsch=new JSch();
                //Set the known hosts file
                jsch.setKnownHosts(file);				

                //Get the KnownHosts repository from JSchs
                HostKeyRepository hkr=jsch.getHostKeyRepository();

                //Print all known hosts and keys
                HostKey[] hks=hkr.getHostKey();
                HostKey hk;
                if(hks!=null)
                {
                    Console.WriteLine();
                    Console.WriteLine("Host keys in "+hkr.getKnownHostsRepositoryID()+":");
                    for(int i=0; i<hks.Length; i++)
                    {
                        hk=hks[i];
                        Console.WriteLine(hk.getHost()+" "+
                            hk.getType()+" "+
                            hk.getFingerPrint(jsch));
                    }
                    Console.WriteLine("");
                }

                //Now connect to the remote server...

                //Prompt for username and server host
                Console.WriteLine("Please enter the user and host info at the popup window...");
                String host = InputForm.GetUserInput
                    ("Enter [email protected]",
                    Environment.UserName+"@localhost");
                String user=host.Substring(0, host.IndexOf('@'));
                host=host.Substring(host.IndexOf('@')+1);

                //Create a new SSH session
                Session session=jsch.getSession(user, host, 22);

                // username and password will be given via UserInfo interface.
                UserInfo ui=new MyUserInfo();
                session.setUserInfo(ui);

                //Connect to remote SSH server
                session.connect();

                //Print the host key info
                //of the connected server:
                hk=session.getHostKey();
                Console.WriteLine("HostKey: "+
                    hk.getHost()+" "+
                    hk.getType()+" "+
                    hk.getFingerPrint(jsch));
            
                //Open a new Shell channel on the SSH session
                Channel channel=session.openChannel("shell");

                //Redirect standard I/O to the SSH channel
                channel.setInputStream(Console.OpenStandardInput());
                channel.setOutputStream(Console.OpenStandardOutput());

                //Connect the channel
                channel.connect();

                Console.WriteLine("-- Shell channel is connected using the {0} cipher", 
                    session.getCipher());

                //Wait till channel is closed
                while(!channel.isClosed())
                {
                    System.Threading.Thread.Sleep(500);
                }

                //Disconnect from remote server
                channel.disconnect();
                session.disconnect();
            }
            catch(Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }
開發者ID:TonyZhu2015,項目名稱:sharpssh,代碼行數:89,代碼來源:KnownHosts.cs


注:本文中的Tamir.SharpSsh.jsch.JSch.getHostKeyRepository方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。