Exemple #1
0
        public void UploadViaSftp(string zipFileName, string sftpUrl, string userName, string password)
        {
            SFtp sftp = new SFtp();

            //  Any string automatically begins a fully-functional 30-day trial.
            bool success;

            success = sftp.UnlockComponent("HURRIYSSH_XAN0ZUeq9En4");
            if (success != true)
            {
                throw new ApplicationException(sftp.LastErrorText);
            }

            //  Set some timeouts, in milliseconds:
            sftp.ConnectTimeoutMs = 60000;
            sftp.IdleTimeoutMs    = 300000;

            //  Connect to the SSH server.
            //  The standard SSH port = 22
            //  The hostname may be a hostname or IP address.
            int    port;
            string hostname;

            hostname = sftpUrl;
            port     = 22;
            success  = sftp.Connect(hostname, port);
            if (success != true)
            {
                throw new ApplicationException(sftp.LastErrorText);
            }

            //  Authenticate with the SSH server.  Chilkat SFTP supports
            //  both password-based authenication as well as public-key
            //  authentication.  This example uses password authenication.
            success = sftp.AuthenticatePw(userName, password);
            if (success != true)
            {
                throw new ApplicationException(sftp.LastErrorText);
            }

            //  After authenticating, the SFTP subsystem must be initialized:
            success = sftp.InitializeSftp();
            if (success != true)
            {
                throw new ApplicationException(sftp.LastErrorText);
            }

            //  Open a file on the server for writing.
            //  "createTruncate" means that a new file is created; if the file already exists, it is opened and truncated.
            string handle;

            handle = sftp.OpenFile(Path.GetFileName(zipFileName), "writeOnly", "createTruncate");
            if (handle == null)
            {
                throw new ApplicationException(sftp.LastErrorText);
            }

            //  Upload from the local file to the SSH server.
            success = sftp.UploadFile(handle, zipFileName);
            if (success != true)
            {
                throw new ApplicationException(sftp.LastErrorText);
            }

            //  Close the file.
            success = sftp.CloseHandle(handle);
            if (success != true)
            {
                throw new ApplicationException(sftp.LastErrorText);
            }
        }
