perl - how to run command in local or may be on remote machine -
i have setup hosta software application components installed .i have written script works if run same host (hosta) .the kind of operation doing truncate file ,read pattern file etc. in other cases components may not installed in same host .may component on hosta, component b , c on hostb.how write script handle such situation?
some of code performing action .now want if provide truncatelogfile(/path/to/file) should truncate regardless whether on local machine or remote machine.
sub truncatelogfile { $pathfile = shift; if (! -e $pathfile ){ warn "the [$pathfile] not exist\n" } ; system("truncate $pathfile --size 0") == 0 or warn "failed "; } sub folderclean { $self = shift; $pathfolder = shift; opendir (dir,$pathfolder) or warn "[!] failed open dir $!"; @files = map { $pathfolder . '/' . $_ } grep { !/^\.{1,2}$/ } readdir (dir); closedir (dir); print "\nno files present in $pathfolder\n" if !@files; foreach $file (@files) { print "removing $file...\n"; unlink "$file"; } }
if understanding of question correct, requirement run script 1 host want perform file operations on several hosts without knowing on host folder/file available. there many ways can handled.
one easy way have "system" command inside loop , each iteration should ssh respective host machine. pseudo code below.
my @hosts = (hosta, hostb, hostc, hostd); # assume have these 4 hosts. foreach (@hosts) { system("ssh $_ truncate $pathfile --size 0") == 0 or warn "failed "; }
the above code let program iterate through hosts , perform required operation. have used 'ssh' in example mention secured shell login. depending on typical login method, can use sqsh, rsh, etc. instead of ssh.
the above method works if same file available in multple hosts. hope ok requirement.
Comments
Post a Comment