Perl ssh operations in one session

In the following code, I am trying to transfer ssh to another computer and check if this file exists or not.
If the file exists, then I need to check whether the person accessing the file is the creator of this file, that is, its username is in the log file.
If present, then I need to delete this file. How can I achieve this with single sign-on ie, the user enters the password only once.

$user = $ENV{USER}; $il_check_cmd = "cat $shared_il_path/$il_name/info/.info_cat.log"; my $ssh_delete = Net::SSH::Perl->new($hostname, protocol => '1,2', debug => 0, interactive => 1) ; $ssh_delete->login($username, $password); ($stdout,$stderr,$exit) = $ssh_delete->cmd("$il_check_cmd"); if((defined $stderr) && ($stderr =~ /No such file or directory/)) { print "-E- $RUNCMD: \"$il_name\" you have entered does not exist in \"$shared_il_path\"!! !\n"; print "-E- $RUNCMD: or\n"; print "-E- $RUNCMD: \"$il_name\" does not contain \".info_cat.log\" file!!!\n"; print "-E- $RUNCMD: Exiting...\n"; exit; } @content = split(/ /,$stdout); chomp($user_e = shift(@content)); if($user_e =~ /\b$user\b/) { print "This is the user who created the file"; //then remove the $shared_il_path/$il_name/info/.info_cat.log } 
+4
source share
3 answers

Use Net::SSH::Expect instead. The linked page has sample code for an SSH session.

+2
source

What is the problem? Just send commands to the remote side via the $ssh_delete object.

And BTW, at present, there are more advanced modules for SSH like Net :: OpenSSH or Net :: SSH2 .

+1
source
 my ($stdout, $stderr, $exit) = $ssh->cmd(q{perl -e' my ($file_name) = @ARGV; ... Perl code that does what you want to do ... if (...some error...) { die("...error message...\n"); } ' filename}); if ($exit) { # An error occurred. die("Error: $stderr"); } 

Just use & '\'' & raquo; where do you usually use & lquo; ' & raquo ;.

+1
source

Source: https://habr.com/ru/post/1399728/


All Articles