65.9K
CodeProject 正在变化。 阅读更多。
Home

终端自动登录和思科配置自动备份程序

emptyStarIconemptyStarIconemptyStarIconemptyStarIconemptyStarIcon

0/5 (0投票)

2015年10月23日

CPOL

1分钟阅读

viewsIcon

6681

downloadIcon

2

用于学习 DataGridView、TreeView、文件访问、加密和进程的工具

引言

该应用程序供管理员使用,用于自动备份网络节点的配置,并通过 SSH 和 Telnet 远程执行脚本。请从 这里 下载二进制文件。

背景

我的应用程序使用 Tcl 中的 Expect 包来自动化发送和接收终端消息的过程,因此需要在您的机器上安装 Active State Tcl 应用程序。为了连接到设备,我使用 putty。我非常喜欢使用 KeePass,并尝试创建类似此应用程序的界面。

Using the Code

文件和窗体

  1. CiscoSX - 脚本执行器
  2. 设备配置 - 保存节点或机器数据。数据包括:标题、IP 地址、用户名、密码、连接类型(SSH 或 Telnet)
  3. GroupConfig - 添加节点的组窗体
  4. DeviceStore - 包含 Device 结构体 的哈希表,其中包含关于机器或节点的数据,以及文件系统快速访问类
  5. BackupOptions - 需要配置 TFTP 服务器以存储备份
  6. FailedList - 自动化过程的结果列表(成功或失败)
  7. Scripting - 使用 Expect 和 putty 连接到远程设备,为每个设备生成脚本并执行该脚本

脚本类方法

//
// to constructor we send our backup configuration information which sets in BackupOptions
  public Scripting(BackupConfiguration config)
        {
            this.config = config;
            InitDirectory();
            InitPath();
            InitScript();
            CheckRequirements();
        }
        //CHECK REQUIREMENTS
        bool CheckRequirements()
        {
            string path_expect="C:\\Tcl\\bin";
            string plink_exist = "C:\\Tcl\\bin\\plink.exe";
            hasExpect=explorer.DirectoryExist(path_expect);
            hasPlink = explorer.FileExists(plink_exist);            
            if (hasExpect & hasPlink)
                return true;
            else return false;
        }
        public BackupConfiguration BackupConfig
        {
            get { return config; }
            set { config = value; }
        }
        //DEVICE CONNECT AND BACKUP
        public void DeviceConnect(Device device_info)
        {            
            try
            {
                prev_device = device_info.ip_address;
                explorer.WriteCharacters(ScriptBuilder(device_info), execute_path);
                string full_path = explorer.FilePath(execute_path);              
                Process process = new Process();
                process.StartInfo.FileName = @"cmd.exe";
                string tmpinfo=process.StartInfo.Arguments = 
                	@"/Q /C " + argstcl + execute_path + argsstat + status_path;               
                process.Start();
                process.WaitForExit();
                CheckSuccess();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }
        public string LastLog
        {
            get { return lastlog; }
        }
        public List<string> GetFailed
        {
            get { return fatalConnect; }
        }
        public void ClearFailed()
        {
            fatalConnect.Clear();
        }
        //----------------------------------------------
        //CHECK OPERATION SUCCESS
        void CheckSuccess()
        {
            string[] content = new string[1];
            explorer.FileReadAllLines(status_path, ref content);
            string status = string.Join("\n", content).ToUpper();          
            if (status.Contains("FATAL ERROR") || 
            	status.Contains("FAILED"))
            {                
                fatalConnect.Add(prev_device);
                lastlog = "FAILED=>" + status.Replace("\n", " ");
                Logging(lastlog);
            }
            else
            {   
                string extension=".cfg";
                status = string.Join("\n", content);
                explorer.WriteCharacters(status, config.localPath + 
                	"\\" + prev_device+extension);
                lastlog = "SUCCESS=>" + prev_device;
                Logging(lastlog);
            }
        }
        //SCRIPT BUILD
        string ScriptBuilder(Device device_info)
        {
            string temp = "";
            script = "";
            switch (config.storage)
            {
                case StorageType.Local:
                    {
                        script += set + username + device_info.username + end + line;
                        script += set + password + device_info.password + end + line;
                        script += set + device + device_info.ip_address + end + line;
                        script += set + connect + device_info.connect.ToLower() + end + line;
                        temp += head_script+line;
                        temp += script;
                        temp += local_script;
                    }
                    break;
                case StorageType.Server:
                    {
                        script += set + username + device_info.username + end + line;
                        script += set + password + device_info.password + end + line;
                        script += set + device + device_info.ip_address + end + line;
                        script += set + connect + device_info.connect.ToLower() + end + line;
                        script += set + tftp + config.serverPath + end + line;
                        temp += head_script+line;
                        temp += script;
                        temp += tftp_script;
                    }
                    break;
            }
            return temp;
        }
        //LOG JOURNAL
        void Logging(string log)
        {
            string[] content = new string[1] { "**Logging journal Cisco SX" };
            explorer.FileReadAllLines(logjournal, ref content);
            string buffer = string.Join("\n", content);
            string line = "\n";
            string space = " => ";
            string date = DateTime.Now.ToString();
            explorer.WriteCharacters(buffer + line + date + space + log, logjournal);
        }
        //-------------------------------------------
        //INITIALIZE
        void InitScript()
        {            
            try
            {
                string[] content=new string[1];
                explorer.FileReadAllLines("Script\\head.tcl", ref content);
                head_script = string.Join("\n", content);                
                explorer.FileReadAllLines("Script\\local.tcl", ref content);
                local_script = string.Join("\n", content);
                explorer.FileReadAllLines("Script\\tftp.tcl", ref content);
                tftp_script=string.Join("\n", content);  
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }
        //start process accessing cmd => to use expect => for use putty
        void InitPath()
        {
            Process.Start(argscmd, argsint + set_path);
        }
        void InitDirectory()
        {
            explorer.DirectoryHiddenCreate(oper_path);
        }      
        //---------------------------------------------
        public void Clear()
        {
            explorer.DirectoryDelete(oper_path);
        }
//

其他特性

  1. 文件和目录访问类 - 用于访问文件系统的安全类
  2. Crypto - 安全加密 AES 类,用于加密网络节点和机器的登录名和密码

历史

恕我直言,这个工具可能不可用,但对于学习使用 DataGridViewTreeView、AES 加密和文件系统访问非常有用。对于远程自动备份,我建议您使用免费的 rConfig 或付费的 Cisco Prime。

© . All rights reserved.