如何使用Perl在while循环中切换文件中的不同字符串

如何使用Perl在while循环中切换文件中的不同字符串,perl,cmd,Perl,Cmd,我有一个问题,可能很简单,但我还没有找到答案 我有一个字符串文件(每个字符串在一个单独的行中,我需要在cmd中使用每个字符串(行) 我使用的是“while”循环,但我不知道如何将每个字符串附加到循环中 c:\putty\putty.exe -ssh "root@XXX.XXX.XXX.XXX" -pw "password" -m "c:\putty\putty.txt" 当XXX.XXX.XXX是循环中需要更改的字符串时,我需要运行以下命令 c:\putty\putty.exe -ssh "r

我有一个问题,可能很简单,但我还没有找到答案

我有一个字符串文件(每个字符串在一个单独的行中,我需要在cmd中使用每个字符串(行)

我使用的是“while”循环,但我不知道如何将每个字符串附加到循环中

c:\putty\putty.exe -ssh "root@XXX.XXX.XXX.XXX" -pw "password" -m "c:\putty\putty.txt"
当XXX.XXX.XXX是循环中需要更改的字符串时,我需要运行以下命令

c:\putty\putty.exe -ssh "root@XXX.XXX.XXX.XXX" -pw "password" -m "c:\putty\putty.txt"
试试这个:

#!/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;
!/usr/bin/perl
使用警告;
严格使用;

打开我的$fh,“更详细的版本是:

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($!).
    }
}
使用严格;
使用警告;
my$file=“file\u of_strings.file”;
#在关闭句柄之前打开文件并读取内容

open(FH),它是使用lexicalfilehandle$FH的良好实践,如果文件大小很大,将整个文件内容放入内存不是一个好主意。