简体   繁体   中英

Perl SSH to remote server and check file exists

I have developed a script which should login into remote server and do certain operation.

In remote server it will check for sample_file.txt file exists or not, if YES then it should create create_user_file.txt file in home path with certain set of data.

The thing here is I am able to do ssh to remote machine though below script but unable to check for the files since I don't have idea how we can open the remote session and do all the operation.

Below is my code:

#!/usr/bin/perl 

use Net::OpenSSH;

.
.
.

foreach my $ip( @list_of_ips){ 
    print "Connecting to $ip...\n";
    my $ssh = OpenSSH_Connection( $ip, $user, $passwd );
    print "Connected!\n";

    $dir = "/remote/server/home/path/";
    $file = $dir."sample_file.txt";

    if (-e $file){ #if sample_file.txt exists then create user file
        open(my $fh, '>', $dir."create_user_file.txt") || die "Couldn't open file $!";;
        print $fh "ID=123&PW=Passw0rd";
        close $fh;
    }

    last if($ssh); #if connection is success no need to login into another server
    $ssh->close();
}

sub OpenSSH_Connection {
    my ( $host, $user, $passwd ) = @_;

    my $ssh = Net::OpenSSH->new("$user:$passwd\@$host");
    $ssh->error and die "Couldn't establish SSH connection: ". $ssh->error;

    return $ssh;
}

Here in the code, below part of code should be executed in the remote server. But its been checking for a file from where I am executing this script.

if (-e $file){ #if sample_file.txt exists then create user file
        open(my $fh, '>', $dir."create_user_file.txt") || die "Couldn't open file $!";;
        print $fh "ID=123&PW=Passw0rd";
        close $fh;
}

How can I keep open the remote session of the server and execute above code, or do we have any Perl module which could do this kind of operation from local server. TIA.

Your assumption that Net::SSH will make remote resources available locally is incorrect.

SSH creates encrypted channel to remote system and drops you either into shell, or runs remote command (for example perl/shell script), or allows to copy/retrieve a file.

You can open SSH channel into remote shell and run commands as on local computer, but if you want to check if file exists on remote system then you have to communicate it through remote shell or perl script.

Probably what you are after is SSH FS which may make remote files available locally.

I must make note that users on local and remote system can have different id on system level and it might be not what you want (manipulate files of different user). This approach may work well if your local and remote id is the same and belongs to you.

I guess that most easy way would be write perl script on remote system and pass some arguments to it. The script on remote system will do rest part of the job.

In such situation you could use Net::SSH2 to establish connection into remote shell and then run remote perl script with parameters.

To get a better idea how it works without creating long post I refer you to PerlMonks Net::SSH2 demo .

Other more difficult way would be writing enough perl code to establish connection with remote system over SSH2 protocol to remote perl script with capability to 'translate' your local commands

$chan->command($argument)

into remote perl code blocks to perform particular task.

Net::SSH2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM