Help with Backup Shell Script for Network Device Configuration backup

HI all, im new to shell scripting. need your guidence for my script. i wrote one script and is attached here

Im explaining the requirement of script.

AIM: Shell script to run automatically as per scheduled and backup few network devices configurations. Script will contain a set of commands which will run on the N/w devices and log the output to a text file and save them to the destination folder in the mentioned path.

I have 100 devices which are mix of Routers, switches, firewalls and packet shapers. Each device has different set of commands. so I plan to make a text files which contain all the device info like device role and ip address of the device, user credentials and Destination path where to save the logs Below are the example of the txt files content.

Device_info.txt
devrole:ipadd
router:10.10.10.1
switch:20.20.20.1
pix:30.30.30.1
netscreen:40.40.40.1
shaper:50.50.50.1

credentials.txt
username:password

Dest_path.txt
/usr/conf_backups/

� In simple, script should read Device_info.txt file and connect to the specified device via SSH.
� Read �credentials.txt� file for authentication
� Check the device role IF condition and jump to the respective loop,
� Run the set of commands and log the output to destination folder.
� Read �Dest_path.txt� file for Destination path.
� Cycle should loop until it complete all the entryes in the �Device_info.txt� file.

In script I�m creating 5 variables for username($usr), password($passwd), destination path($dest), Device role($dev) and IP address($ip)
Now using script I want to read each line from device_info.text, split the line into two parts, content before and after the delimiter (:slight_smile: and save them into the respective variables. For example, if It read 2nd line(router:10.10.10.1) from the device_info.txt, it should separate the router and send to variable ($dev) and 10.10.10.1 send to variable($IP).

In the code I have to send different commands to different types of devices. So I want to create loops with different set of commands. Then that particular loop should be called by if condition. If IF condition satisfy, should execute the respective commands in the loop.

As im new to scripting, I don�t know how to write if conditions. Need your help and guidance to achieve this. For illustration I just mentioned how my IF condition should be.

If [ �$dev� == �router� ]
then
execute loop (1)
else
read the next line from the Device_info.txt
fi

If [�$dev� == �switch� ]
then
execute loop (2)
else
read the next line from the Device_info.txt
fi

If [�$dev� == �pix� ]
then
execute loop (3)
else
read the next line from the Device_info.txt
fi
����.so on��.

Case {1}
do
{
Send few set of commands to my router
}
Done

Case {2}
do
{
send few set of commands to my switch
}
Done

Case {3}
do
{
send few set of commands to my firewall
log_file $dest
}
Done

Im attaching you my script. Which is having a problem and described below.

if, library functions are defined as below,
#!/usr/bin/expect -f
#!/bin/sh
spawn, expect commands are working but Functions command is not working. for reference plz observe attachmant 11.png file

if, library functions are defined as below
#!/bin/sh
#!/usr/bin/expect -f

Function commands are working but spawn, expect commands are not working. for reference plz observe attachmant 22.png file

if, library functions are defined as below, and no function commands are using in our script, its working fine. for reference plz observe attachmant working.png file this script is my old and basic script

#!/usr/bin/expect -f
#!/bin/sh

Can you suggest me solution or an alternative approach to fulfill my requirement�

And I also want to create one more script for encrypting my credentials.txt file then I can achieve Confidentiality. so while auditing, auditor wil not say anything. This script should work like this. For egg., consider this script name is encrypt.sh when I execute this script,

  1.   On the screen it Should ask me to input in format �username:password�
    
  2.   Read the content and encrypt it.
    
  3.   Find the path from where this script is running and write the encrypted content to credentials.text file. If this credentials.txt file is already exists, should over write it, if not create newly.
    
  4.   Then in our main script, first read this encrypted content in credentials.txt file, DECRYPT it and write to some file in temp folder. 
    
  5.   Then read the decrypted txt file from temp folder and execute our old procedure like read the line and split content by the delimiter and save them to respective variables. 
    

Guide me on doing this friends and suggest me if there are any other easy ways to achieve this encryption procedure.

Thank you...:slight_smile:

You might want to reduce the values to either:

  • RDBMS-like lines/rows for easy comparison with sort and comm, and then write a tool to turn them into configuration files or scripts to reinstall the configuration. If you want to change some parameters, modify the file and then feed the lines to the tool. Each line/row might have one value after its description. The storge of a value that appears many places in one cell means generated files are mutually consistent.
  • Write a shadow subtree of host-dirs/files: configuration files or scripts to reinstall the configuration. You can then diff -r saved subtrees with new subtrees.

This way, you can easily detect a changed configuration, and restore it.

Hi DGPickett,

thanks for your responce. aim of my script is SSH to device, execute few commands, capture screen output and log them to a file. like this i have 100+ devices. few devices have different set of commands. so i ve to execute commands with respective to the device type. thats it, im not looking for any thing like comparasion and all.

share if you have any ideas.....:slight_smile:

Well, proceessing the data makes it more interpretable, comparable and usable for reset of values.

You might test commands and dynamically choose commands that are available/effective, like with 'which' and with command-specific trials, so new machines with known commands are adapted to automatically.

To avoid sending a script to 100+ servers, you can either use something like "ssh user@host bash <local_bash_script" or mount a small dir everywhere, once.

Sometimes, having a script called /system.profile on each box that sets up normal variables for that host, which enables the master script to be much simpler. It can have shell functions to provide the same values on every host using local commands, or flag variables to vary the master script.

Gnu parallel is great for doing this very quickly.

1 Like

Thanks for your sugessions DGPickett....:slight_smile: