예제 #1
0
        public ActionResult DeleteSSH(int id)
        {
            int bookingId = id;

            //Declare MyBookings VM
            DeleteSSHVM model;

            using (Db db = new Db())
            {
                //Get user
                SSHRecordDTO dto = db.SSHRecords.FirstOrDefault(x => x.Id.Equals(bookingId));

                if (dto == null)
                {
                    TempData["Failure"] = "The account does not exist";
                    return(View("SSHRecord"));
                }

                model             = new DeleteSSHVM();
                model.Id          = dto.Id;
                model.SSHPassword = "";

                return(View("DeleteSSH", model));
            }
        }
예제 #2
0
        public ActionResult EditSSH(DeleteSSHVM model)
        {
            string SSH = @"c:\temp\ssh.bat";
            string Del = @"c:\temp\delete.bat";


            using (Db db = new Db())
            {
                if (db.SSHRecords.Any(x => x.Id.Equals(model.Id)))
                {
                    string OldUser;
                    string OldPass;

                    SSHRecordDTO dto = db.SSHRecords.Find(model.Id);
                    OldUser = dto.SSHUser;

                    if (!string.IsNullOrEmpty(model.SSHPassword))
                    {
                        CustomPasswordHasher hash = new CustomPasswordHasher();
                        string hashedPassword     = hash.HashPassword(model.SSHPassword);

                        if (hashedPassword.Equals(dto.SSHPassword))
                        {
                            OldPass = model.SSHPassword;

                            if (!System.IO.File.Exists(SSH))
                            {
                                // Create a file to write to.
                                using (StreamWriter sw = System.IO.File.CreateText(SSH))
                                {
                                    sw.WriteLine("en");
                                    sw.WriteLine("cisco");
                                    sw.WriteLine("");
                                    sw.WriteLine("conf t");
                                    sw.WriteLine("no username " + OldUser + " password " + OldPass);
                                    sw.WriteLine("enable password " + "cisco");
                                    sw.WriteLine("exit");
                                    sw.WriteLine("exit");
                                    sw.WriteLine("exit");
                                }
                            }
                        }
                        else
                        {
                            TempData["Failure"] = "Password does not match your SSH account";
                            return(View("DeleteSSH", model));
                        }
                    }

                    db.SSHRecords.Remove(dto);

                    db.SaveChanges();
                }
            }


            if (!System.IO.File.Exists(Del))
            {
                // Create a file to write to.
                using (StreamWriter sw = System.IO.File.CreateText(Del))
                {
                    sw.WriteLine("@echo off");
                    sw.WriteLine("del ssh.bat");
                    sw.WriteLine("del delete.bat");
                }
            }
            //Provision Account here
            string com     = @"C:\ProgramData\Microsoft\Windows\Start Menu\Programs\PuTTY (64-bit)";
            string com2    = @"putty.exe -ssh " + "admin" + "@" + "40.122.27.77" + " -pw " + "cisco" + " -m " + SSH;
            string command = "/C cd " + com + " & @echo off & " + com2 + " & cd.. & cd.. & cd.. & cd.. & cd.. & cd.. & cd C:/temp/ & @echo off & delete.bat";

            System.Diagnostics.Process.Start("cmd.exe", command);

            TempData["Success"] = "You have successfully deleted the SSH Account";
            return(RedirectToAction("ssh-record"));
        }