// This method uses SCP protocol.
        private static void ScpCommand(string[] args)
        {
            ScpParameter scp_param = new ScpParameter();
            #if true //OKAJIMA
            #if true
            scp_param.Direction = SCPCopyDirection.LocalToRemote;
            scp_param.RemoteFilename = "test.txt";
            scp_param.LocalSource = new ScpLocalSource("C:\\IOPort\\test.txt");
            #else
            scp_param.Direction = SCPCopyDirection.RemoteToLocal;
            scp_param.RemoteFilename = "hiro.jpg";
            scp_param.LocalSource = new ScpLocalSource("C:\\IOPort\\hiro.jpg");
            #endif
            //string host_ip;
            //string username, password;

            SSHConnectionParameter f = new SSHConnectionParameter();
            f.EventTracer = new Tracer(); //to receive detailed events, set ISSHEventTracer
            Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            f.Protocol = SSHProtocol.SSH2;
            f.UserName = "******";          //<--!!! if you try this sample, edit these values for your environment!
            f.Password = "******";              //<--!!!
            f.AuthenticationType = AuthenticationType.Password;
            s.Connect(new IPEndPoint(IPAddress.Parse("172.22.1.2"), 22)); //22 is the default SSH port

            SSHConnection conn = SSHConnection.Connect(f, new Reader(), s);
            conn.AutoDisconnect = false; //auto close is disabled for multiple scp operations
            conn.ExecuteSCP(scp_param);

            conn.Disconnect("");
            #endif
            #if HIRATA
            // check argument
            if (args.Length != 6) {
                Console.WriteLine("Usage: ScpCommand <server:port> <username> <password> to|from <src_file> <dst_file>");
                Environment.Exit(0);
            }

            // test pattern
            int test = 103;

            if (test == 0) {
                args[0] = "192.168.1.2";
                args[1] = "yutaka";
                args[2] = "yutaka";
                args[3] = "to";
                args[4] = "hoge6.txt";
                args[5] = "hoge6s.txt";
            }
            if (test == 1) {
                args[0] = "192.168.1.2";
                args[1] = "yutaka";
                args[2] = "yutaka";
                args[3] = "to";
                args[4] = "hoge28k.txt";
                args[5] = "hoge28ks.txt";
            }
            if (test == 2) {
                args[0] = "192.168.1.2";
                args[1] = "yutaka";
                args[2] = "yutaka";
                args[3] = "to";
                args[4] = null;   // use Local Memory
                args[5] = "hogeLM.txt";
            }
            if (test == 3) { // big file transfer
                args[0] = "192.168.1.2";
                args[1] = "yutaka";
                args[2] = "yutaka";
                args[3] = "to";
                args[4] = "bigfile.bin";
                args[5] = "bigfile.bin";
            }

            if (test == 100) {
                args[0] = "192.168.1.2";
                args[1] = "yutaka";
                args[2] = "yutaka";
                args[3] = "from";
                args[4] = "hoge6.txt";
                args[5] = "hoge6c.txt";
            }
            if (test == 101) {
                args[0] = "192.168.1.2";
                args[1] = "yutaka";
                args[2] = "yutaka";
                args[3] = "from";
                args[4] = "hoge28k.txt";
                args[5] = "hoge28kc.txt";
            }
            if (test == 102) {
                args[0] = "192.168.1.2";
                args[1] = "yutaka";
                args[2] = "yutaka";
                args[3] = "from";
                args[4] = "hoge6.txt";
                //args[4] = "hoge28k.txt";
                args[5] = null;   // use Local Memory
            }
            if (test == 103) {  // big file transfer
                args[0] = "192.168.1.2";
                args[1] = "yutaka";
                args[2] = "yutaka";
                args[3] = "from";
                args[4] = "bigfile.bin";
                args[5] = "bigfilec.bin";
            }

            host_ip = args[0];
            username = args[1];
            password = args[2];

            // setup SCP parameter
            if (args[3] == "to") {  // Local to Remote
                if (args[5] == null || args[5] == "") {
                    param.RemoteFilename = null;
                }
                else {
                    param.RemoteFilename = args[5];  // remote file
                }

                // �]�����̎w��i���[�J���t�@�C������у��[�J����������I��j
                if (args[4] != null) {
                    // ���[�J���t�@�C���̓]��
                    param.LocalSource = args[4]; // src file

                }
                else {
                    // �I�����C���������̓]��
                    //param.IoStream = new MemoryStream(256);
                    param.IoStream = new MemoryStream(8192);
                    for (int i = 0; i < 8192; i++) {
                        param.IoStream.WriteByte((byte)i);
                    }
                    param.IoStream.Seek(0, SeekOrigin.Begin);
                }
                param.Direction = true;

                param.Permission = "0666";

            }
            else {  // Remote to Local
                param.RemoteFilename = args[4]; // remote file

                // �]�����̎w��i���[�J���t�@�C������у��[�J����������I��j
                if (args[5] != null) {
                    // ���[�J���t�@�C���̓]��
                    param.LocalSource = args[5];
                }
                else {
                    // �I�����C���������̓]��
                    param.IoStream = null;
                }
                param.Direction = false;
            }

            // connect to server with SSH protocol
            SSHConnectionParameter f = new SSHConnectionParameter();
            f.EventTracer = new Tracer(); //to receive detailed events, set ISSHEventTracer
            Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            f.Protocol = SSHProtocol.SSH2; //this sample works on both SSH1 and SSH2
            f.UserName = username;          //<--!!! if you try this sample, edit these values for your environment!
            f.Password = password;              //<--!!!
            s.Connect(new IPEndPoint(IPAddress.Parse(host_ip), 22)); //22 is the default SSH port

            f.AuthenticationType = AuthenticationType.Password;
            //NOTE: if you use public-key authentication, follow this sample instead of the line above:
            //  f.AuthenticationType = AuthenticationType.PublicKey;
            //  f.IdentityFile = "privatekey.bin";
            //  f.Password = "******";

            //former algorithm is given priority in the algorithm negotiation
            f.PreferableHostKeyAlgorithms = new PublicKeyAlgorithm[] { PublicKeyAlgorithm.DSA };
            f.PreferableCipherAlgorithms = new CipherAlgorithm[] { CipherAlgorithm.Blowfish, CipherAlgorithm.TripleDES };

            //this option is ignored with SSH1
            f.WindowSize = 0x1000; //NG: ERROR: MAC mismatch
            //f.WindowSize = 0x800; //NG
            //f.WindowSize = 0x30000; //NG
            //f.WindowSize = 0x400; //OK
            //f.CheckMACError = false; //NG: unexpected channel pt=SSH_MSG_CHANNEL_DATA local_channel=33243

            /* USER OPTION */
            //param.CancelTransfer = true;  // cancel flag
            param.ProgressCallback = delegate() {
                Debug.Write("*");
            };   // callback function

            if (SSHConnection.SCPExecute(param, f, s)) {
                Debug.WriteLine("scp success!");

                if (param.Direction == false) {
                    if (param.IoStream != null) {
                        Debug.Write("IO Stream: ");
                        for (int i = 0; i < param.IoStream.Length; i++) {
                            byte b = (byte)param.IoStream.ReadByte();
                            Debug.Write(b.ToString("x2") + " ");
                        }
                        Debug.WriteLine("");
                    }
                }
            }
            else {
                Debug.WriteLine("scp failure: " + param.ErrorMessage);
            }

            #endif
        }
