#!/usr/bin/perl -w use strict; use Net::SSH::Perl; my $rcmd = " perl -e 'use strict; open(FD, filetochange.txt) or die \"can not open FD: $!\"; while () { if (/mon/ && /messages/) { print $_; #change line here } } close(FD); '"; my $ssh = Net::SSH::Perl->new('localhost', protocol=>('2,1')); $ssh->login('joe1','blabla'); my($stdout, $stderr, $exit) = $ssh->cmd("$rcmd"); print $stdout;