我有一个问题,这可能很简单,但我还没有找到答案。
我有一个字符串文件(每个字符串都在一个单独的行中,我需要在cmd中使用每个字符串(行)。
我正在使用' while'循环,但我不知道如何将每个字符串附加到循环。
当XXX.XXX.XXX是需要在循环中更改的字符串时,我需要运行以下命令。
c:\putty\putty.exe -ssh "root@XXX.XXX.XXX.XXX" -pw "password" -m "c:\putty\putty.txt"
答案 0 :(得分:1)
试试这个:
#!/usr/bin/perl
use warnings;
use strict;
open my $fh, "<", "file.txt" or die $!;
while (my $line = <$fh>)
{
chomp $line;
#Here you can replace 'XXX.XXX.XXX' with '$line'. Modify below line as per your requirement.
my $cmd = `c:\\putty\\putty.exe -ssh "root\@$line" -pw "password" -m "c:\\putty\\putty.txt"`;
}
close $fh;
答案 1 :(得分:1)
更详细的版本将是:
use strict;
use warnings;
my $file = "file_of_strings.file";
# Open file and read contents before closing handle
open(FH, "<", $file) or die "Unable to open \"$file\" $!";
chomp(my @users = <FH>);
close(FH);
for my $user (@users) {
# Frame remote command
my $cmd = "c:\putty\putty.exe -ssh 'root\@${user}' -pw 'password' -m 'c:\putty\putty.txt'";
if (system $cmd != 0) { # system command returns 0 on successful execution
print "Successfully executed command: $cmd\n";
} else {
print "Failed to execute command: $cmd exited $? $!\n"; # Better to log the exit code ($?) and error message, if any($!).
}
}