Create a Post
cancel
Showing results for 
Search instead for 
Did you mean: 
Amir_Arama
Advisor

help with a script

Jump to solution

Hi, i would love if someone can help me with this script. it used for backup clish configuration of all GWs remotely from mgmt, save i to file and upload to ftp server. i guess it's something simple to those familiar with bash more than i do.

it was working fine, by creating seperate file for each gw which GWName_Date.txt filename. but lately (maybe from some ver upgrade) it failed to save the filename with $hostname, so it's only save it with the date like 11.7.21.txt, and this cause that each time it override this file, because all files get the same name. if i just run from mgmt  cprid_util -server  x.x.x.x -verbose rexec -rcmd /bin/bash -c "hostname" it brings the correct hosname. what can be the problem here? how can i fix it?

thx!

 

#!/bin/sh
source /opt/CPshrd-R80.40/tmp/.CPprofile.sh
for dest in $(</var/log/scripts/gws_list.txt); do
hostname=`cprid_util -server $dest -verbose rexec -rcmd /bin/bash -c "hostname"`
now=$(date +"%m_%d_%Y").txt
cprid_util -server $dest -verbose rexec -rcmd /bin/clish -c "show configuration" > /var/log/gws_backup/$hostname_$now
done

.........ftp commands.....


rm /var/log/gws_backup/*

0 Kudos
1 Solution

Accepted Solutions
JozkoMrkvicka
Leader
Leader

#!/bin/bash
source /opt/CPshrd-R80.40/tmp/.CPprofile.sh
while read dest; do
gwhostname=$(cprid_util -server $dest -verbose rexec -rcmd /bin/bash -c "hostname")
now=$(date +"%m_%d_%Y").txt
cprid_util -server $dest -verbose rexec -rcmd /bin/clish -c "show configuration" > /var/log/gws_backup/"$gwhostname"_"$now"

done </var/log/scripts/gws_list.txt

Kind regards,
Jozko Mrkvicka

View solution in original post

(1)
11 Replies
JozkoMrkvicka
Leader
Leader

1. replace #!/bin/sh with #!/bin/bash

2. if still not working, try to rename "hostname" with something else, like "gw_hostname" and save it as variable as you have for date:

gw_hostname=$(cprid_util -server $dest -verbose rexec -rcmd /bin/bash -c "hostname")

do not forget to replace new variable of hostname in last cprid_util command

PS: I suggest to do not use exact path, like you have for source command. If you upgrade to another version (to R81.x), you will need to replace the path in the source command (use $FWDIR instead).

Kind regards,
Jozko Mrkvicka
Amir_Arama
Advisor

Hi,

thank you. here is the script now, but it still do the same thing - ignore the hostname and only writes the date as the filename.

[Expert@fwm-nihuli:0]# cat gws_backup.sh
#!/bin/bash
source /opt/CPshrd-R80.40/tmp/.CPprofile.sh
for dest in $(</var/log/scripts/gws_list.txt); do
gwhostname=$(cprid_util -server $dest -verbose rexec -rcmd /bin/bash -c "hostname")
now=$(date +"%m_%d_%Y").txt
cprid_util -server $dest -verbose rexec -rcmd /bin/clish -c "show configuration" > /var/log/gws_backup/$gwhostname_$now
done

as for the option to run it on every gw. i used to do that, but it's have to maintain. as i have tens of gws.

0 Kudos
JozkoMrkvicka
Leader
Leader

Is file /var/log/scripts/gws_list.txt present and NOT empty?

are you getting any output from variable "dest" ? try to add "echo $dest" in the loop to see if this will give you desired value (the IPs or hostnames).

Maybe that "for" is not working properly.

Try to use following instead:

while read dest; do
  echo "$dest"
. . . done </var/log/scripts/gws_list.txt

 

Kind regards,
Jozko Mrkvicka
0 Kudos
Amir_Arama
Advisor

Hi,

thank you so much for your assistance. unfortunately i don't have much knowledge in bash scripts so i don't know how to complete your ". . ." if you can write all the section as it should be so i can copy & paste it, i will appreciate it. 

as for the dest, i know it's working in some way because when i tail the output file ($date.txt)  whlie it runs i can see that it go over the FWs(IPS) list from the /var/log/scripts/gws_list.txt file(yes it's exist). so the file is replaced but it run on all the gws list and write their configuration to the file.

0 Kudos
JozkoMrkvicka
Leader
Leader

#!/bin/bash
source /opt/CPshrd-R80.40/tmp/.CPprofile.sh
while read dest; do
echo "$dest"
gwhostname=$(cprid_util -server $dest -verbose rexec -rcmd /bin/bash -c "hostname")
now=$(date +"%m_%d_%Y").txt
cprid_util -server $dest -verbose rexec -rcmd /bin/clish -c "show configuration" > /var/log/gws_backup/$gwhostname_$now

done </var/log/scripts/gws_list.txt

Kind regards,
Jozko Mrkvicka
0 Kudos
Amir_Arama
Advisor

i copied it exactly, and the result is the same. output file name ignoring hostname.

only now when running it, i can see output on the screen with the IP's of the GWs it working on live:

#GWS_list

x.x.x.x
x.x.x.x
x.x.x.x
x.x.x.x
x.x.x.x

0 Kudos
Amir_Arama
Advisor

it didn't change the result. the only change was that on the window i run the script i see live output that goes line by line from the gws_list.txt file, so i see on which gw it's working at any moment, but the output filename still goes without hostname..

0 Kudos
JozkoMrkvicka
Leader
Leader

#!/bin/bash
source /opt/CPshrd-R80.40/tmp/.CPprofile.sh
while read dest; do
gwhostname=$(cprid_util -server $dest -verbose rexec -rcmd /bin/bash -c "hostname")
now=$(date +"%m_%d_%Y").txt
cprid_util -server $dest -verbose rexec -rcmd /bin/clish -c "show configuration" > /var/log/gws_backup/"$gwhostname"_"$now"

done </var/log/scripts/gws_list.txt

Kind regards,
Jozko Mrkvicka
(1)
Amir_Arama
Advisor

It worked!

You are the best

Thank you very much

0 Kudos
the_rock
Champion
Champion

Jozko is 100% right...that was actually first thing I noticed as well. Let us know if that works.

0 Kudos
JozkoMrkvicka
Leader
Leader

By the way, another way would be to schedule cron job at specific date on every affected gateway to run command "clish -c 'save configuration <PATH>' "

and then just transfer the backup file to ftp server.

There are plenty of options how to reach the goal of every script 🙂

Kind regards,
Jozko Mrkvicka
0 Kudos