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 ( 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,
-
On the screen it Should ask me to input in format �username:password�
-
Read the content and encrypt it.
-
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.
-
Then in our main script, first read this encrypted content in credentials.txt file, DECRYPT it and write to some file in temp folder.
-
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...