CISCO Perl脚本 - 多个语法错误

时间:2015-03-19 06:27:41

标签: perl backup asa

当我从Ubuntu 14.04运行以下代码时,我遇到了多个语法错误。

root @bnbackup:〜#perl asa_backup_3

#!/usr/bin/perl

#Function: Backup/restore configuration/extensions to/from a TFTP server.

#Description: The objective of this script is to show how to back up 
#configurations/extensions before the backup/restore command is developed. 

# It currently backs up the running configuration, all extensions imported via "import 
#webvpn" command, the CSD configuration XML file, and the DAP configuration XML file.

#Requirements: Perl with Expect, SSH to the ASA, and a TFTP server.

#Usage: backupasa -option option_value

#       -h: ASA hostname or IP address

#       -u: User name to log in via SSH

#       -w: Password to log in via SSH

#       -e: The Enable password on the security appliance

#       -p: Global configuration mode prompt

#       -s: Host name or IP address of the TFTP server to store the configurations

#       -r: Restore with an argument that specifies the file name. This file is produced 
#during backup. 

#If you don't enter an option, the script will prompt for it prior to backup.

#

#Make sure that you can SSH to the ASA.

use Expect;

use Getopt::Std;

#global variables

%options=();

$restore = 0; #does backup by default

$restore_file = `';

$asa = `';

$storage = `';

$user = `';

$password = `';

$enable = `';

$prompt = `';

$date = `date +%F';

chop($date);

my $exp = new Expect();

getopts("h:u:p:w:e:s:r:",\%options);

do process_options();

do login($exp);

do enable($exp);

if ($restore) {

   do restore($exp,$restore_file);

}

else {

   $restore_file = "$prompt-restore-$date.cli";

   open(OUT,">$restore_file") or die "Can't open $restore_file\n";

   do running_config($exp);

   do lang_trans($exp);

   do customization($exp);

   do plugin($exp);

   do url_list($exp);

   do webcontent($exp);

   do dap($exp);

   do csd($exp);

   close(OUT);

}

do finish($exp);

