Пример #1
0
        /// <summary>
        /// Obtain the JSch used to create new sessions.
        /// </summary>
        /// <param name="hc">host configuration</param>
        /// <returns>the JSch instance to use.</returns>
        protected JSch getJSch(OpenSshConfig.Host hc)
        {
            if (hc == null)
            {
                throw new System.ArgumentNullException("hc");
            }

            JSch     def          = getDefaultJSch();
            FileInfo identityFile = hc.getIdentityFile();

            if (identityFile == null)
            {
                return(def);
            }

            string identityKey = identityFile.FullName;
            JSch   jsch        = _byIdentityFile[identityKey];

            if (jsch == null)
            {
                jsch = new JSch();
                jsch.setHostKeyRepository(def.getHostKeyRepository());
                jsch.addIdentity(identityKey);
                _byIdentityFile.Add(identityKey, jsch);
            }
            return(jsch);
        }
Пример #2
0
        internal bool SSHConnect()
        {
            try
            {
                channels_ = new Dictionary <int, ChannelSftp>();

                jsch_ = new JSch();
                Hashtable config = new Hashtable();
                config["StrictHostKeyChecking"] = "no";

                if (identity_ != null)
                {
                    jsch_.addIdentity(identity_, passphrase_);
                }

                session_ = jsch_.getSession(user_, host_, port_);
                session_.setConfig(config);
                session_.setUserInfo(new DokanUserInfo(password_, passphrase_));
                session_.setPassword(password_);

                session_.connect();

                return(true);
            }
            catch (Exception e)
            {
                Debug(e.ToString());
                return(false);
            }
        }
Пример #3
0
        /// <summary>
        /// Constructs a new SSH stream.
        /// </summary>
        /// <param name="host">The hostname or IP address of the remote SSH machine</param>
        /// <param name="username">The name of the user connecting to the remote machine</param>
        /// <param name="password">The password of the user connecting to the remote machine</param>
        /// <param name="privateKeyPath">privateKeyPath</param>
        public SshStream(string host, string username, string password, string privateKeyPath)
        {
            this.m_host = host;
            JSch jsch = new JSch();

            m_session = jsch.getSession(username, host, 22);
            if (string.IsNullOrEmpty(password))
            {
                jsch.addIdentity(privateKeyPath);
            }
            else
            {
                m_session.setPassword(password);
            }

            Hashtable config = new Hashtable();

            config.Add("StrictHostKeyChecking", "no");
            m_session.setConfig(config);

            m_session.connect();
            m_channel = (ChannelShell)m_session.openChannel("shell");

            m_in  = m_channel.getInputStream();
            m_out = m_channel.getOutputStream();

            m_channel.connect();
            m_channel.setPtySize(80, 132, 1024, 768);

            Prompt = "\n";
            m_escapeCharPattern = "\\[[0-9;?]*[^0-9;]";
        }
