Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Removing files from remote server

by Bindo (Acolyte)
on Apr 22, 2013 at 13:02 UTC ( [id://1029862]=perlquestion: print w/replies, xml ) Need Help??

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

Greetings good people. I want to write a script to delete a set of files from one or more remote servers. The ip address of the servers are mentioned in a file called "machines.txt". Note that public key ssh is enabled from source server to the remote hosts.I attempted in a certain direction but no luck. Following is the script.

my @LIBS = ("lib1", "lib2", "lib3"); my $MACHINES_PATH = $ENV{'SYSTEM_HOME'}; my $LIBS_PATH = $MACHINES_PATH."/testlib"; my $MACHINES_LIST = $MACHINES_PATH."/machines.txt"; my $SSH_USER = "abc"; my $SSH_CMD = "/usr/bin/ssh"; open (MYFILE,"<$MACHINES_LIST") or die "Could not open the file $MACHI +NES_LIST, $!"; my @HOSTS = <MYFILE>; foreach my $BOX (@HOSTS) { chomp $BOX; `$SSH_CMD $SSH_USER\@$BOX -o BatchMode=yes ConnectionAttempts=5 Co +nnectTimeout=5 \"cd $LIBS_PATH \&\& files_DEL(@LIBS)"`; } sub files_DEL { my @LIB_FILES = shift; foreach my $lib (@LIB_FILES) { print "deleting file $lib\n"; system("rm -rf $lib"); } }

I'm sure that the sub "files_DEL" has problems. Following is the errors that are returned anyways.

xx@sbi-126:/x02/abc>./filedel.pl bash: -c: line 0: syntax error near unexpected token `libcudart.so.4.1 +.28' bash: -c: line 0: `ConnectionAttempts=5 ConnectTimeout=5 cd /x02/abc/t +estlib && files_DEL(lib1 lib2 lib3)' ssh: Could not resolve hostname : Name or service not known xx@sbi-126:/x02/abc>

Please can someone help. Im still a newbie so appreciate if you could edit my code so i'd understand better. Many thanks in advance

Replies are listed 'Best First'.
Re: Removing files from remote server
by salva (Canon) on Apr 22, 2013 at 13:40 UTC
    You can't pass ssh the name of a Perl subroutine from your script and expect it to be executed on the remote machine. It just doesn't work that way!

    ssh can only run commands and shell statements.

Re: Removing files from remote server
by CountOrlok (Friar) on Apr 22, 2013 at 13:52 UTC
    I would recommend debugging your script by first printing the ssh statement instead of putting it in backticks. This way you can verify what will be sent and once you are satisfied with that, you can put it in backticks. You will note that you are sending a bad command to the other server. You will also then figure out why you are getting a "could not resolve hostname" error.

    There are a couple of issues with your files_DEL subroutine. my @LIB_FILES = shift is not going to do what you want. Remove that line and use @_ instead of @LIB_FILES. Running system inside your subroutine will run that command on your local computer. What you really intend to do is return a string like " && rm -fr lib1 && rm -fr lib2" to the caller which in turn will append it to the ssh command.
Re: Removing files from remote server
by pvaldes (Chaplain) on Apr 22, 2013 at 14:03 UTC

    I suspect also that unlink $lib is more portable that system("rm -rf $lib");

    nah, forget it, Salva and CountOrlok had explained much better the idea

    mmh -> my $HOSTS = <MYFILE>; myfile is a filehandle (scalar) if you want an array from a file (my @HOSTS = <MYFILE>;) use tie. You should also to close the filehandle when you finish

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1029862]
Front-paged by Arunbear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (4)
As of 2024-03-29 07:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found