Exemple #2
0
        public string WriteTilSFtp(int lobnr)
        {
            string TilPBSFilename = "Unknown";
            int    FilesSize;

            var rec_regnskab = Program.qryAktivRegnskab();

            var qry_selectfiles =
                from h in Program.dbData3060.Tblpbsforsendelse
                join d1 in Program.dbData3060.Tblpbsfiles on h.Id equals d1.Pbsforsendelseid into details1
                from d1 in details1.DefaultIfEmpty()
                where d1.Id != null && d1.Filename == null
                join d2 in Program.dbData3060.Tbltilpbs on h.Id equals d2.Pbsforsendelseid into details2
                from d2 in details2.DefaultIfEmpty()
                where d2.Id == lobnr
                select new
            {
                tilpbsid = (int?)d2.Id,
                d2.Leverancespecifikation,
                d2.Delsystem,
                d2.Leverancetype,
                Bilagdato        = (DateTime?)d2.Bilagdato,
                Pbsforsendelseid = (int?)d2.Pbsforsendelseid,
                Udtrukket        = (DateTime?)d2.Udtrukket,
                pbsfilesid       = (int?)d1.Id,
                Leveranceid      = (int)h.Leveranceid
            };

            int antal = qry_selectfiles.Count();

            if (antal > 0)
            {
                var rec_selecfiles = qry_selectfiles.First();

                var qry_pbsfiles = from h in Program.dbData3060.Tblpbsfiles
                                   where h.Id == rec_selecfiles.pbsfilesid
                                   select h;
                if (qry_pbsfiles.Count() > 0)
                {
                    Tblpbsfiles m_rec_pbsfiles = qry_pbsfiles.First();
                    TilPBSFilename = AddPbcnetRecords(rec_selecfiles.Delsystem, rec_selecfiles.Leveranceid, m_rec_pbsfiles.Id);
                    bool success;


                    var qry_pbsfile =
                        from h in m_rec_pbsfiles.Tblpbsfile
                        orderby h.Seqnr
                        select h;

                    string TilPBSFile = "";
                    int    i          = 0;
                    foreach (var rec_pbsfile in qry_pbsfile)
                    {
                        if (i++ > 0)
                        {
                            TilPBSFile += "\r\n";
                        }
                        TilPBSFile += rec_pbsfile.Data;
                    }
                    char[] c_TilPBSFile = TilPBSFile.ToCharArray();
                    byte[] b_TilPBSFile = System.Text.Encoding.GetEncoding("windows-1252").GetBytes(c_TilPBSFile);
                    FilesSize = b_TilPBSFile.Length;

                    sendAttachedFile(TilPBSFilename, b_TilPBSFile, true);

                    string fullpath = m_rec_sftp.Inbound + "/" + TilPBSFilename;
                    string handle   = m_sftp.OpenFile(fullpath, "writeOnly", "createTruncate");
                    if (handle == null)
                    {
                        throw new Exception(m_sftp.LastErrorText);
                    }

                    success = m_sftp.WriteFileBytes(handle, b_TilPBSFile);
                    if (!success)
                    {
                        throw new Exception(m_sftp.LastErrorText);
                    }

                    success = m_sftp.CloseHandle(handle);
                    if (success != true)
                    {
                        throw new Exception(m_sftp.LastErrorText);
                    }

                    m_rec_pbsfiles.Type         = 8;
                    m_rec_pbsfiles.Path         = m_rec_sftp.Inbound;
                    m_rec_pbsfiles.Filename     = TilPBSFilename;
                    m_rec_pbsfiles.Size         = FilesSize;
                    m_rec_pbsfiles.Atime        = DateTime.Now;
                    m_rec_pbsfiles.Mtime        = DateTime.Now;
                    m_rec_pbsfiles.Transmittime = DateTime.Now;
                    Program.dbData3060.SubmitChanges();
                }
            }
            return(TilPBSFilename);
        }