Example #2
0
        // This method uses SCP protocol.
        private static void ScpCommand(string[] args)
        {
            ScpParameter scp_param = new ScpParameter();

#if true //OKAJIMA
#if true
            scp_param.Direction      = SCPCopyDirection.LocalToRemote;
            scp_param.RemoteFilename = "test.txt";
            scp_param.LocalSource    = new ScpLocalSource("C:\\IOPort\\test.txt");
#else
            scp_param.Direction      = SCPCopyDirection.RemoteToLocal;
            scp_param.RemoteFilename = "hiro.jpg";
            scp_param.LocalSource    = new ScpLocalSource("C:\\IOPort\\hiro.jpg");
#endif
            //string host_ip;
            //string username, password;

            SSHConnectionParameter f = new SSHConnectionParameter();
            f.EventTracer = new Tracer();             //to receive detailed events, set ISSHEventTracer
            Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            f.Protocol           = SSHProtocol.SSH2;
            f.UserName           = "******";                                //<--!!! if you try this sample, edit these values for your environment!
            f.Password           = "******";                             //<--!!!
            f.AuthenticationType = AuthenticationType.Password;
            s.Connect(new IPEndPoint(IPAddress.Parse("172.22.1.2"), 22)); //22 is the default SSH port

            SSHConnection conn = SSHConnection.Connect(f, new Reader(), s);
            conn.AutoDisconnect = false; //auto close is disabled for multiple scp operations
            conn.ExecuteSCP(scp_param);

            conn.Disconnect("");
#endif
#if HIRATA
            // check argument
            if (args.Length != 6)
            {
                Console.WriteLine("Usage: ScpCommand <server:port> <username> <password> to|from <src_file> <dst_file>");
                Environment.Exit(0);
            }

            // test pattern
            int test = 103;

            if (test == 0)
            {
                args[0] = "192.168.1.2";
                args[1] = "yutaka";
                args[2] = "yutaka";
                args[3] = "to";
                args[4] = "hoge6.txt";
                args[5] = "hoge6s.txt";
            }
            if (test == 1)
            {
                args[0] = "192.168.1.2";
                args[1] = "yutaka";
                args[2] = "yutaka";
                args[3] = "to";
                args[4] = "hoge28k.txt";
                args[5] = "hoge28ks.txt";
            }
            if (test == 2)
            {
                args[0] = "192.168.1.2";
                args[1] = "yutaka";
                args[2] = "yutaka";
                args[3] = "to";
                args[4] = null;   // use Local Memory
                args[5] = "hogeLM.txt";
            }
            if (test == 3)   // big file transfer
            {
                args[0] = "192.168.1.2";
                args[1] = "yutaka";
                args[2] = "yutaka";
                args[3] = "to";
                args[4] = "bigfile.bin";
                args[5] = "bigfile.bin";
            }

            if (test == 100)
            {
                args[0] = "192.168.1.2";
                args[1] = "yutaka";
                args[2] = "yutaka";
                args[3] = "from";
                args[4] = "hoge6.txt";
                args[5] = "hoge6c.txt";
            }
            if (test == 101)
            {
                args[0] = "192.168.1.2";
                args[1] = "yutaka";
                args[2] = "yutaka";
                args[3] = "from";
                args[4] = "hoge28k.txt";
                args[5] = "hoge28kc.txt";
            }
            if (test == 102)
            {
                args[0] = "192.168.1.2";
                args[1] = "yutaka";
                args[2] = "yutaka";
                args[3] = "from";
                args[4] = "hoge6.txt";
                //args[4] = "hoge28k.txt";
                args[5] = null; // use Local Memory
            }
            if (test == 103)    // big file transfer
            {
                args[0] = "192.168.1.2";
                args[1] = "yutaka";
                args[2] = "yutaka";
                args[3] = "from";
                args[4] = "bigfile.bin";
                args[5] = "bigfilec.bin";
            }

            host_ip  = args[0];
            username = args[1];
            password = args[2];

            // setup SCP parameter
            if (args[3] == "to")                // Local to Remote
            {
                if (args[5] == null || args[5] == "")
                {
                    param.RemoteFilename = null;
                }
                else
                {
                    param.RemoteFilename = args[5];  // remote file
                }

                // 転送元の指定(ローカルファイルおよびローカルメモリを選択)
                if (args[4] != null)
                {
                    // ローカルファイルの転送
                    param.LocalSource = args[4]; // src file
                }
                else
                {
                    // オンラインメモリの転送
                    //param.IoStream = new MemoryStream(256);
                    param.IoStream = new MemoryStream(8192);
                    for (int i = 0; i < 8192; i++)
                    {
                        param.IoStream.WriteByte((byte)i);
                    }
                    param.IoStream.Seek(0, SeekOrigin.Begin);
                }
                param.Direction = true;

                param.Permission = "0666";
            }
            else                                // Remote to Local
            {
                param.RemoteFilename = args[4]; // remote file

                // 転送元の指定(ローカルファイルおよびローカルメモリを選択)
                if (args[5] != null)
                {
                    // ローカルファイルの転送
                    param.LocalSource = args[5];
                }
                else
                {
                    // オンラインメモリの転送
                    param.IoStream = null;
                }
                param.Direction = false;
            }

            // connect to server with SSH protocol
            SSHConnectionParameter f = new SSHConnectionParameter();
            f.EventTracer = new Tracer();             //to receive detailed events, set ISSHEventTracer
            Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            f.Protocol = SSHProtocol.SSH2;                           //this sample works on both SSH1 and SSH2
            f.UserName = username;                                   //<--!!! if you try this sample, edit these values for your environment!
            f.Password = password;                                   //<--!!!
            s.Connect(new IPEndPoint(IPAddress.Parse(host_ip), 22)); //22 is the default SSH port

            f.AuthenticationType = AuthenticationType.Password;
            //NOTE: if you use public-key authentication, follow this sample instead of the line above:
            //  f.AuthenticationType = AuthenticationType.PublicKey;
            //  f.IdentityFile = "privatekey.bin";
            //  f.Password = "******";

            //former algorithm is given priority in the algorithm negotiation
            f.PreferableHostKeyAlgorithms = new PublicKeyAlgorithm[] { PublicKeyAlgorithm.DSA };
            f.PreferableCipherAlgorithms  = new CipherAlgorithm[] { CipherAlgorithm.Blowfish, CipherAlgorithm.TripleDES };

            //this option is ignored with SSH1
            f.WindowSize = 0x1000; //NG: ERROR: MAC mismatch
            //f.WindowSize = 0x800; //NG
            //f.WindowSize = 0x30000; //NG
            //f.WindowSize = 0x400; //OK
            //f.CheckMACError = false; //NG: unexpected channel pt=SSH_MSG_CHANNEL_DATA local_channel=33243

            /* USER OPTION */
            //param.CancelTransfer = true;  // cancel flag
            param.ProgressCallback = delegate() { Debug.Write("*"); };   // callback function

            if (SSHConnection.SCPExecute(param, f, s))
            {
                Debug.WriteLine("scp success!");

                if (param.Direction == false)
                {
                    if (param.IoStream != null)
                    {
                        Debug.Write("IO Stream: ");
                        for (int i = 0; i < param.IoStream.Length; i++)
                        {
                            byte b = (byte)param.IoStream.ReadByte();
                            Debug.Write(b.ToString("x2") + " ");
                        }
                        Debug.WriteLine("");
                    }
                }
            }
            else
            {
                Debug.WriteLine("scp failure: " + param.ErrorMessage);
            }
#endif
        }