sub enable {

   $obj = shift;

   $obj->send("enable\n");

   unless ($obj->expect(15, `Password:')) {

      print "timed out waiting for Password:\n";

   }

   $obj->send("$enable\n");

   unless ($obj->expect(15, "$prompt#")) {

      print "timed out waiting for $prompt#\n";

   }

}

sub lang_trans {

   $obj = shift;

   $obj->clear_accum();

   $obj->send("show import webvpn translation-table\n");

   $obj->expect(15, "$prompt#" );

   $output = $obj->before();

   @items = split(/\n+/, $output);

   for (@items) {

     s/^\s+//;

     s/\s+$//;

     next if /show import/ or /Translation Tables/;

     next unless (/^.+\s+.+$/);

     ($lang, $transtable) = split(/\s+/,$_);

     $cli = "export webvpn translation-table $transtable language $lang 
$storage/$prompt-$date-$transtable-$lang.po";

     $ocli = $cli;

     $ocli =~ s/^export/import/;

     print "$cli\n";

     print OUT "$ocli\n";

     $obj->send("$cli\n");

     $obj->expect(15, "$prompt#" );

   }

}

sub running_config {

  $obj = shift;

  $obj->clear_accum();

  $cli ="copy /noconfirm running-config $storage/$prompt-$date.cfg";

  print "$cli\n";

  $obj->send("$cli\n");

  $obj->expect(15, "$prompt#" );

}

sub customization {

  $obj = shift;

  $obj->clear_accum();

  $obj->send("show import webvpn customization\n");

  $obj->expect(15, "$prompt#" );

  $output = $obj->before();

  @items = split(/\n+/, $output);

  for (@items) {

    chop;

    next if /^Template/ or /show import/ or /^\s*$/;

    $cli = "export webvpn customization $_ $storage/$prompt-$date-cust-$_.xml";

    $ocli = $cli;

    $ocli =~ s/^export/import/;

    print "$cli\n";

    print OUT "$ocli\n";

    $obj->send("$cli\n");

    $obj->expect(15, "$prompt#" );

  }

}

sub plugin {

   $obj = shift;

   $obj->clear_accum();

   $obj->send("show import webvpn plug-in\n");

   $obj->expect(15, "$prompt#" );

   $output = $obj->before();

   @items = split(/\n+/, $output);

   for (@items) {

     chop;

     next if /^Template/ or /show import/ or /^\s*$/;

     $cli = "export webvpn plug-in protocol $_ $storage/$prompt-$date-plugin-$_.jar";

     $ocli = $cli;

     $ocli =~ s/^export/import/;

     print "$cli\n";

     print OUT "$ocli\n";

     $obj->send("$cli\n");

     $obj->expect(15, "$prompt#" );

   }

}

sub url_list {

   $obj = shift;

   $obj->clear_accum();

   $obj->send("show import webvpn url-list\n");

   $obj->expect(15, "$prompt#" );

   $output = $obj->before();

   @items = split(/\n+/, $output);

   for (@items) {

     chop;

     next if /^Template/ or /show import/ or /^\s*$/ or /No bookmarks/;

     $cli="export webvpn url-list $_ $storage/$prompt-$date-urllist-$_.xml";

     $ocli = $cli;

     $ocli =~ s/^export/import/;

     print "$cli\n";

     print OUT "$ocli\n";

     $obj->send("$cli\n");

     $obj->expect(15, "$prompt#" );

   }

}

sub dap {

   $obj = shift;

   $obj->clear_accum();

   $obj->send("dir dap.xml\n");

   $obj->expect(15, "$prompt#" );

   $output = $obj->before();

   return 0 if($output =~ /Error/);

   $cli="copy /noconfirm dap.xml $storage/$prompt-$date-dap.xml";

   $ocli="copy /noconfirm $storage/$prompt-$date-dap.xml disk0:/dap.xml";

   print "$cli\n";

   print OUT "$ocli\n";

   $obj->send("$cli\n");

   $obj->expect(15, "$prompt#" );

}

sub csd {

   $obj = shift;

   $obj->clear_accum();

   $obj->send("dir sdesktop\n");

   $obj->expect(15, "$prompt#" );

   $output = $obj->before();

   return 0 if($output =~ /Error/);

   $cli="copy /noconfirm sdesktop/data.xml $storage/$prompt-$date-data.xml";

   $ocli="copy /noconfirm $storage/$prompt-$date-data.xml disk0:/sdesktop/data.xml";

   print "$cli\n";

   print OUT "$ocli\n";

   $obj->send("$cli\n");

   $obj->expect(15, "$prompt#" );

}

sub webcontent {

   $obj = shift;

   $obj->clear_accum();

   $obj->send("show import webvpn webcontent\n");

   $obj->expect(15, "$prompt#" );

   $output = $obj->before();

   @items = split(/\n+/, $output);

   for (@items) {

     s/^\s+//;

     s/\s+$//;

     next if /show import/ or /No custom/;

     next unless (/^.+\s+.+$/);

     ($url, $type) = split(/\s+/,$_);

     $turl = $url;

     $turl =~ s/\/\+//;

     $turl =~ s/\+\//-/;

     $cli = "export webvpn webcontent $url $storage/$prompt-$date-$turl";

     $ocli = $cli;

     $ocli =~ s/^export/import/;

     print "$cli\n";

     print OUT "$ocli\n";

     $obj->send("$cli\n");

     $obj->expect(15, "$prompt#" );

   }

}

sub login {

    $obj = shift;

    $obj->raw_pty(1);

    $obj->log_stdout(0); #turn off console logging.

    $obj->spawn("/usr/bin/ssh $user\@$asa") or die "can't spawn ssh\n";

    unless ($obj->expect(15, "password:" )) {

        die "timeout waiting for password:\n";

    }

    $obj->send("$password\n");

    unless ($obj->expect(15, "$prompt>" )) {

        die "timeout waiting for $prompt>\n";

    }

}

sub finish {

    $obj = shift;

    $obj->hard_close();

    print "\n\n";

}

sub restore {

   $obj = shift;

   my $file = shift;

   my $output;

   open(IN,"$file") or die "can't open $file\n";

   while (<IN>) {

      $obj->send("$_");

      $obj->expect(15, "$prompt#" );

      $output = $obj->before();

      print "$output\n";

   }

   close(IN);

}

sub process_options {

  if (defined($options{s})) {

      $tstr= $options{s};

      $storage = "tftp://$tstr";

  }

  else {

      print "Enter TFTP host name or IP address:";

      chop($tstr=<>);

      $storage = "tftp://$tstr";

  }

  if (defined($options{h})) {

      $asa = $options{h};

  }

  else {

      print "Enter ASA host name or IP address:";

      chop($asa=<>);

  }



  if (defined ($options{u})) {

      $user= $options{u};

  }

  else {

      print "Enter user name:";

      chop($user=<>);

  }



  if (defined ($options{w})) {

      $password= $options{w};

  }

  else {

      print "Enter password:";

      chop($password=<>);

  }

  if (defined ($options{p})) {

      $prompt= $options{p};

  }

  else {

      print "Enter ASA prompt:";

      chop($prompt=<>);

  }

  if (defined ($options{e})) {

      $enable = $options{e};

  }

  else {

      print "Enter enable password:";

      chop($enable=<>);

  }

  if (defined ($options{r})) {

     $restore = 1;

     $restore_file = $options{r};

  }

}

错误:

String found where operator expected at asa_backup_3 line 31, near "$storage = `'"
  (Might be a runaway multi-line '' string starting on line 30)
    (Missing semicolon on previous line?)
String found where operator expected at asa_backup_3 line 34, near "$enable = `'"
  (Might be a runaway multi-line '' string starting on line 33)
    (Missing semicolon on previous line?)
Bareword found where operator expected at asa_backup_3 line 36, near "$date = `date"
  (Might be a runaway multi-line `` string starting on line 35)
    (Missing operator before date?)
String found where operator expected at asa_backup_3 line 49, near "open(OUT,">$restore_file") or die "Can'"
  (Might be a runaway multi-line '' string starting on line 36)
    (Missing semicolon on previous line?)
Bareword found where operator expected at asa_backup_3 line 49, near "open(OUT,">$restore_file") or die "Can't"
Backslash found where operator expected at asa_backup_3 line 49, near "$restore_file\"
    (Missing operator before \?)
String found where operator expected at asa_backup_3 line 63, near "$obj->send(""
  (Might be a runaway multi-line "" string starting on line 49)
    (Missing semicolon on previous line?)
Backslash found where operator expected at asa_backup_3 line 63, near "enable\"
String found where operator expected at asa_backup_3 line 65, near "print ""
  (Might be a runaway multi-line "" string starting on line 63)
    (Missing semicolon on previous line?)
String found where operator expected at asa_backup_3 line 67, near "$obj->send(""
  (Might be a runaway multi-line "" string starting on line 65)
    (Missing semicolon on previous line?)
Backslash found where operator expected at asa_backup_3 line 67, near "$enable\"
    (Missing operator before \?)
String found where operator expected at asa_backup_3 line 68, near "unless ($obj->expect(15, ""
  (Might be a runaway multi-line "" string starting on line 67)
    (Missing semicolon on previous line?)
syntax error at asa_backup_3 line 31, near "$storage = `'"
syntax error at asa_backup_3 line 34, near "$enable = `'"
syntax error at asa_backup_3 line 36, near "$date = `date "
Unmatched right curly bracket at asa_backup_3 line 70, at end of line
Unmatched right curly bracket at asa_backup_3 line 71, at end of line
syntax error at asa_backup_3 line 94, near "}"
syntax error at asa_backup_3 line 102, near "}"
syntax error at asa_backup_3 line 121, near "}"
syntax error at asa_backup_3 line 140, near "}"
syntax error at asa_backup_3 line 159, near "}"
asa_backup_3 has too many errors.

我想知道这些错误是否确实存在,因为此代码来自CISCO

有人可以提出一种方法来运行代码而不会出现所有这些错误。

谢谢!

更新

按照Jens说明并删除反引号后,我收到此错误:

Bareword found where operator expected at asa_backup_6 line 2, near "/extensions to/from a"
    (Missing operator before a?)
Unknown regexp modifier "/f" at asa_backup_6 line 2, at end of line
Unknown regexp modifier "/r" at asa_backup_6 line 2, at end of line
syntax error at asa_backup_6 line 2, near "/extensions to/from a TFTP "
"use" not allowed in expression at asa_backup_6 line 35, at end of line
BEGIN not safe after errors--compilation aborted at asa_backup_6 line 37.

更新2:

这就是我的代码现在在文件asa_backup_6中的样子:

#!/usr/bin/perl

#Function: Backup/restore configuration/extensions to/from a TFTP server.

#Description: The objective of this script is to show how to back up 
#configurations/extensions before the backup/restore command is developed. 

# It currently backs up the running configuration, all extensions imported via "import 
#webvpn" command, the CSD configuration XML file, and the DAP configuration XML file.

#Requirements: Perl with Expect, SSH to the ASA, and a TFTP server.

#Usage: backupasa -option option_value

#       -h: ASA hostname or IP address

#       -u: User name to log in via SSH

#       -w: Password to log in via SSH

#       -e: The Enable password on the security appliance

#       -p: Global configuration mode prompt

#       -s: Host name or IP address of the TFTP server to store the configurations

#       -r: Restore with an argument that specifies the file name. This file is produced 
#during backup. 

#If you don't enter an option, the script will prompt for it prior to backup.

#

#Make sure that you can SSH to the ASA.

use Expect;

use Getopt::Std;

#global variables

%options=();

$restore = 0; #does backup by default

$restore_file = '';

$asa = '';

$storage = '';

$user = '';

$password = '';

$enable = '';

$prompt = '';

$date = `date +%F`;

chop($date);

my $exp = new Expect();

getopts("h:u:p:w:e:s:r:",\%options);

do process_options();

do login($exp);

do enable($exp);

if ($restore) {

   do restore($exp,$restore_file);

}

else {

   $restore_file = "$prompt-restore-$date.cli";

   open(OUT,">$restore_file") or die "Can't open $restore_file\n";

   do running_config($exp);

   do lang_trans($exp);

   do customization($exp);

   do plugin($exp);

   do url_list($exp);

   do webcontent($exp);

   do dap($exp);

   do csd($exp);

   close(OUT);

}

do finish($exp);

sub enable {

   $obj = shift;

   $obj->send("enable\n");

   unless ($obj->expect(15, 'Password:')) {

      print "timed out waiting for Password:\n";

   }

   $obj->send("$enable\n");

   unless ($obj->expect(15, "$prompt#")) {

      print "timed out waiting for $prompt#\n";

   }

}

sub lang_trans {

   $obj = shift;

   $obj->clear_accum();

   $obj->send("show import webvpn translation-table\n");

   $obj->expect(15, "$prompt#" );

   $output = $obj->before();

   @items = split(/\n+/, $output);

   for (@items) {

     s/^\s+//;

     s/\s+$//;

     next if /show import/ or /Translation Tables/;

     next unless (/^.+\s+.+$/);

     ($lang, $transtable) = split(/\s+/,$_);

     $cli = "export webvpn translation-table $transtable language $lang 
$storage/$prompt-$date-$transtable-$lang.po";

     $ocli = $cli;

     $ocli =~ s/^export/import/;

     print "$cli\n";

     print OUT "$ocli\n";

     $obj->send("$cli\n");

     $obj->expect(15, "$prompt#" );

   }

}

sub running_config {

  $obj = shift;

  $obj->clear_accum();

  $cli ="copy /noconfirm running-config $storage/$prompt-$date.cfg";

  print "$cli\n";

  $obj->send("$cli\n");

  $obj->expect(15, "$prompt#" );

}

sub customization {

  $obj = shift;

  $obj->clear_accum();

  $obj->send("show import webvpn customization\n");

  $obj->expect(15, "$prompt#" );

  $output = $obj->before();

  @items = split(/\n+/, $output);

  for (@items) {

    chop;

    next if /^Template/ or /show import/ or /^\s*$/;

    $cli = "export webvpn customization $_ $storage/$prompt-$date-cust-$_.xml";

    $ocli = $cli;

    $ocli =~ s/^export/import/;

    print "$cli\n";

    print OUT "$ocli\n";

    $obj->send("$cli\n");

    $obj->expect(15, "$prompt#" );

  }

}

sub plugin {

   $obj = shift;

   $obj->clear_accum();

   $obj->send("show import webvpn plug-in\n");

   $obj->expect(15, "$prompt#" );

   $output = $obj->before();

   @items = split(/\n+/, $output);

   for (@items) {

     chop;

     next if /^Template/ or /show import/ or /^\s*$/;

     $cli = "export webvpn plug-in protocol $_ $storage/$prompt-$date-plugin-$_.jar";

     $ocli = $cli;

     $ocli =~ s/^export/import/;

     print "$cli\n";

     print OUT "$ocli\n";

     $obj->send("$cli\n");

     $obj->expect(15, "$prompt#" );

   }

}

sub url_list {

   $obj = shift;

   $obj->clear_accum();

   $obj->send("show import webvpn url-list\n");

   $obj->expect(15, "$prompt#" );

   $output = $obj->before();

   @items = split(/\n+/, $output);

   for (@items) {

     chop;

     next if /^Template/ or /show import/ or /^\s*$/ or /No bookmarks/;

     $cli="export webvpn url-list $_ $storage/$prompt-$date-urllist-$_.xml";

     $ocli = $cli;

     $ocli =~ s/^export/import/;

     print "$cli\n";

     print OUT "$ocli\n";

     $obj->send("$cli\n");

     $obj->expect(15, "$prompt#" );

   }

}

sub dap {

   $obj = shift;

   $obj->clear_accum();

   $obj->send("dir dap.xml\n");

   $obj->expect(15, "$prompt#" );

   $output = $obj->before();

   return 0 if($output =~ /Error/);

   $cli="copy /noconfirm dap.xml $storage/$prompt-$date-dap.xml";

   $ocli="copy /noconfirm $storage/$prompt-$date-dap.xml disk0:/dap.xml";

   print "$cli\n";

   print OUT "$ocli\n";

   $obj->send("$cli\n");

   $obj->expect(15, "$prompt#" );

}

sub csd {

   $obj = shift;

   $obj->clear_accum();

   $obj->send("dir sdesktop\n");

   $obj->expect(15, "$prompt#" );

   $output = $obj->before();

   return 0 if($output =~ /Error/);

   $cli="copy /noconfirm sdesktop/data.xml $storage/$prompt-$date-data.xml";

   $ocli="copy /noconfirm $storage/$prompt-$date-data.xml disk0:/sdesktop/data.xml";

   print "$cli\n";

   print OUT "$ocli\n";

   $obj->send("$cli\n");

   $obj->expect(15, "$prompt#" );

}

sub webcontent {

   $obj = shift;

   $obj->clear_accum();

   $obj->send("show import webvpn webcontent\n");

   $obj->expect(15, "$prompt#" );

   $output = $obj->before();

   @items = split(/\n+/, $output);

   for (@items) {

     s/^\s+//;

     s/\s+$//;

     next if /show import/ or /No custom/;

     next unless (/^.+\s+.+$/);

     ($url, $type) = split(/\s+/,$_);

     $turl = $url;

     $turl =~ s/\/\+//;

     $turl =~ s/\+\//-/;

     $cli = "export webvpn webcontent $url $storage/$prompt-$date-$turl";

     $ocli = $cli;

     $ocli =~ s/^export/import/;

     print "$cli\n";

     print OUT "$ocli\n";

     $obj->send("$cli\n");

     $obj->expect(15, "$prompt#" );

   }

}

sub login {

    $obj = shift;

    $obj->raw_pty(1);

    $obj->log_stdout(0); #turn off console logging.

    $obj->spawn("/usr/bin/ssh $user\@$asa") or die "can't spawn ssh\n";

    unless ($obj->expect(15, "password:" )) {

        die "timeout waiting for password:\n";

    }

    $obj->send("$password\n");

    unless ($obj->expect(15, "$prompt>" )) {

        die "timeout waiting for $prompt>\n";

    }

}

sub finish {

    $obj = shift;

    $obj->hard_close();

    print "\n\n";

}

sub restore {

   $obj = shift;

   my $file = shift;

   my $output;

   open(IN,"$file") or die "can't open $file\n";

   while (<IN>) {

      $obj->send("$_");

      $obj->expect(15, "$prompt#" );

      $output = $obj->before();

      print "$output\n";

   }

   close(IN);

}

sub process_options {

  if (defined($options{s})) {

      $tstr= $options{s};

      $storage = "tftp://$tstr";

  }

  else {

      print "Enter TFTP host name or IP address:";

      chop($tstr=<>);

      $storage = "tftp://$tstr";

  }

  if (defined($options{h})) {

      $asa = $options{h};

  }

  else {

      print "Enter ASA host name or IP address:";

      chop($asa=<>);

  }



  if (defined ($options{u})) {

      $user= $options{u};

  }

  else {

      print "Enter user name:";

      chop($user=<>);

  }



  if (defined ($options{w})) {

      $password= $options{w};

  }

  else {

      print "Enter password:";

      chop($password=<>);

  }

  if (defined ($options{p})) {

      $prompt= $options{p};

  }

  else {

      print "Enter ASA prompt:";

      chop($prompt=<>);

  }

  if (defined ($options{e})) {

      $enable = $options{e};

  }

  else {

      print "Enter enable password:";

      chop($enable=<>);

  }

  if (defined ($options{r})) {

     $restore = 1;

     $restore_file = $options{r};

  }

}

更新3

删除行后@Jens在下面的评论中说:“#Function:备份/恢复配置/扩展到/来自TFTP服务器。”

我明白了。现在请注意,它要求我输入底线: - )

root@bnbackup:~# perl  asa_backup_7
Use of "do" to call subroutines is deprecated at asa_backup_7 line 64.
Use of "do" to call subroutines is deprecated at asa_backup_7 line 66.
Use of "do" to call subroutines is deprecated at asa_backup_7 line 68.
Use of "do" to call subroutines is deprecated at asa_backup_7 line 72.
Use of "do" to call subroutines is deprecated at asa_backup_7 line 82.
Use of "do" to call subroutines is deprecated at asa_backup_7 line 84.
Use of "do" to call subroutines is deprecated at asa_backup_7 line 86.
Use of "do" to call subroutines is deprecated at asa_backup_7 line 88.
Use of "do" to call subroutines is deprecated at asa_backup_7 line 90.
Use of "do" to call subroutines is deprecated at asa_backup_7 line 92.
Use of "do" to call subroutines is deprecated at asa_backup_7 line 94.
Use of "do" to call subroutines is deprecated at asa_backup_7 line 96.
Use of "do" to call subroutines is deprecated at asa_backup_7 line 102.
**Enter TFTP host name or IP address:**

2 个答案:

答案 0 :(得分:0)

来自$restore_file =&#39 ;;的行是反对和单引号混合。将其更改为单引号。

cange:

restore_file = `';

$asa = `';

$storage = `';

$user = `';

$password = `';

$enable = `';

$prompt = `';

$date = `date +%F';

restore_file = '';

$asa = '';

$storage = '';

$user = '';

$password = '';

$enable = '';

$prompt = '';

$date = `date +%F`;

<强>更新 这是另一个反击:

unless ($obj->expect(15, `Password:')) {

还必须更改为:

unless ($obj->expect(15, 'Password:')) {

答案 1 :(得分:0)

以上人员编辑后的完整工作代码:

#!/usr/bin/perl
#Description: The objective of this script is to show how to back up 
#configurations/extensions before the backup/restore command is developed. 
# It currently backs up the running configuration, all extensions imported via "import 
#webvpn" command, the CSD configuration XML file, and the DAP configuration XML file.
#Requirements: Perl with Expect, SSH to the ASA, and a TFTP server.
#Usage: backupasa -option option_value

#       -h: ASA hostname or IP address

#       -u: User name to log in via SSH

#       -w: Password to log in via SSH

#       -e: The Enable password on the security appliance

#       -p: Global configuration mode prompt

#       -s: Host name or IP address of the TFTP server to store the configurations

#       -r: Restore with an argument that specifies the file name. This file is produced 
#during backup. 

#If you don't enter an option, the script will prompt for it prior to backup.

#

#Make sure that you can SSH to the ASA.

use Expect;

use Getopt::Std;

#global variables

%options=();

$restore = 0; #does backup by default

$restore_file = '';

$asa = '';

$storage = '';

$user = '';

$password = '';

$enable = '';

$prompt = '';

$date = `date +%F`;

chop($date);

my $exp = new Expect();

getopts("h:u:p:w:e:s:r:",\%options);

do process_options();

do login($exp);

do enable($exp);

if ($restore) {

   do restore($exp,$restore_file);

}

else {

   $restore_file = "$prompt-restore-$date.cli";

   open(OUT,">$restore_file") or die "Can't open $restore_file\n";

   do running_config($exp);

   do lang_trans($exp);

   do customization($exp);

   do plugin($exp);

   do url_list($exp);

   do webcontent($exp);

   do dap($exp);

   do csd($exp);

   close(OUT);

}

do finish($exp);

sub enable {

   $obj = shift;

   $obj->send("enable\n");

   unless ($obj->expect(15, 'Password:')) {

      print "timed out waiting for Password:\n";

   }

   $obj->send("$enable\n");

   unless ($obj->expect(15, "$prompt#")) {

      print "timed out waiting for $prompt#\n";

   }

}

sub lang_trans {

   $obj = shift;

   $obj->clear_accum();

   $obj->send("show import webvpn translation-table\n");

   $obj->expect(15, "$prompt#" );

   $output = $obj->before();

   @items = split(/\n+/, $output);

   for (@items) {

     s/^\s+//;

     s/\s+$//;

     next if /show import/ or /Translation Tables/;

     next unless (/^.+\s+.+$/);

     ($lang, $transtable) = split(/\s+/,$_);

     $cli = "export webvpn translation-table $transtable language $lang 
$storage/$prompt-$date-$transtable-$lang.po";

     $ocli = $cli;

     $ocli =~ s/^export/import/;

     print "$cli\n";

     print OUT "$ocli\n";

     $obj->send("$cli\n");

     $obj->expect(15, "$prompt#" );

   }

}

sub running_config {

  $obj = shift;

  $obj->clear_accum();

  $cli ="copy /noconfirm running-config $storage/$prompt-$date.cfg";

  print "$cli\n";

  $obj->send("$cli\n");

  $obj->expect(15, "$prompt#" );

}

sub customization {

  $obj = shift;

  $obj->clear_accum();

  $obj->send("show import webvpn customization\n");

  $obj->expect(15, "$prompt#" );

  $output = $obj->before();

  @items = split(/\n+/, $output);

  for (@items) {

    chop;

    next if /^Template/ or /show import/ or /^\s*$/;

    $cli = "export webvpn customization $_ $storage/$prompt-$date-cust-$_.xml";

    $ocli = $cli;

    $ocli =~ s/^export/import/;

    print "$cli\n";

    print OUT "$ocli\n";

    $obj->send("$cli\n");

    $obj->expect(15, "$prompt#" );

  }

}

sub plugin {

   $obj = shift;

   $obj->clear_accum();

   $obj->send("show import webvpn plug-in\n");

   $obj->expect(15, "$prompt#" );

   $output = $obj->before();

   @items = split(/\n+/, $output);

   for (@items) {

     chop;

     next if /^Template/ or /show import/ or /^\s*$/;

     $cli = "export webvpn plug-in protocol $_ $storage/$prompt-$date-plugin-$_.jar";

     $ocli = $cli;

     $ocli =~ s/^export/import/;

     print "$cli\n";

     print OUT "$ocli\n";

     $obj->send("$cli\n");

     $obj->expect(15, "$prompt#" );

   }

}

sub url_list {

   $obj = shift;

   $obj->clear_accum();

   $obj->send("show import webvpn url-list\n");

   $obj->expect(15, "$prompt#" );

   $output = $obj->before();

   @items = split(/\n+/, $output);

   for (@items) {

     chop;

     next if /^Template/ or /show import/ or /^\s*$/ or /No bookmarks/;

     $cli="export webvpn url-list $_ $storage/$prompt-$date-urllist-$_.xml";

     $ocli = $cli;

     $ocli =~ s/^export/import/;

     print "$cli\n";

     print OUT "$ocli\n";

     $obj->send("$cli\n");

     $obj->expect(15, "$prompt#" );

   }

}

sub dap {

   $obj = shift;

   $obj->clear_accum();

   $obj->send("dir dap.xml\n");

   $obj->expect(15, "$prompt#" );

   $output = $obj->before();

   return 0 if($output =~ /Error/);

   $cli="copy /noconfirm dap.xml $storage/$prompt-$date-dap.xml";

   $ocli="copy /noconfirm $storage/$prompt-$date-dap.xml disk0:/dap.xml";

   print "$cli\n";

   print OUT "$ocli\n";

   $obj->send("$cli\n");

   $obj->expect(15, "$prompt#" );

}

sub csd {

   $obj = shift;

   $obj->clear_accum();

   $obj->send("dir sdesktop\n");

   $obj->expect(15, "$prompt#" );

   $output = $obj->before();

   return 0 if($output =~ /Error/);

   $cli="copy /noconfirm sdesktop/data.xml $storage/$prompt-$date-data.xml";

   $ocli="copy /noconfirm $storage/$prompt-$date-data.xml disk0:/sdesktop/data.xml";

   print "$cli\n";

   print OUT "$ocli\n";

   $obj->send("$cli\n");

   $obj->expect(15, "$prompt#" );

}

sub webcontent {

   $obj = shift;

   $obj->clear_accum();

   $obj->send("show import webvpn webcontent\n");

   $obj->expect(15, "$prompt#" );

   $output = $obj->before();

   @items = split(/\n+/, $output);

   for (@items) {

     s/^\s+//;

     s/\s+$//;

     next if /show import/ or /No custom/;

     next unless (/^.+\s+.+$/);

     ($url, $type) = split(/\s+/,$_);

     $turl = $url;

     $turl =~ s/\/\+//;

     $turl =~ s/\+\//-/;

     $cli = "export webvpn webcontent $url $storage/$prompt-$date-$turl";

     $ocli = $cli;

     $ocli =~ s/^export/import/;

     print "$cli\n";

     print OUT "$ocli\n";

     $obj->send("$cli\n");

     $obj->expect(15, "$prompt#" );

   }

}

sub login {

    $obj = shift;

    $obj->raw_pty(1);

    $obj->log_stdout(0); #turn off console logging.

    $obj->spawn("/usr/bin/ssh $user\@$asa") or die "can't spawn ssh\n";

    unless ($obj->expect(15, "password:" )) {

        die "timeout waiting for password:\n";

    }

    $obj->send("$password\n");

    unless ($obj->expect(15, "$prompt>" )) {

        die "timeout waiting for $prompt>\n";

    }

}

sub finish {

    $obj = shift;

    $obj->hard_close();

    print "\n\n";

}

sub restore {

   $obj = shift;

   my $file = shift;

   my $output;

   open(IN,"$file") or die "can't open $file\n";

   while (<IN>) {

      $obj->send("$_");

      $obj->expect(15, "$prompt#" );

      $output = $obj->before();

      print "$output\n";

   }

   close(IN);

}

sub process_options {

  if (defined($options{s})) {

      $tstr= $options{s};

      $storage = "tftp://$tstr";

  }

  else {

      print "Enter TFTP host name or IP address:";

      chop($tstr=<>);

      $storage = "tftp://$tstr";

  }

  if (defined($options{h})) {

      $asa = $options{h};

  }

  else {

      print "Enter ASA host name or IP address:";

      chop($asa=<>);

  }



  if (defined ($options{u})) {

      $user= $options{u};

  }

  else {

      print "Enter user name:";

      chop($user=<>);

  }



  if (defined ($options{w})) {

      $password= $options{w};

  }

  else {

      print "Enter password:";

      chop($password=<>);

  }

  if (defined ($options{p})) {

      $prompt= $options{p};

  }

  else {

      print "Enter ASA prompt:";

      chop($prompt=<>);

  }

  if (defined ($options{e})) {

      $enable = $options{e};

  }

  else {

      print "Enter enable password:";

      chop($enable=<>);

  }

  if (defined ($options{r})) {

     $restore = 1;

     $restore_file = $options{r};

  }

}
相关问题