示例#1
0
文件: AES.cs 项目: x893/SharpSSH
        public static void RunExample(string[] arg)
        {
            try
            {
                //Create a new JSch instance
                JSch jsch = new JSch();

                //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, SharpSsh.SshBase.SSH_TCP_PORT);

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

                //Add AES128 as default cipher in the session config store
                StringDictionary config = new StringDictionary(2);
                config.Add("cipher.s2c", "aes128-cbc,3des-cbc");
                config.Add("cipher.c2s", "aes128-cbc,3des-cbc");
                session.setConfig(config);

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

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

                //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);
            }
        }
示例#2
0
文件: JSch.cs 项目: x893/SharpSSH
        public static void Init()
        {
            m_config = new StringDictionary(30);

            //  config.Add("kex", "diffie-hellman-group-exchange-sha1");
            m_config.Add("kex", "diffie-hellman-group1-sha1,diffie-hellman-group-exchange-sha1");
            m_config.Add("server_host_key", "ssh-rsa,ssh-dss");
            //	config.Add("server_host_key", "ssh-dss,ssh-rsa");
            //	config.Add("cipher.s2c", "3des-cbc,blowfish-cbc");
            //	config.Add("cipher.c2s", "3des-cbc,blowfish-cbc");

            m_config.Add("cipher.s2c", "3des-cbc,aes128-cbc");
            m_config.Add("cipher.c2s", "3des-cbc,aes128-cbc");

            //	config.Add("mac.s2c", "hmac-md5,hmac-sha1,hmac-sha1-96,hmac-md5-96");
            //	config.Add("mac.c2s", "hmac-md5,hmac-sha1,hmac-sha1-96,hmac-md5-96");
            m_config.Add("mac.s2c", "hmac-md5,hmac-sha1");
            m_config.Add("mac.c2s", "hmac-md5,hmac-sha1");
            m_config.Add("compression.s2c", "none");
            m_config.Add("compression.c2s", "none");
            m_config.Add("lang.s2c", "");
            m_config.Add("lang.c2s", "");

            m_config.Add("diffie-hellman-group-exchange-sha1", "SharpSsh.jsch.DHGEX");
            m_config.Add("diffie-hellman-group1-sha1", "SharpSsh.jsch.DHG1");

            m_config.Add("dh", "SharpSsh.jsch.jce.DH");
            m_config.Add("3des-cbc", "SharpSsh.jsch.jce.TripleDESCBC");
            //config.Add("blowfish-cbc",  "SharpSsh.jsch.jce.BlowfishCBC");
            m_config.Add("hmac-sha1", "SharpSsh.jsch.jce.HMACSHA1");
            m_config.Add("hmac-sha1-96", "SharpSsh.jsch.jce.HMACSHA196");
            m_config.Add("hmac-md5", "SharpSsh.jsch.jce.HMACMD5");
            m_config.Add("hmac-md5-96", "SharpSsh.jsch.jce.HMACMD596");
            m_config.Add("sha-1", "SharpSsh.jsch.jce.SHA1");
            m_config.Add("md5", "SharpSsh.jsch.jce.MD5");
            m_config.Add("signature.dss", "SharpSsh.jsch.jce.SignatureDSA");
            m_config.Add("signature.rsa", "SharpSsh.jsch.jce.SignatureRSA");
            m_config.Add("keypairgen.dsa", "SharpSsh.jsch.jce.KeyPairGenDSA");
            m_config.Add("keypairgen.rsa", "SharpSsh.jsch.jce.KeyPairGenRSA");
            m_config.Add("random", "SharpSsh.jsch.jce.Random");

            m_config.Add("aes128-cbc", "SharpSsh.jsch.jce.AES128CBC");

            //config.Add("zlib", "com.jcraft.jsch.jcraft.Compression");

            m_config.Add("StrictHostKeyChecking", "ask");
        }
示例#3
0
文件: Session.cs 项目: x893/SharpSSH
 // public void setX11Host(string host) { ChannelX11.setHost(host); }
 // public void setX11Port(int port) { ChannelX11.setPort(port); }
 // public void setX11Cookie(string cookie) { ChannelX11.setCookie(cookie); }
 public void setConfig(StringDictionary foo)
 {
     if (m_config == null)
         m_config = new StringDictionary();
     foreach (string key in foo.Keys)
         m_config.Add(key, foo[key]);
 }
