[C#] SharpSSH で SSH 公開鍵認証で SFTP

2011-8-30 00:14
このエントリーをはてなブックマークに追加

.NET で SSH を行なうライブラリとして SharpSSH というのがあります。

ネット上をさがすとパスワード認証のサンプルは結構あるのですが、公開鍵・秘密鍵を使った認証方法のサンプルがありません。

NSDN のフォーラムの SFTP Communications in C# using Private – Public key というページに公開鍵暗号で SFTP を行なうコードのサンプルとしてラッパークラスである SftpPlugin というクラスが投稿されていました。

このクラスを利用すると簡単にファイルのアップロードやダウンロードができるのですが、公開鍵のパスフレーズが空の場合でないとうまくいかないことが判りました。

いろいろ調べた結果、password として指定した文字列は公開鍵・秘密鍵を使わないパスワード認証の差異にしか使われず、秘密鍵のパスフレーズとしては利用されていないことが判りました。

そこで、.netでSFTPをしてみる 2 | hippos-lab::blog の記事を参考に SftpPlugin クラスを書き換えたところ、公開鍵認証でパスフレーズを使用する場合にも対応できました。

以下、そのクラスの修正版コードを載せておきます。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Tamir.SharpSsh;

namespace FileTransfer
{
    public class SftpPlugin
    {
        // Private fields
        private int _port;
        private string _privateSshKeyPath;
        private string _password;
        private Tamir.SharpSsh.Sftp _tamirSftp;

        public SftpPlugin(string host, int port, string username, string password, string privateSshKeyPath)
        {
            this._tamirSftp = new Sftp(host, username, password);
            this._port = port;
            this._password = password;
            this._privateSshKeyPath = privateSshKeyPath;
        }

        // Private Methods
        private void Connect()
        {
            try
            {
                // add the private Key if it is specified
                if (!string.IsNullOrEmpty(this._privateSshKeyPath))
                {
                        this._tamirSftp.AddIdentityFile(this._privateSshKeyPath, _password);
                    
                }
                this._tamirSftp.Connect(this._port);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        // Public Methods

        // upload a file to a remote path
        public bool UploadFile(string file, string toRemotePath)
        {
            if (!this._tamirSftp.Connected)
            {
                this.Connect();
            }

            try
            {
                this._tamirSftp.Put(file, toRemotePath);
                return true;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                this._tamirSftp.Close();
            }
        }

        // download a file to a localPath
        public bool DownloadFile(string file, string toLocalPath)
        {
            if (!this._tamirSftp.Connected)
            {
                this.Connect();
            }
            try
            {
                this._tamirSftp.Get(file, toLocalPath);
                return true;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                this._tamirSftp.Close();
            }
        }

        // list the files of a remote location
        public string[] ListFiles(string remotePath)
        {
            if (!this._tamirSftp.Connected)
            {
                this.Connect();
            }

            string[] files = new string[] { };
            try
            {
                files = this._tamirSftp.GetFileList(remotePath).Cast().ToArray();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                this._tamirSftp.Close();
            }
            return files;
        }
    }
}
  • ブックマーク : アクセス: 37,205回
  • カテゴリー : C#
  • キーワード : , ,

コメントはまだありません

No comments yet.

Sorry, the comment form is closed at this time.

33 queries. HTML convert time: 0.074 sec. Powered by WordPress. Valid XHTML
Copyright © 2003-2017 @ futuremix.org ログイン