Exemple #3
0
        public bool WriteTilSFtp(XDocument xdoc)
        {
            Guid id1 = clsSQLite.insertStoreXML(Program.sftpName, false, Program.AppEngName, xdoc.ToString(), "");

            m_SendqueueId      = xdoc.Descendants("Sendqueue").Descendants("Id").First().Value;
            m_PbsfileId        = xdoc.Descendants("Pbsfile").Descendants("Id").First().Value;
            m_TilPBSFilename   = xdoc.Descendants("Pbsfile").Descendants("TilPBSFilename").First().Value;
            m_Transmisionsdato = DateTime.Parse(xdoc.Descendants("Pbsfile").Descendants("Transmisionsdato").First().Value);
            m_SendData         = xdoc.Descendants("Pbsfile").Descendants("SendData").First().Value;

            bool   success;
            string TilPBSFilename = m_TilPBSFilename;
            string TilPBSFile     = m_SendData;

            char[] c_TilPBSFile = TilPBSFile.ToCharArray();
            byte[] b_TilPBSFile = System.Text.Encoding.GetEncoding("windows-1252").GetBytes(c_TilPBSFile);
            int    FilesSize    = b_TilPBSFile.Length;

            sendAttachedFile(TilPBSFilename, b_TilPBSFile, true);

            string fullpath = m_Inbound + "/" + TilPBSFilename;
            string handle   = m_sftp.OpenFile(fullpath, "writeOnly", "createTruncate");

            if (handle == null)
            {
                throw new Exception(m_sftp.LastErrorText);
            }

            success = m_sftp.WriteFileBytes(handle, b_TilPBSFile);
            if (!success)
            {
                throw new Exception(m_sftp.LastErrorText);
            }

            success = m_sftp.CloseHandle(handle);
            if (success != true)
            {
                throw new Exception(m_sftp.LastErrorText);
            }

            clsSQLite.updateStoreXML(id1, true);

            XElement xmlPbsfilesUpdate = new XElement("Pbsfiles");

            xmlPbsfilesUpdate.Add(new XElement("SendqueueId", m_SendqueueId));
            xmlPbsfilesUpdate.Add(new XElement("Id", m_PbsfileId));
            xmlPbsfilesUpdate.Add(new XElement("Type", 8));
            xmlPbsfilesUpdate.Add(new XElement("Path", m_Inbound));
            xmlPbsfilesUpdate.Add(new XElement("Filename", TilPBSFilename));
            xmlPbsfilesUpdate.Add(new XElement("Size", FilesSize));
            xmlPbsfilesUpdate.Add(new XElement("Atime", DateTime.Now));
            xmlPbsfilesUpdate.Add(new XElement("Mtime", DateTime.Now));
            xmlPbsfilesUpdate.Add(new XElement("Transmittime", m_Transmisionsdato));
            string strxmlPbsfilesUpdate = @"<?xml version=""1.0"" encoding=""utf-8"" ?> " + xmlPbsfilesUpdate.ToString();;

            Guid id2 = clsSQLite.insertStoreXML(Program.AppEngName, false, Program.sftpName, strxmlPbsfilesUpdate, "");

            clsRest   objRest    = new clsRest();
            string    strxmldata = objRest.HttpPost2(clsRest.urlBaseType.data, "tilpbs", strxmlPbsfilesUpdate);
            XDocument xmldata    = XDocument.Parse(strxmldata);
            string    Status     = xmldata.Descendants("Status").First().Value;

            if (Status == "True")
            {
                clsSQLite.updateStoreXML(id2, true);
                return(true);
            }
            else
            {
                return(false);
            }
        }
        private async Task FtpBlobStreams(CloudBlobContainer container, BlobResultSegment blobs, ILogger log)
        {
            await StubBlobContainerMetadata(container);

            if (!blobs.Results.Any())
            {
                log.LogInformation("No blobs to ftp.");
                return;
            }

            log.LogInformation("Found blobs to ftp.");

            Chilkat.Global obj = new Global();

            // this will come from key vault
            obj.UnlockBundle("KARMAK.CBX032021_TDNDgEuT804Y");

            SFtp sftp = new SFtp();

            // resolve to auth token from container identity metadata
            var authToken = await GetAuthToken(
                ExtractMetadataValue(container, "X-Elk-Identity-Account"),
                ExtractMetadataValue(container, "X-Elk-Identity-Branch"),
                ExtractMetadataValue(container, "X-Elk-Identity-User"));

            log.LogInformation($"auth token acquired: {authToken}");

            // these will come from settings via container metadata
            bool success = sftp.Connect("karmakqasftp.westus.cloudapp.azure.com", 22);

            if (success)
            {
                log.LogInformation("Connected to sftp server.");
                success = sftp.AuthenticatePw("karmaksftp", "K@rmakQa2019");
            }

            if (success)
            {
                log.LogInformation("Successfully authenticated.");
                success = sftp.InitializeSftp();
            }

            if (!success)
            {
                log.LogError("Error initializing sftp.");
                Console.WriteLine(sftp.LastErrorText);
                return;
            }

            foreach (var blob in blobs.Results)
            {
                var blobFile = (CloudBlockBlob)blob;
                log.LogInformation($"Next file to upload: {blobFile.Name}");

                await blobFile.FetchAttributesAsync();

                log.LogInformation("Extracting identity metadata from blob file.");

                var blobStream = GetBlobStream(blobFile);

                log.LogInformation($"Uploading {blobFile.Name}");
                var handle = sftp.OpenFile("/upload/testFile.xml", "readWrite", "createNew");
                sftp.WriteFileBytes(handle, blobStream.Result.ToArray());
                sftp.CloseHandle(handle);
                log.LogInformation("File successfully uploaded.");

                await blobFile.DeleteAsync();

                log.LogInformation("Blob removed from container.");
            }
        }
