Exemplo n.º 1
0
        public void RunTest()
        {
            Console.WriteLine("文件传输");
            sshCp.Connect();
            string localPath  = @"F:\file\Code\SharpSSH-1.1.1.13.src";
            string serverPath = @"/usr/lijie";

            FileInfo[] distFIles = FileService.GetAllFiles(localPath);
            if (distFIles == null)
            {
                Console.WriteLine("没有发现更新文件");
                //exec.Close();
                sshCp.Close();
            }
            Console.WriteLine("开始更新");
            foreach (FileInfo tempFileInfo in distFIles)
            {
                string tempNowPath = serverPath + tempFileInfo.DirectoryName.MyTrimStr(localPath, null).Replace(@"\", @"/") + @"/" + tempFileInfo.Name;
                try
                {
                    sshCp.Put(tempFileInfo.DirectoryName + @"\" + tempFileInfo.Name, tempNowPath);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    //scp: /tmp/dist/123: No such file or directory
                    if (ex.Message.Contains("No such file or directory"))
                    {
                        string tempPath = ex.Message;
                        tempPath = tempPath.Remove(0, tempPath.IndexOf(@"/"));
                        tempPath = tempPath.Remove(tempPath.LastIndexOf(@"/"));
                        Console.WriteLine("创建文件夹" + tempPath);
                        if (MySshTool.SshFileMkFullDir(sshCp, tempPath))
                        {
                            try
                            {
                                sshCp.Put(tempFileInfo.DirectoryName + @"\" + tempFileInfo.Name, tempNowPath);
                            }
                            catch (Exception innerEx)
                            {
                                Console.WriteLine(innerEx.Message);
                                Console.WriteLine("传输失败,跳过该文件");
                            }
                        }
                        else
                        {
                            Console.WriteLine("创建文件夹失败,跳过该文件");
                        }
                    }
                }
            }
            Console.WriteLine("更新完成");
            sshCp.Close();
        }
Exemplo n.º 2
0
        public void CopyFilesToRemote(List <FileInfo> files, string remotePath)
        {
            _sshCp.Connect();
            int i = 0;

            StatusModel.Instance.Status = "Copying files";
            foreach (FileInfo file in files)
            {
                _sshCp.Put(file.FullName, Path.Combine(remotePath, file.Name));
                i++;
                StatusModel.Instance.setProgress(i, files.Count);
            }

            _sshCp.Close();
        }
Exemplo n.º 3
0
 public void Close()
 {
     if (m_sshCp.Connected)
     {
         m_sshCp.Close();
     }
 }
Exemplo n.º 4
0
        private void UploadFile()
        {
            try
            {
                //sshCp = new Sftp(Host.address, Host.Connection.Username);
                sshCp = new Scp(Host.address, Host.Connection.Username);

                sshCp.Password            = Host.Connection.Password;
                sshCp.OnTransferProgress += new FileTransferEvent(sshCp_OnTransferProgress);
                sshCp.OnTransferEnd      += new FileTransferEvent(sshCp_OnTransferEnd);
                log.InfoFormat("sshCp Connecting...");
                sshCp.Connect();

                log.InfoFormat("sshCp Connecting OK .......");

                sshCp.Put(fromfile, outFolder + "/" + Path.GetFileName(fromfile));

                isoUploadState = UploadState.InProgress;

                sshCp.Close();
            } catch (Exception e)
            {
                log.Error(e);
                throw new Exception(Messages.UPLOAD_ISO_FAILED);
            }
        }
Exemplo n.º 5
0
        private void StartTransfer(SSHTransferProtocol Protocol)
        {
            if (AllFieldsSet() == false)
            {
                Runtime.MessageCollector.AddMessage(Messages.MessageClass.ErrorMsg, Language.strPleaseFillAllFields);
                return;
            }

            if (File.Exists(txtLocalFile.Text) == false)
            {
                Runtime.MessageCollector.AddMessage(Messages.MessageClass.WarningMsg, Language.strLocalFileDoesNotExist);
                return;
            }

            try
            {
                if (Protocol == SSHTransferProtocol.SCP)
                {
                    sshT = new Scp(txtHost.Text, txtUser.Text, txtPassword.Text);
                }
                else if (Protocol == SSHTransferProtocol.SFTP)
                {
                    sshT = new Sftp(txtHost.Text, txtUser.Text, txtPassword.Text);
                }

                sshT.OnTransferStart    += SshTransfer_Start;
                sshT.OnTransferProgress += SshTransfer_Progress;
                sshT.OnTransferEnd      += SshTransfer_End;

                sshT.Connect(Convert.ToInt32(txtPort.Text));

                LocalFile  = txtLocalFile.Text;
                RemoteFile = txtRemoteFile.Text;

                Thread t = new Thread(new ThreadStart(StartTransferBG));
                t.SetApartmentState(ApartmentState.STA);
                t.IsBackground = true;
                t.Start();
            }
            catch (Exception ex)
            {
                Runtime.MessageCollector.AddMessage(Messages.MessageClass.ErrorMsg, Language.strSSHTransferFailed + Environment.NewLine + ex.Message);
                sshT.Close();
            }
        }
Exemplo n.º 6
0
 private static void CloseSshConnection()
 {
     sftpBase.Close();
     ssh.Close();
 }
Exemplo n.º 7
0
        private static void Main(string[] args)
        {
            var env            = environments["vagrant"];
            var localPath      = "Z:\\WindowsDev\\Gateway\\";
            var remoteFilePath = "/home/tripservice/servicestack/";
            var host           = env.host;
            var user           = env.user;
            var password       = env.password;
            var sshPort        = env.sshPort;
            var monoServer     = "http://" + host + "/";
            var webServer      = "http://" + host + ":8080/";

            ssh = new SshExec(host, user, password);
            ssh.Connect(sshPort);
            Console.WriteLine("Connected");

            sftpBase = new Tamir.SharpSsh.Sftp(host, user, password);
            sftpBase.OnTransferStart += new FileTransferEvent(sftpBase_OnTransferStart);
            sftpBase.OnTransferEnd   += new FileTransferEvent(sftpBase_OnTransferEnd);
            Console.WriteLine("Trying to Open Connection...");
            sftpBase.Connect(sshPort);
            Console.WriteLine("Connected Successfully !");

            if (fullDeploy)
            {
                //Remove any old files and upload projects
                Console.WriteLine("Uploading projects");
                ssh.RunCommand("cd " + remoteFilePath);
                ssh.RunCommand("rm -rf " + remoteFilePath + "ServiceStack.* Booking*");
                ssh.RunCommand("mkdir -p " + remoteFilePath + "ServiceStack.TripThruGateway/Web");
                ssh.RunCommand("mkdir -p " + remoteFilePath + "ServiceStack.TripThruPartnerGateway/Web");
                ssh.RunCommand("mkdir -p " + remoteFilePath + "BookingWebsite");
                var omittedDirectories = new List <string> {
                    "packages"
                };
                UploadDirectory(localPath + "BookingWebsite", remoteFilePath + "BookingWebsite");
                UploadDirectory(localPath + "ServiceStack.TripThruGateway/Web", remoteFilePath + "ServiceStack.TripThruGateway/Web");
                UploadDirectory(localPath + "ServiceStack.TripThruPartnerGateway/Web", remoteFilePath + "ServiceStack.TripThruPartnerGateway/Web");
                ssh.RunCommand("mv " + remoteFilePath + "ServiceStack.TripThruGateway/Web/mono/*  " + remoteFilePath +
                               "ServiceStack.TripThruGateway/Web/bin");
                ssh.RunCommand("mv " + remoteFilePath + "ServiceStack.TripThruPartnerGateway/Web/mono/*  " +
                               remoteFilePath + "ServiceStack.TripThruPartnerGateway/Web/bin");
            }

            var webappNames = new List <string>();

            string[]      partnerConfigurations   = Directory.GetFiles("PartnerConfigurations/", "*.txt");
            List <string> partnerscallbackUrlMono = new List <string>();

            foreach (var partnerConfiguration in partnerConfigurations)
            {
                var configuration = JsonSerializer.DeserializeFromString <PartnerConfiguration>(File.ReadAllText(partnerConfiguration));
                configuration.TripThruUrlMono         = monoServer + configuration.TripThruUrlMono;
                configuration.Partner.CallbackUrlMono = monoServer + configuration.Partner.CallbackUrlMono;
                configuration.Partner.WebUrl          = webServer + configuration.Partner.WebUrl;
                string configStr = JsonSerializer.SerializeToString <PartnerConfiguration>(configuration);
                File.WriteAllText(partnerConfiguration, configStr);

                if (configuration.Enabled)
                {
                    partnerscallbackUrlMono.Add(configuration.Partner.CallbackUrlMono);
                    var name = configuration.Partner.Name.Replace(" ", "");
                    Console.WriteLine("Configuring " + name);
                    webappNames.Add(name);
                    ssh.RunCommand("cp -a " + remoteFilePath + "ServiceStack.TripThruPartnerGateway/  " + remoteFilePath +
                                   "ServiceStack." + name + "/");
                    ssh.RunCommand("rm " + remoteFilePath + "ServiceStack." + name + "/Web/PartnerConfiguration.txt");
                    sftpBase.Put(partnerConfiguration,
                                 remoteFilePath + "ServiceStack." + name + "/Web/PartnerConfiguration.txt");

                    var bookingwebConfig = new System.IO.StreamWriter("config.txt");
                    bookingwebConfig.WriteLine("HomeUrl=" + configuration.Partner.WebUrl);
                    bookingwebConfig.WriteLine("RelativeHomeUrl=" + configuration.Partner.WebUrlRelative);
                    bookingwebConfig.WriteLine("TripThruUrl=" + configuration.TripThruUrlMono);
                    bookingwebConfig.WriteLine("TripThruAccessToken=" + "jaosid1201231"); //fixed tripthru access token
                    bookingwebConfig.WriteLine("PartnerUrl=" + configuration.Partner.CallbackUrlMono);
                    bookingwebConfig.WriteLine("PartnerAccessToken=" + configuration.Partner.AccessToken);
                    bookingwebConfig.WriteLine("PartnerName=" + name);
                    bookingwebConfig.WriteLine("PartnerId=" + configuration.Partner.ClientId);
                    bookingwebConfig.Flush();
                    bookingwebConfig.Close();
                    ssh.RunCommand("rm " + remoteFilePath + "BookingWebsite/inc/tripthru/config.txt");
                    sftpBase.Put("config.txt", remoteFilePath + "BookingWebsite/inc/tripthru/");
                    ssh.RunCommand("rm " + remoteFilePath + "BookingWebsite/images/taxi-cars_logo.png");
                    var x = name + ".png";
                    var y = remoteFilePath + "BookingWebsite/images/taxi-cars_logo.png";
                    sftpBase.Put("PartnerConfigurations/" + name + ".png",
                                 remoteFilePath + "BookingWebsite/images/taxi-cars_logo.png");
                    ssh.RunCommand("rm -rf /var/www/sanfran/Bookings" + name);
                    ssh.RunCommand("cp -a " + remoteFilePath + "BookingWebsite/ /var/www/sanfran/Bookings" + name);
                }
            }

            if (fullDeploy)
            {
                //create fast-cgi mono webapp config
                var webappConfig = new System.IO.StreamWriter("tripthru.webapp");
                webappConfig.WriteLine("<apps>");
                webappConfig.Flush();

                webappConfig.WriteLine(@"<web-application>
                                    <name>TripThru.TripThruGateway</name>
                                    <vhost>*</vhost>
                                    <vport>80</vport>
                                    <vpath>/TripThru.TripThruGateway</vpath>
                                    <path>/var/www/ServiceStack.TripThruGateway/Web</path>
                                 </web-application>"
                                       );
                webappConfig.Flush();

                foreach (var webapp in webappNames)
                {
                    webappConfig.WriteLine(@"<web-application>
                                    <name>TripThru.{0}</name>
                                    <vhost>*</vhost>
                                    <vport>80</vport>
                                    <vpath>/TripThru.{0}</vpath>
                                    <path>/var/www/ServiceStack.{0}/Web</path>
                                 </web-application>", webapp
                                           );
                    webappConfig.Flush();
                }

                webappConfig.WriteLine("</apps>");
                webappConfig.Flush();
                webappConfig.Close();

                Console.WriteLine("Updating mono webapp config");
                ssh.RunCommand("rm /etc/rc.d/init.d/mono-fastcgi/tripthru.webapp");
                sftpBase.Put("tripthru.webapp", "/etc/rc.d/init.d/mono-fastcgi/tripthru.webapp");
            }

            Console.WriteLine("Stopping mono");
            ssh.RunCommand("kill -9 $(netstat -tpan |grep \"LISTEN\"|grep :9000|awk -F' ' '{print $7}'|awk -F'/' '{print $1}')");

            Console.WriteLine("Updating web folder");
            ssh.RunCommand("rm -rf /var/www/ServiceStack.*");
            ssh.RunCommand("cp -a " + remoteFilePath + "/ServiceStack.* /var/www/");

            Thread startMono = new Thread(
                delegate()
            {
                Console.WriteLine("Starting mono");
                ssh.RunCommand("export MONO_OPTIONS=\"--debug\"");
                ssh.RunCommand("fastcgi-mono-server4 --appconfigdir /etc/rc.d/init.d/mono-fastcgi /socket=tcp:127.0.0.1:9000 /logfile=/var/log/mono/fastcgi.log &");
            });

            startMono.Start();

            Console.WriteLine("Sleep 8 seconds, waiting for mono to initialize.");
            Thread.Sleep(8000);

            var client = new System.Net.WebClient();

            foreach (string callbackUrlMono in partnerscallbackUrlMono)
            {
                Console.WriteLine("Sending request to: \n" + @callbackUrlMono.ToString() + "log");
                while (true)
                {
                    try
                    {
                        var response        = client.DownloadString(@callbackUrlMono.ToString() + "log");
                        var analyzeResponse = JsonSerializer.DeserializeFromString <ResponseRequest>(response);
                        if (analyzeResponse.ResultCode.Equals("OK"))
                        {
                            Console.WriteLine("Correct.");
                            break;
                        }
                        else
                        {
                            Thread.Sleep(1000);
                        }
                    }
                    catch (Exception ex)
                    {
                    }
                }
            }


            Console.WriteLine("Done!");
            startMono.Abort();
            sftpBase.Close();
            ssh.Close();
        }
Exemplo n.º 8
0
 public void closeConnection()
 {
     sshCp.Close();
 }
Exemplo n.º 9
0
 public void close()
 {
     sshCp.Close();
     Connected = false;
 }