示例#4
0
文件: Sftp.cs 项目: x893/SharpSSH
        public static void RunExample(string[] arg)
        {
            try
            {
                JSch jsch = new JSch();

                InputForm inForm = new InputForm();
                inForm.Text = "Enter username@hostname";
                string host = null, user;
                while(true)
                {
                    inForm.SetText("");

                    if (!inForm.PromptForInput())
                    {
                        Console.WriteLine("Cancelled");
                        return;
                    }
                    user = inForm.GetText();
                    if (!string.IsNullOrEmpty(user) && user.IndexOf('@') >= 0 && user.IndexOf('@') < user.Length - 1)
                    {
                        host = user.Substring(user.IndexOf('@') + 1);
                        break;
                    }
                }

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

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

                session.Connect();

                Channel channel = session.openChannel("sftp");
                channel.connect();
                ChannelSftp c = (ChannelSftp)channel;

                Stream ins = Console.OpenStandardInput();
                TextWriter outs = Console.Out;

                List<string> cmds = new List<string>();
                byte[] buf = new byte[1024];
                int i;
                string str;
                int level = 0;

                while (true)
                {
                    outs.Write("sftp> ");
                    cmds.Clear();
                    i = ins.Read(buf, 0, 1024);
                    if (i <= 0)
                        break;

                    i--;
                    if (i > 0 && buf[i - 1] == 0x0d)
                        i--;
                    int s = 0;
                    for (int ii = 0; ii < i; ii++)
                    {
                        if (buf[ii] == ' ')
                        {
                            if (ii - s > 0)
                                cmds.Add(Util.getString(buf, s, ii - s));
                            while (ii < i)
                            {
                                if (buf[ii] != ' ')
                                    break;
                                ii++;
                            }
                            s = ii;
                        }
                    }

                    if (s < i)
                        cmds.Add(Util.getString(buf, s, i - s));

                    if (cmds.Count == 0)
                        continue;

                    string cmd = cmds[0];
                    if (cmd.Equals("quit"))
                    {
                        c.quit();
                        break;
                    }

                    if (cmd.Equals("exit"))
                    {
                        c.exit();
                        break;
                    }

                    if (cmd.Equals("rekey"))
                    {
                        session.rekey();
                        continue;
                    }

                    if (cmd.Equals("compression"))
                    {
                        if (cmds.Count < 2)
                        {
                            outs.WriteLine("compression level: " + level);
                            continue;
                        }
                        try
                        {
                            level = int.Parse((String)cmds[1]);
                            StringDictionary config = new StringDictionary(2);
                            if (level == 0)
                            {
                                config.Add("compression.s2c", "none");
                                config.Add("compression.c2s", "none");
                            }
                            else
                            {
                                config.Add("compression.s2c", "zlib,none");
                                config.Add("compression.c2s", "zlib,none");
                            }
                            session.setConfig(config);
                        }
                        catch { }
                        continue;
                    }
                    if (cmd.Equals("cd") || cmd.Equals("lcd"))
                    {
                        if (cmds.Count < 2) continue;
                        string path = (String)cmds[1];
                        try
                        {
                            if (cmd.Equals("cd")) c.cd(path);
                            else c.lcd(path);
                        }
                        catch (SftpException e)
                        {
                            Console.WriteLine(e.ToString());
                        }
                        continue;
                    }
                    if (cmd.Equals("rm") || cmd.Equals("rmdir") || cmd.Equals("mkdir"))
                    {
                        if (cmds.Count < 2) continue;
                        string path = (String)cmds[1];
                        try
                        {
                            if (cmd.Equals("rm")) c.rm(path);
                            else if (cmd.Equals("rmdir")) c.rmdir(path);
                            else c.mkdir(path);
                        }
                        catch (SftpException e)
                        {
                            Console.WriteLine(e.ToString());
                        }
                        continue;
                    }
                    if (cmd.Equals("chgrp") || cmd.Equals("chown") || cmd.Equals("chmod"))
                    {
                        if (cmds.Count != 3) continue;
                        string path = (String)cmds[2];
                        int foo = 0;
                        if (cmd.Equals("chmod"))
                        {
                            byte[] bar = Util.getBytes((String)cmds[1]);
                            int k;
                            for (int j = 0; j < bar.Length; j++)
                            {
                                k = bar[j];
                                if (k < '0' || k > '7') { foo = -1; break; }
                                foo <<= 3;
                                foo |= (k - '0');
                            }
                            if (foo == -1) continue;
                        }
                        else
                        {
                            try { foo = int.Parse((String)cmds[1]); }
                            catch { }//(Exception e){continue;}
                        }
                        try
                        {
                            if (cmd.Equals("chgrp")) { c.chgrp(foo, path); }
                            else if (cmd.Equals("chown")) { c.chown(foo, path); }
                            else if (cmd.Equals("chmod")) { c.chmod(foo, path); }
                        }
                        catch (SftpException e)
                        {
                            Console.WriteLine(e.ToString());
                        }
                        continue;
                    }
                    if (cmd.Equals("pwd") || cmd.Equals("lpwd"))
                    {
                        str = (cmd.Equals("pwd") ? "Remote" : "Local");
                        str += " working directory: ";
                        if (cmd.Equals("pwd"))
                            str += c.Pwd;
                        else
                            str += c.LPwd;
                        outs.WriteLine(str);
                        continue;
                    }
                    if (cmd.Equals("ls") || cmd.Equals("dir"))
                    {
                        string path = ".";
                        if (cmds.Count == 2) path = (String)cmds[1];
                        try
                        {
                            ArrayList vv = c.ls(path);
                            if (vv != null)
                            {
                                for (int ii = 0; ii < vv.Count; ii++)
                                {
                                    object obj = vv[ii];
                                    if (obj is ChannelSftp.LsEntry)
                                        outs.WriteLine(vv[ii]);
                                }
                            }
                        }
                        catch (SftpException e)
                        {
                            Console.WriteLine(e.ToString());
                        }
                        continue;
                    }

                    if (cmd.Equals("lls") || cmd.Equals("ldir"))
                    {
                        string path = ".";
                        if (cmds.Count == 2) path = (String)cmds[1];
                        try
                        {
                            if (!File.Exists(path))
                            {
                                outs.WriteLine(path + ": No such file or directory");
                                continue;
                            }
                            if (Directory.Exists(path))
                            {
                                string[] list = Directory.GetDirectories(path);
                                for (int ii = 0; ii < list.Length; ii++)
                                {
                                    outs.WriteLine(list[ii]);
                                }
                                continue;
                            }
                            outs.WriteLine(path);
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine(e);
                        }
                        continue;
                    }

                    if (cmd.Equals("get")
                    || cmd.Equals("get-resume")
                    || cmd.Equals("get-append")
                    || cmd.Equals("put")
                    || cmd.Equals("put-resume")
                    || cmd.Equals("put-append")
                        )
                    {
                        if (cmds.Count != 2 && cmds.Count != 3)
                            continue;
                        string p1 = (string)cmds[1];
                        string p2 = ".";
                        if (cmds.Count == 3) p2 = (String)cmds[2];
                        try
                        {
                            SftpProgressMonitor monitor = new MyProgressMonitor();
                            if (cmd.StartsWith("get"))
                            {
                                ChannelSftp.ChannelSftpModes mode = ChannelSftp.ChannelSftpModes.OVERWRITE;
                                if (cmd.Equals("get-resume"))
                                    mode = ChannelSftp.ChannelSftpModes.RESUME;
                                else if (cmd.Equals("get-append"))
                                    mode = ChannelSftp.ChannelSftpModes.APPEND;
                                c.get(p1, p2, monitor, mode);
                            }
                            else
                            {
                                ChannelSftp.ChannelSftpModes mode = ChannelSftp.ChannelSftpModes.OVERWRITE;
                                if (cmd.Equals("put-resume"))
                                    mode = ChannelSftp.ChannelSftpModes.RESUME;
                                else if (cmd.Equals("put-append"))
                                    mode = ChannelSftp.ChannelSftpModes.APPEND;
                                c.put(p1, p2, monitor, mode);
                            }
                        }
                        catch (SftpException e)
                        {
                            Console.WriteLine(e.ToString());
                        }
                        continue;
                    }
                    if (cmd.Equals("ln")
                    || cmd.Equals("symlink")
                    || cmd.Equals("rename")
                        )
                    {
                        if (cmds.Count != 3)
                            continue;
                        string p1 = (string)cmds[1];
                        string p2 = (string)cmds[2];
                        try
                        {
                            if (cmd.Equals("rename")) c.rename(p1, p2);
                            else c.symlink(p1, p2);
                        }
                        catch (SftpException e)
                        {
                            Console.WriteLine(e.ToString());
                        }
                        continue;
                    }
                    if (cmd.Equals("stat") || cmd.Equals("lstat"))
                    {
                        if (cmds.Count != 2) continue;
                        string p1 = (String)cmds[1];
                        SftpATTRS attrs = null;
                        try
                        {
                            if (cmd.Equals("stat")) attrs = c.stat(p1);
                            else attrs = c.lstat(p1);
                        }
                        catch (SftpException e)
                        {
                            Console.WriteLine(e.ToString());
                        }
                        if (attrs != null)
                            outs.WriteLine(attrs);
                        continue;
                    }
                    if (cmd.Equals("version"))
                    {
                        outs.WriteLine("SFTP protocol version " + c.Version);
                        continue;
                    }
                    if (cmd.Equals("help") || cmd.Equals("?"))
                    {
                        outs.WriteLine(help);
                        continue;
                    }
                    outs.WriteLine("unimplemented command: " + cmd);
                }
                session.disconnect();
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }
示例#5
0
文件: SshBase.cs 项目: x893/SharpSSH
 protected virtual void ConnectSession(int port)
 {
     m_session = m_jsch.getSession(m_user, m_host, port);
     if (Password != null)
         m_session.setUserInfo(new KeyboardInteractiveUserInfo(Password));
     StringDictionary config = new StringDictionary();
     config.Add("StrictHostKeyChecking", "no");
     m_session.setConfig(config);
     m_session.Connect();
 }