- 论坛徽章:
- 0
|
大家好,我按照Y龙-鬼先生的代码,运行,如果带参数config,能正确的将主机和用户名,密码(有加密)写入config.log文档中.
如果是带参数shutdown, 从config.log我打印出 print $line;读取到的每行都是对的,但是无法打印出主机,用户名和密码,好像用split获取不到。提示use of uninitialized value $_ in spilt,use of uninitialized value $host in print,,,
错误的图片如附件
我的完整脚本如下,- #!/usr/bin/perl -w
- use strict;
- use warnings;
- use VMware::VIRuntime;
- use MIME::Base64;
- my %opts = (
- level => {
- type => "=s",
- help => "level [shutdown|config]",
- required => 1,
- default => "shutdown",
- },
- );
- Opts::add_options(%opts);
- Opts::parse();
- #Opts::validate();
- my $level = Opts::get_option('level');
- my $log_output_config = "config.log";
- if ($level eq "config") {
- open(FILE,">>$log_output_config");
- print "Enter your target host ip or hostname:\n";
- my $inputhost = <STDIN>;
- chomp($inputhost);
- exit if($inputhost=~/\bexit\b/);
- print(FILE "$inputhost\t\t");
-
- print "Enter your host username:\n";
- my $inputusername = <STDIN>;
- chomp($inputusername);
- exit if($inputusername=~/\bexit\b/);
- print(FILE "$inputusername\t\t");
-
- system "stty -echo";
- print "Enter your host password:\n";
- my $inputpasswd = <STDIN>;
- chomp($inputpasswd);
- exit if($inputpasswd=~/\bexit\b/);
- my $encryppasswd = encode_base64($inputpasswd);
- system "stty echo";
- print(FILE $encryppasswd);
- close(FILE);
- exit;
- }
- &shutdownHost;
- # Shutdown target host
- sub shutdownHost {
- my ($log_output_config) = "config.log";
- my $HANDLE;
- open (HANDLE, $log_output_config) or die("ERROR: Can not locate \$log_output_config\" input file!\n");
- my @lines = <HANDLE>;
- my @errorArray;
- my $line_no = 0;
- close(HANDLE);
- foreach my $line (@lines) {
- $line_no++;
- &trim($line);
- print $line;
- if($line) {
- if($line =~ /^\s*:|:\s*$/){
- print "Error in Parsing File at line: $line_no\n";
- print "Continuing to the next line\n";
- next;
- }
- (my $host, my $user, my $passwd) = split($line);
- my $decryppasswd = decode_base64($passwd);
- print $host;
- print $user;
- print $decryppasswd;
- my $result_output = `./ghettoShutdown.pl --server $host --username $user --password $decryppasswd`;
-
-
- }
- }
- }
- sub trim($)
- {
- my $string = shift;
- $string =~ s/^\s+|\s+$//g;
- return $string;
- }
复制代码 |
|