bowei_99 has asked for the wisdom of the Perl Monks concerning the following question:

Hi all, I'm trying to rsh in to a bunch of machines and use perl to do a search and replace of some text. It doesn't give any error message(s), but it also doesn't change the file on either the local box or the machine I'm rshing to.

This runs and *seems* successful by using normal rsh, but actually changes nothing. Note that it failed using the first two authentication methods, but is OK the last time. This is consistent with other commands with rsh that work.

#/usr/kerberos/bin/rsh remote-host /usr/bin/perl -p -i -e 's/^#\s+NETDUMPADDR=/#\+NETDUMPADDR=\nNETDUMPADDR=ip-address-here\n/g' + /etc/sysconfig/netdump connect to address ip-address-here: Connection refused Trying krb4 rsh... connect to address ip-address-here: Connection refused trying normal rsh (/usr/bin/rsh) #

even though this works, i.e. actually replaces text, on the local system:

#/usr/bin/perl -p -i -e 's/^# NETDUMPADDR=/# NETDUMPADDR=\nNETDUMPADDR +=ip-address-here\n/g' netdump

I suppose I could rcp from the remote host to the local host, change it, then copy it back, but that seems kinda kludgey. Anybody know what I'm missing here?

-- Burvil

Replies are listed 'Best First'.
Re: Search and replace on remote system using rsh
by Argel (Prior) on May 18, 2006 at 00:43 UTC
    Try and verify the arguments to perl are making to to the remote system intact. For example, make that substitute operation a string and then print it out. Is it the same version of Perl on the remote system?

    You might want to take a look at the File::Remote module. I have never used it but it sounds like it fits your situation.

    And the obligatory: You should be using SSH!!

Re: Search and replace on remote system using rsh
by graff (Chancellor) on May 18, 2006 at 04:04 UTC
    From your description, it sounds like perl is installed on each of the remote hosts. So how about if you create the intended filter script as a file on your local host first (and debug it on your local host, to be sure), then for each of your remote hosts:
    - scp filter.pl remote.host:/your/home/filter.pl - ssh remote.host /your/home/filter.pl /etc/sysconfig/netdump
    Note that you can include the "-i" and "-p" flags on the shebang line of the script file, so the script itself can still be just a one-liner (after the shebang line).

    If the idea is that each remote host needs a different value in "ip-address-here", you just need a script on the local host that loops over the list of remote hosts, and for each one:

    - create the version of the script for this host as "tmp.pl" - scp tmp.pl remote.host:/your/path/tmp.pl - ssh remote.host /your/path/tmp.pl /etc/sysconfig/netdump - for neatness, tmp.pl could include a third line (after the shebang and the s/.../.../): END { unlink /your/path/tmp.pl }
    This will avoid the problem your having with insufficient quoting/escaping of shell metacharacters needed for the command-line perl script to work as intended in the remote shell.
Re: Search and replace on remote system using rsh
by jesuashok (Curate) on May 18, 2006 at 03:41 UTC