Advertisement
qiwichupa

Save cisco config over ftp

Apr 6th, 2012
522
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 2.49 KB | None | 0 0
  1. #!/usr/bin/perl
  2. #  'expect' is needed
  3. #
  4. #  Use special user on ftp-server for running this script.
  5. #  It's a better and tested way, but you can also make some little changes in code
  6. #  and use different servers for running scripts and conf-files collecting.
  7. #  1. Place this script in some directory inside homedir (~/bin for example)
  8. #  2. Create additional list of your cisco-routers (see SETTINGS part for additional info)
  9. #  3. Create additional directory for saved conf-files (~/config for example)
  10. #  4. Fill SETTINGS part correctly
  11. #  5. If you use enable password - uncomment "UNCOMMENT IF ENABLE" part of script
  12. #
  13. #  Run the script and you will see the *.tgz with all conf-files in homedir.
  14.  
  15. use English;
  16.  
  17. # SETTINGS
  18. $ftpserver = '192.168.1.100';
  19. $FTPUSER="ftpuser";
  20. $FTPPASS="ftppassword";
  21.  
  22. # cisco user
  23. $USER="ciscouser";
  24. $PASS_USER="ciscopassword";
  25. # ENABLE password
  26. $PASS_ENABLE="enablepass";
  27.  
  28. # directory with this file
  29. $DIR="/home/ftpuser/bin";
  30. # directory inside 'ftpuser' homedir where script will place configs
  31. $CONFDIR='config';
  32.  
  33. # cisco list in format:
  34. # cisco1-name::192.168.1.1
  35. # cisco2-name::192.168.1.2 etc.
  36. $filename = "$DIR/cisco.list";
  37.  
  38. # CODE
  39. system("cd $DIR");
  40. system("rm -f ./temp.sh");
  41. ($DAY, $MONTH, $YEAR) = (localtime)[3,4,5];
  42. $MONTH=$MONTH+1;
  43. $YEAR=$YEAR+1900;
  44. open DATA, $filename or die "Unable to open $filename: $!";
  45.  
  46. while (<DATA>) {
  47.   chomp;
  48.   ($name, $ip) = split(/::/);
  49.  
  50.   system("cd $DIR");
  51.   system("echo '#!/usr/bin/expect -f' >> $DIR/temp.sh");
  52.   system("echo spawn telnet $ip >> $DIR/temp.sh");
  53.   system("echo expect 'Username: ' >> $DIR/temp.sh");
  54.   system("echo send '\"$USER\n\"' >> $DIR/temp.sh");
  55.   system("echo expect 'Password: ' >> $DIR/temp.sh");
  56.   system("echo send '\"$PASS_USER\n\"' >> $DIR/temp.sh");
  57.  # UNCOMMENT IF ENABLE
  58.  # system("echo send \"enable\\n\" >> $DIR/temp.sh");
  59.  # system("echo expect 'Password: ' >> $DIR/temp.sh");
  60.  # system("echo send \"$PASS_ENABLE\\n\" >> $DIR/temp.sh");
  61.   system("echo expect '\\#' >> $DIR/temp.sh");
  62.   system("echo send '\"copy running-config ftp://$FTPUSER:$FTPPASS\@$ftpserver/$CONFDIR/config-$name-($YEAR-$MONTH-$DAY)\n\n\n\n\"' >> $DIR/temp.sh");
  63.   system("echo expect '\\#' >> $DIR/temp.sh");
  64.   system("echo send '\"exit\n\"' >> $DIR/temp.sh");
  65.   system("chmod 755 $DIR/temp.sh");
  66.   system("$DIR/temp.sh");
  67.   system("rm -f $DIR/temp.sh");
  68. }
  69.  
  70. close DATA;
  71.  
  72. system("rm -f $DIR/temp.sh");
  73. system("tar -czvf ~/$YEAR-$MONTH-$DAY.tgz ~/$CONFDIR/  && rm -rf  ~/$CONFDIR/**");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement