示例#1
0
文件: ViaHTTP.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);

                string proxy = InputForm.GetUserInput("Enter proxy server", "hostname:port");

                string proxy_host = proxy.Substring(0, proxy.IndexOf(':'));
                int proxy_port = int.Parse(proxy.Substring(proxy.IndexOf(':') + 1));

                session.setProxy(new ProxyHTTP(proxy_host, proxy_port));

                // username and password will be given via UserInfo interface.
                UserInfo ui = new UserInfoViaHTTP();
                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();

                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);
            }
        }
示例#2
0
        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 UserInfoSubsystem();
                session.setUserInfo(ui);

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

                // Get subsystem name from user
                string subsystem = InputForm.GetUserInput("Enter subsystem name", "");

                // Open a new Subsystem channel on the SSH session
                Channel channel = session.openChannel("subsystem");
                ((ChannelSubsystem)channel).setSubsystem(subsystem);
                ((ChannelSubsystem)channel).setPty(true);

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

                // Connect the channel
                channel.connect();

                Console.WriteLine("-- Subsystem '" + subsystem + "' 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);
            }
        }
示例#3
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);
            }
        }
示例#4
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, SharpSsh.SshBase.SSH_TCP_PORT);

                // username and password will be given via UserInfo interface.
                UserInfo ui = new UserInfoPubKey();
                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);
            }
        }
示例#5
0
        public static void RunExample(string[] arg)
        {
            int port;

            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 UserInfoStreamForwarding();
                session.setUserInfo(ui);
                session.Connect();

                // Get from user the remote host and remote host port
                string foo = InputForm.GetUserInput("Enter host and port", "host:port");
                host = foo.Substring(0, foo.IndexOf(':'));
                port = int.Parse(foo.Substring(foo.IndexOf(':') + 1));

                Console.WriteLine("System.{in,out} will be forwarded to " + host + ":" + port + ".");
                Channel channel = session.openChannel("direct-tcpip");
                ((ChannelDirectTCPIP)channel).setInputStream(Console.OpenStandardInput());
                ((ChannelDirectTCPIP)channel).setOutputStream(Console.OpenStandardOutput());
                ((ChannelDirectTCPIP)channel).Host = host;
                ((ChannelDirectTCPIP)channel).Port = port;
                channel.connect();

                while (!channel.IsClosed)
                    System.Threading.Thread.Sleep(500);

                channel.disconnect();
                session.disconnect();
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }
示例#6
0
        public static void RunExample(string[] arg)
        {
            //int port;

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

                // Get from user the remote port, local host and local host port
                string foo = InputForm.GetUserInput("Enter -R port:host:hostport", "port:host:hostport");
                int rport = int.Parse(foo.Substring(0, foo.IndexOf(':')));
                foo = foo.Substring(foo.IndexOf(':') + 1);
                string lhost = foo.Substring(0, foo.IndexOf(':'));
                int lport = int.Parse(foo.Substring(foo.IndexOf(':') + 1));

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

                Console.WriteLine(host + ":" + rport + " -> " + lhost + ":" + lport);

                // Set port forwarding on the opened session
                session.setPortForwardingR(rport, lhost, lport);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }
示例#7
0
文件: ScpTo.cs 项目: x893/SharpSSH
        public static void RunExample(string[] arg)
        {
            if (arg.Length != 2)
            {
                Console.WriteLine("usage: java ScpTo file1 user@remotehost:file2");
                Environment.Exit(-1);
            }

            try
            {
                string lfile = arg[0];
                string user = arg[1].Substring(0, arg[1].IndexOf('@'));
                arg[1] = arg[1].Substring(arg[1].IndexOf('@') + 1);

                string host = arg[1].Substring(0, arg[1].IndexOf(':'));
                string rfile = arg[1].Substring(arg[1].IndexOf(':') + 1);

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

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

                // exec 'scp -t rfile' remotely
                string command = "scp -p -t " + rfile;
                Channel channel = session.openChannel("exec");
                ((ChannelExec)channel).setCommand(command);

                // get I/O streams for remote scp
                Stream outs = channel.getOutputStream();
                Stream ins = channel.getInputStream();

                channel.connect();

                byte[] tmp = new byte[1];
                if (checkAck(ins) != 0)
                    Environment.Exit(0);

                // send "C0644 filesize filename", where filename should not include '/'

                int filesize = (int)(new FileInfo(lfile)).Length;
                command = "C0644 " + filesize + " ";
                if (lfile.LastIndexOf('/') > 0)
                    command += lfile.Substring(lfile.LastIndexOf('/') + 1);
                else
                    command += lfile;

                command += "\n";
                byte[] buff = Util.getBytes(command);
                outs.Write(buff, 0, buff.Length);
                outs.Flush();

                if (checkAck(ins) != 0)
                    Environment.Exit(0);

                // send a content of lfile
                FileStream fis = File.OpenRead(lfile);
                byte[] buf = new byte[1024];
                while (true)
                {
                    int len = fis.Read(buf, 0, buf.Length);
                    if (len <= 0) break;
                    outs.Write(buf, 0, len); outs.Flush();
                    Console.Write("#");
                }

                // send '\0'
                buf[0] = 0; outs.Write(buf, 0, 1); outs.Flush();
                Console.Write(".");

                if (checkAck(ins) != 0)
                    Environment.Exit(0);

                Console.WriteLine("OK");
                Environment.Exit(0);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }
示例#8
0
        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.Host + " " +
                            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 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 UserInfoKnownHosts();
                session.setUserInfo(ui);

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

                // Print the host key info
                // of the connected server:
                hk = session.HostKey;
                Console.WriteLine("HostKey: " +
                    hk.Host + " " +
                    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.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);
            }
        }
示例#9
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);
            }
        }
示例#10
0
文件: ScpFrom.cs 项目: x893/SharpSSH
        public static void RunExample(string[] arg)
        {
            if (arg.Length != 2)
            {
                Console.WriteLine("usage: java ScpFrom user@remotehost:file1 file2");
                return;
            }

            try
            {
                string user = arg[0].Substring(0, arg[0].IndexOf('@'));
                arg[0] = arg[0].Substring(arg[0].IndexOf('@') + 1);
                string host = arg[0].Substring(0, arg[0].IndexOf(':'));
                string rfile = arg[0].Substring(arg[0].IndexOf(':') + 1);
                string lfile = arg[1];

                string prefix = null;
                if (Directory.Exists(lfile))
                    prefix = lfile + Path.DirectorySeparatorChar;

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

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

                // exec 'scp -f rfile' remotely
                string command = "scp -f " + rfile;
                Channel channel = session.openChannel("exec");
                ((ChannelExec)channel).setCommand(command);

                // get I/O streams for remote scp
                Stream outs = channel.getOutputStream();
                Stream ins = channel.getInputStream();

                channel.connect();

                byte[] buf = new byte[1024];

                // send '\0'
                buf[0] = 0; outs.Write(buf, 0, 1); outs.Flush();

                while (true)
                {
                    int c = checkAck(ins);
                    if (c != 'C')
                        break;

                    // read '0644 '
                    ins.Read(buf, 0, 5);

                    int filesize = 0;
                    while (true)
                    {
                        ins.Read(buf, 0, 1);
                        if (buf[0] == ' ')
                            break;
                        filesize = filesize * 10 + (buf[0] - '0');
                    }

                    string file = null;
                    for (int i = 0; ; i++)
                    {
                        ins.Read(buf, i, 1);
                        if (buf[i] == (byte)0x0a)
                        {
                            file = Util.getString(buf, 0, i);
                            break;
                        }
                    }

                    //Console.WriteLine("filesize="+filesize+", file="+file);

                    // send '\0'
                    buf[0] = 0; outs.Write(buf, 0, 1); outs.Flush();

                    // read a content of lfile
                    FileStream fos = File.OpenWrite(prefix == null ? lfile : prefix + file);
                    int foo;
                    while (true)
                    {
                        if (buf.Length < filesize)
                            foo = buf.Length;
                        else
                            foo = filesize;
                        ins.Read(buf, 0, foo);
                        fos.Write(buf, 0, foo);
                        filesize -= foo;
                        if (filesize == 0)
                            break;
                    }
                    fos.Close();

                    byte[] tmp = new byte[1];

                    if (checkAck(ins) != 0)
                        Environment.Exit(0);

                    // send '\0'
                    buf[0] = 0; outs.Write(buf, 0, 1); outs.Flush();
                }
                Environment.Exit(0);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }