Example #1
0
        public void Test_Vagrant_Connect()
        {
            SshConnectionInfo input = UserInput;

            SshShell shell = new SshShell(input.Host, input.User);

            if (input.Pass != null)
            {
                shell.Password = input.Pass;
            }
            if (input.IdentityFile != null)
            {
                shell.AddIdentityFile(input.IdentityFile);
            }

            //This statement must be prior to connecting
            shell.RedirectToConsole();

            Console.Write("Connecting...");
            shell.Connect();
            Console.WriteLine("OK");

            // SSH-2.0-OpenSSH_6.6.1p1 Ubuntu-2ubuntu2
            Console.WriteLine("server=" + shell.ServerVersion);

            SshExec shellExec = null;

            if (shell.ShellOpened)
            {
                // shell.Close();

                shellExec = SshExec.Clone(shell);
                // new SshExec(shell.Host, shell.Username, shell.Password);
                shellExec.Connect();
            }

            if (shellExec != null && shellExec.Connected)
            {
                var session = shellExec.Session;
                var channel = shellExec.Channel;
                Console.WriteLine(session);
                Console.WriteLine(channel);

                var stream = shellExec.RunCommandEx("ls -l", true);
                // = shell.StreamASCII();
                while (stream.MoveNext())
                {
                    Console.WriteLine(stream.Current);
                }

                System.Threading.Thread.Sleep(500);
            }

            Console.Write("Disconnecting...");
            if (shellExec != null)
            {
                shellExec.Close();
            }
            Console.WriteLine("OK");
        }
Example #2
0
        public static SshConnectionInfo GetConnectionInfo(string Host, string User, string Pass, string IdentityFile)
        {
            SshConnectionInfo info = new SshConnectionInfo();

            info.Host         = Host;
            info.User         = User;
            info.Pass         = Pass;
            info.IdentityFile = IdentityFile;
            return(info);
        }
Example #3
0
        public void Test_Vagrant_ls()
        {
            SshConnectionInfo input = UserInput;

            SshExec exec = new SshExec(input.Host, input.User);

            if (input.Pass != null)
            {
                exec.Password = input.Pass;
            }
            // if(input.IdentityFile != null) exec.AddIdentityFile( input.IdentityFile );

            Console.Write("Connecting...");
            exec.Connect();
            Console.WriteLine("OK");

            // while
            {
                Console.Write("Enter a command to execute ['Enter' to cancel]: ");
                string command = "ls";

                Console.WriteLine(command);

                var outputEnum = exec.RunCommandEx(command, false); // .RunCommand(command);
                while (outputEnum.MoveNext())
                {
                    Console.WriteLine(outputEnum.Current);
                }
            }

            SFTPUtil util = SFTPUtil.Clone(exec);

            var list = util.ListFiles("/");

            foreach (ChannelSftp.LsEntry line in list)
            {
                Console.WriteLine(line.Filename);
            }

            Console.Write("Disconnecting...");

            util.Dispose();
            exec.Close();
            Console.WriteLine("OK");
        }
        public void TransferFile(SshConnectionInfo input)
        {
            try
            {
                string proto = "scp";
                SshTransferProtocolBase sshCp;

                if (proto.Equals("scp"))
                    sshCp = new Scp(input.Host, input.User);
                else
                    sshCp = new Sftp(input.Host, input.User);

                if (input.Pass != null) sshCp.Password = input.Pass;
                //if (input.IdentityFile != null) sshCp.AddIdentityFile(input.IdentityFile);
                sshCp.OnTransferStart += new FileTransferEvent(sshCp_OnTransferStart);
                sshCp.OnTransferProgress += new FileTransferEvent(sshCp_OnTransferProgress);
                sshCp.OnTransferEnd += new FileTransferEvent(sshCp_OnTransferEnd);

                Console.Write("Connecting...");
                sshCp.Connect();
                Console.WriteLine("OK");

                if (input.Direction == TransferDirection.To)
                    sshCp.Put(input.LocalFilePath, input.RemoteFilePath);
                else
                    sshCp.Get(input.RemoteFilePath, input.LocalFilePath);


                Console.Write("Disconnecting...");
                sshCp.Close();
                Console.WriteLine("OK");
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                throw e;
            }
        }
Example #5
0
        public bool TranferirArquivo()
        {
            bool lRetorno = false;

            Sftp sshCp = null;

            try
            {
                gLogger.InfoFormat("*******************************************************************");
                gLogger.InfoFormat("SharpSSH > Abrindo conexão com ANBIMA******************************");
                gLogger.InfoFormat("*******************************************************************");

                DateTime lDtUil = new ImportacaoDbLib().SelecionaUltimoPregao();

                //SshTransferProtocolBase sshCp;

                SshConnectionInfo lInfo = new SshConnectionInfo();
                lInfo.Host = this.HostAnbimaSFTP;
                lInfo.User = this.UserAnbimaSFTP;
                lInfo.Pass = this.PasswordAnbimaSFTP;

                sshCp = new Sftp(lInfo.Host, lInfo.User);

                sshCp.Password = lInfo.Pass;

                gLogger.InfoFormat("Dados de conexão ANBIMA Host: [{0}] User: [{1}] password: [{2}]", lInfo.Host, lInfo.User, lInfo.Pass);

                sshCp.Connect();

                int lTentativaConexao = 0;

                while (!sshCp.Connected && lTentativaConexao != 10)
                {
                    gLogger.InfoFormat("SharpSSH > Aguardando conexão SFTP com ANBIMA  ******************************");

                    sshCp.Connect();

                    if (lTentativaConexao == 10)
                    {
                        gLogger.InfoFormat("Já tentou conectar 10 vez, saindo do loop de tentativa de conexão");

                        return(false);
                    }

                    Thread.Sleep(2000);

                    lTentativaConexao++;
                }

                sshCp.OnTransferStart    += new FileTransferEvent(sshCp_OnTransferStart);
                sshCp.OnTransferProgress += new FileTransferEvent(sshCp_OnTransferProgress);
                sshCp.OnTransferEnd      += new FileTransferEvent(sshCp_OnTransferEnd);

                string lpath = ConfigurationManager.AppSettings["ArquivosAnbima"];

                foreach (KeyValuePair <string, string> lDir in ListaDir)
                {
                    ArrayList list = sshCp.GetFileList(lDir.Value);

                    foreach (string arquivo in list)
                    {
                        gLogger.InfoFormat("SharpSSH > Arquivo encontrado no ambiente na ANBIMA -> [{0}] ", arquivo);
                    }

                    foreach (var item in list)
                    {
                        string lNome = item.ToString();

                        if (lNome.Length > 2)
                        {
                            string[] lNomeSplitado = lNome.Split('_');

                            int lAno = Convert.ToInt32(lNomeSplitado[2].Substring(0, 4));

                            int lMes = Convert.ToInt32(lNomeSplitado[2].Substring(4, 2));

                            int lDia = Convert.ToInt32(lNomeSplitado[2].Substring(6, 2));

                            DateTime lDtArquivo = new DateTime(lAno, lMes, lDia);

                            if (lDtArquivo == lDtUil ||
                                lDir.Key.ToLower() == "indicmes")
                            {
                                sshCp.Get(string.Concat(lDir.Value, lNome), lpath);
                            }
                        }
                    }

                    gLogger.InfoFormat("SharpSSH > Arquivo [{0}] transferido com sucesso.", lpath);
                }

                sshCp.Close();

                lRetorno = true;
            }
            catch (Exception ex)
            {
                gLogger.Error("SharpSSH > TranferirArquivo -", ex);
            }
            finally
            {
                if (sshCp != null && sshCp.Connected)
                {
                    sshCp.Close();
                }
            }


            return(lRetorno);
        }