Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Terminal Autologin and Cisco Configuration Auto Backuper

0.00/5 (No votes)
23 Oct 2015 1  
Tools for learning DataGridView, TreeView, File Accessing, Cryptography, Process

Intro

App for administrators needs to autobackup configuration from network nodes, execute remotely script over SSH and Telnet. Download binary from here.

Background

My app uses Expect package in Tcl to automate process send and receive messages from terminal, so this is required to install for your machine Active State Tcl application. For connecting to device, I use putty. I really like to use KeePass and tried to create an interface like this app.

Using the Code

Files and Forms

  1. CiscoSX - script executor
  2. Device Config - nodes or machine data saving. Data includes: title, ip address, username, password, connect type (SSH or Telnet)
  3. GroupConfig - nodes group added form
  4. DeviceStore - contains Device struct in hashtable with data about machines or nodes, file system fast use access classes
  5. BackupOptions - needs to configure tftp server to store backups
  6. FailedList - list of results automating process (success or failed)
  7. Scripting - use Expect and putty to connect to remote device, generate script for each devices and execute this

Scripting Class Methods

//
// 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);
        }
//

Other Features

  1. File and Directory Access classes - safety classes for accessing to file system
  2. Crypto - security crypto AES classes for crypt data of network nodes and machines login and password

History

IMHO this tool is not usable, but is very useful for learning to use DataGridView, TreeView, AES crypting and File System accessing. For remotely autoback, I will advice you to use free rConfig or paid Cisco Prime.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here