Пример #4
0
        private void OpenSSH()
        {
            string host = "1.119.7.235";
            string user = "******";
            string pwd  = "";
            //SshShell shell = new SshShell(host, user);
            //ssh
            JSch jsch = new JSch();
            //String file = InputForm.GetFileFromUser(@"E:\A工作文件\AVC CleanData\AVC CleanData\id_rsa_2048.2048");
            string file = @"E:\A工作文件\AVC CleanData\AVC CleanData\id_rsa_2048.2048";

            jsch.addIdentity(file);

            Session session = jsch.getSession(user, host);

            UserInfo ui = new MyUserInfo();

            session.setUserInfo(ui);
            try
            {
                session.connect();
                // Channel channel = session.openChannel("shell");
            }
            catch (Exception e)
            {
                Debug.WriteLine(e);
            }
            finally
            {
                session.disconnect();
            }
        }
        public static void Main(String[] arg)
        {
            try
            {
                JSch jsch=new JSch();

                //Get the private key filename from the user
                Console.WriteLine("Please choose your private key file...");
                String file = InputForm.GetFileFromUser("Choose your privatekey(ex. ~/.ssh/id_dsa)");
                Console.WriteLine("You chose "+file+".");

                //Add the identity file to JSch
                jsch.addIdentity(file);

                //Prompt for username and server host
                Console.WriteLine("Please enter the user and host info at the popup window...");
                String host = InputForm.GetUserInput
                    ("Enter username@hostname",
                    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();

                //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();

                //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);
            }
        }
Пример #6
0
        public static void RunExample(String[] arg)
        {
            try
            {
                JSch jsch = new JSch();

                //Get the "known hosts" filename from the user
                Console.WriteLine("Please choose your private key file...");
                String file = InputForm.GetFileFromUser("Choose your privatekey(ex. ~/.ssh/id_dsa)");
                Console.WriteLine("You chose " + file + ".");

                //Add the identity file to JSch
                jsch.addIdentity(file);

                //Prompt for username and server host
                Console.WriteLine("Please enter the user and host info at the popup window...");
                String host = InputForm.GetUserInput
                                  ("Enter username@hostname",
                                  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();

                //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();

                //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);
            }
        }
Пример #7
0
        private void SetSFTPPara(string user, string ip, int port)
        {
            JSch jsch = new JSch();

            jsch.addIdentity(Application.StartupPath + @"/id_rsa");
            m_session = jsch.getSession(user, ip, port);
            MyUserInfo ui = new MyUserInfo();

            m_session.setUserInfo(ui);
        }
Пример #8
0
        public static void RunExample(String[] arg)
        {
            try
            {
                JSch jsch=new JSch();

                OpenFileDialog chooser = new OpenFileDialog();
                chooser.Title ="Choose your privatekey(ex. ~/.ssh/id_dsa)";
                //chooser.setFileHidingEnabled(false);
                DialogResult returnVal = chooser.ShowDialog();
                if(returnVal == DialogResult.OK)
                {
                    Console.WriteLine("You chose "+
                        chooser.FileName+".");
                    jsch.addIdentity(chooser.FileName
                        //			 , "passphrase"
                        );
                }
                else
                {
                    Console.WriteLine("Error getting key file...");
                    return;
                }
                InputForm inForm = new InputForm();
                inForm.Text = "Enter username@hostname";
                inForm.textBox1.Text = Environment.UserName+"@localhost";

                if (inForm.PromptForInput())
                {
                    String host = inForm.textBox1.Text;
                    String user=host.Substring(0, host.IndexOf('@'));
                    host=host.Substring(host.IndexOf('@')+1);

                    Session session=jsch.getSession(user, host, 22);

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

                    Channel channel=session.openChannel("shell");

                    channel.setInputStream(Console.OpenStandardInput());
                    channel.setOutputStream(Console.OpenStandardOutput());

                    channel.connect();
                }
                inForm.Close();

            }
            catch(Exception e)
            {
                Console.WriteLine(e);
            }
        }
Пример #9
0
        /// <summary>
        /// 实例化 SFTP 对象
        /// </summary>
        /// <param name="ip">服务器IP</param>
        /// <param name="user">用户名</param>
        /// <param name="pwd">密码</param>
        /// <param name="port">端口</param>
        /// <param name="privateKey">私钥</param>
        /// <param name="passphrase">私钥口令</param>
        public SFTPHelper(string ip, string user, string pwd, int port = 22, string privateKey = "", string passphrase = "")
        {
            JSch jsch = new JSch();

            jsch.addIdentity(privateKey, passphrase);
            m_session = jsch.getSession(user, ip, port);
            MyUserInfo ui = new MyUserInfo();

            ui.setPassword(pwd);
            m_session.setUserInfo(ui);
            this.Connect();
        }
Пример #10
0
 private static void loadIdentity(JSch sch, FileInfo priv)
 {
     if (!File.Exists(priv.ToString()))
     {
         return;
     }
     try
     {
         sch.addIdentity(Path.GetFullPath(priv.ToString()));
     }
     catch (JSchException)
     {
     }
 }
Пример #11
0
 private static void loadIdentity(JSch sch, FileInfo priv)
 {
     if (!priv.IsFile())
     {
         return;
     }
     try
     {
         sch.addIdentity(priv.FullName);
     }
     catch (JSchException)
     {
         // Instead, pretend the key doesn't exist.
     }
 }
Пример #12
0
        /// <summary>
        /// 通过私钥建立SFTP连接
        /// </summary>
        private void ConnectSFTPByPrivateKey()
        {
            try
            {
                JSch JSchModel = new JSch();
                JSchModel.addIdentity(_privateKeyFile, _passphrase);
                _session = JSchModel.getSession(_userName, _hostName, _port);
                _session.setUserInfo(new SFTPUserInfoModel());

                CreateConnectSFTP();
            }
            catch (Exception e)
            {
                _log.Warn("通过私钥建立SFTP连接失败!", e);
            }
        }
Пример #13
0
        public SFTP(string host, string user)
        {
            string[] arr  = host.Split(':');
            string   ip   = arr[0];
            int      port = 22;

            if (arr.Length > 1)
            {
                port = Int32.Parse(arr[1]);
            }

            JSch jsch = new JSch();

            jsch.addIdentity(Application.StartupPath + @"/id_rsa");
            m_session = jsch.getSession(user, ip, port);

            MyUserInfo ui = new MyUserInfo();

            m_session.setUserInfo(ui);
        }
Пример #14
0
        protected JSch getJSch(OpenSshConfig.Host hc)
        {
            JSch     def          = getDefaultJSch();
            FileInfo identityFile = hc.getIdentityFile();

            if (identityFile == null)
            {
                return(def);
            }

            string identityKey = Path.GetFullPath(identityFile.ToString());
            JSch   jsch        = byIdentityFile[identityKey];

            if (jsch == null)
            {
                jsch = new JSch();
                jsch.setHostKeyRepository(def.getHostKeyRepository());
                jsch.addIdentity(identityKey);
                byIdentityFile.Add(identityKey, jsch);
            }
            return(jsch);
        }
Пример #15
0
 /// <summary>
 /// Adds identity file for publickey user authentication
 /// </summary>
 /// <param name="privateKeyFile">The path to the private key file</param>
 public virtual void AddIdentityFile(string privateKeyFile)
 {
     m_jsch.addIdentity(privateKeyFile);
 }