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

Hello Monks,
I am connecting to the remote server using Telnet module and want to grep the content of file in particular directory.But it is giving an error,See the code below,
$checkfile = "back-20.bak"; $direcory ="/sapp/backup"; $contcheck = "Backup taken for this file"; $telnet->cmd("opendir dhandle,$directory"); @dots = $telnet->cmd("grep { /^$checkfile/ } readdir(dhandle)"); print"Files=> @dots\n"; foreach $filename (@dots) { chomp($filename); @filecont = $telnet->cmd("grep -i $contcheck"); foreach $line (@filecont) { if($line =~ /$contcheck/){ $status = 'FAIL'; }else{ $status ='PASS'; } } }
For the above code,if back-20.bak file is availabe in the remote machine in the location /sapp/backup and if the content of the file is having data like "Backup taken for this file" I have to assign status as pass or if the content is not available in that file status is assigned to fail.
Monks let me know where I am doing wrong?
I am getting the output as,
Files=> ksh: syntax error: `(' unexpected
Thanks.

Replies are listed 'Best First'.
Re: Remote server file access issue
by Sidhekin (Priest) on Jun 29, 2006 at 12:34 UTC

    Unless the remote server gives you a Perl shell (which is to say, quite, nay utterly, unlikely), the commands you pass through telnet had better be ordinary shell commands -- not Perl statements. ;-)

    print "Just another Perl ${\(trickster and hacker)},"
    The Sidhekin proves Sidhe did it!

    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Remote server file access issue
by shmem (Chancellor) on Jun 29, 2006 at 14:07 UTC
    As Sidhekin and I have pointed out, you were trying to feed perl commands into ksh. Don't do that, it chokes. Wrapping perl commands into ksh syntax is tedious, so you would it find nice to talk directly to perl, I guess. Here's a small script that does the trick. perl will read one line from STDIN at a time, eval it and print a prompt:
    #!/usr/bin/perl $| = 1; # make output unbuffered prompt(); while(<STDIN>) { # read 1 line... eval $_; # ...doit... warn $@ if $@; # ...complain if error prompt(); # ...and print prompt; } sub prompt { print "\n;# perl > " } __END__

    Place that script in your home directory on the remote server, and call perl to execute it:

    $telnet->cmd('perl perlshell.pl');
    Now you can talk to perl.
    print $telnet->cmd('print scalar localtime(time)'); Thu Jun 29 15:58:52 2006 print $telnet->cmd('print $^X'); /usr/bin/perl
    Note that to logout you have to $telnet->cmd('exit') twice, one time to terminate perl, the next to terminate your login shell.

    update: One exit alone leaves you again at the ksh prompt. Note also that you cannot issue multiline statements, as the remote perl interpreter reads one line at a time.

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
Re: Remote server file access issue
by dsheroh (Monsignor) on Jun 29, 2006 at 15:53 UTC
    In this case, you're doing some things which are actually pretty simple shell stuff. While [id://shmem] is correct that it can get tedious translating Perl to shell, his suggestion for creating a Perl shell seems a little like overkill here.

    You should be able to get this to work as you intended by simply connecting to the remote host, then executing

    @filecont = $telnet->cmd("grep -i '$contcheck' $directory/$checkfile") +; if (@filecont) { $status = 'FAIL'; } else { $status ='PASS'; }
    No directory handles or evaluating each line of the file necessary, just ask the remote host to run the grep for you and react based on whether it found any matches or not.
A reply falls below the community's threshold of quality. You may see it by logging in.