Exemple #5
0
        public void ReadDirectoryFraSFtp()
        {
            string homedir = m_sftp.RealPath(".", "");
            //  Open a directory on the server...
            string handle = m_sftp.OpenDir(m_rec_sftp.Outbound);

            if (handle == null)
            {
                throw new Exception(m_sftp.LastErrorText);
            }

            //  Download the directory listing:
            Chilkat.SFtpDir dirListing = null;
            dirListing = m_sftp.ReadDir(handle);
            if (dirListing == null)
            {
                throw new Exception(m_sftp.LastErrorText);
            }

            Program.memPbsnetdir = null; //opret ny memPbsnetdir

            //  Iterate over the files.
            int i;
            int n = dirListing.NumFilesAndDirs;

            if (n > 0)
            {
                for (i = 0; i <= n - 1; i++)
                {
                    Chilkat.SFtpFile fileObj = null;
                    fileObj = dirListing.GetFileObject(i);
                    if (!fileObj.IsDirectory)
                    {
                        recPbsnetdir rec = new recPbsnetdir
                        {
                            Type     = 8,
                            Path     = dirListing.OriginalPath,
                            Filename = fileObj.Filename,
                            Size     = (int)fileObj.Size32,
                            Atime    = fileObj.LastAccessTime,
                            Mtime    = fileObj.LastModifiedTime,
                            Gid      = fileObj.Gid,
                            Uid      = fileObj.Uid,
                            Perm     = fileObj.Permissions.ToString()
                        };
                        Program.memPbsnetdir.Add(rec);
                    }
                }
            }

            //  Close the directory
            bool success = m_sftp.CloseHandle(handle);

            if (!success)
            {
                throw new Exception(m_sftp.LastErrorText);
            }

            var leftqry_pbsnetdir =
                from h in Program.memPbsnetdir
                join d1 in Program.dbData3060.Tblpbsnetdir on new { h.Path, h.Filename } equals new { d1.Path, d1.Filename } into details
            from d1 in details.DefaultIfEmpty(new Tblpbsnetdir {
                Id = -1, Type = (int?)null, Path = null, Filename = null, Size = (int?)null, Atime = (DateTime?)null, Mtime = (DateTime?)null, Perm = null, Uid = (int?)null, Gid = (int?)null
            })
            where d1.Path == null && d1.Filename == null
            select h;

            int    AntalFiler = leftqry_pbsnetdir.Count();
            string MailBody   = "";

            if (leftqry_pbsnetdir.Count() > 0)
            {
                foreach (var rec_pbsnetdir in leftqry_pbsnetdir)
                {
                    MailBody += "\n" + rec_pbsnetdir.Filename + " - " + rec_pbsnetdir.Mtime.ToString() + " - " + rec_pbsnetdir.Size.ToString();

                    Tblpbsnetdir m_rec_pbsnetdir = new Tblpbsnetdir
                    {
                        Type     = rec_pbsnetdir.Type,
                        Path     = rec_pbsnetdir.Path,
                        Filename = rec_pbsnetdir.Filename,
                        Size     = rec_pbsnetdir.Size,
                        Atime    = rec_pbsnetdir.Atime,
                        Mtime    = rec_pbsnetdir.Mtime,
                        Perm     = rec_pbsnetdir.Perm,
                        Uid      = rec_pbsnetdir.Uid,
                        Gid      = rec_pbsnetdir.Gid
                    };
                    Program.dbData3060.Tblpbsnetdir.InsertOnSubmit(m_rec_pbsnetdir);
                }
                sendAttachedFile(MailBody);
            }
            Program.dbData3060.SubmitChanges();
            return;
        }