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
- CiscoSX - script executor
- Device Config - nodes or machine data saving. Data includes: title, ip address, username, password, connect type (SSH or Telnet)
GroupConfig
- nodes group added form DeviceStore
- contains Device struct
in hashtable with data about machines or nodes, file system fast use access classes BackupOptions
- needs to configure tftp server to store backups - FailedList - list of results automating process (success or failed)
Scripting
- use Expect and putty to connect to remote device, generate script for each devices and execute this
Scripting Class Methods
public Scripting(BackupConfiguration config)
{
this.config = config;
InitDirectory();
InitPath();
InitScript();
CheckRequirements();
}
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; }
}
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();
}
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);
}
}
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;
}
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);
}
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());
}
}
void InitPath()
{
Process.Start(argscmd, argsint + set_path);
}
void InitDirectory()
{
explorer.DirectoryHiddenCreate(oper_path);
}
public void Clear()
{
explorer.DirectoryDelete(oper_path);
}
Other Features
- File and Directory Access classes - safety classes for accessing to file system
- 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.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.