简体   繁体   中英

Can I echo local variable via ssh into remote file?

I try to write variable in end of file, but write it into STDOUT in local server some code:

$num = <STDIN>
my $cmd = "echo $dep_keys{$num} >> /root/1";
#$ssh->system({stdin_data =>$dep_keys{$num} },"echo >> /root/1");
#$ssh->error die "Couldn't establish SSH connection: ". $ssh->error;
#$ssh->system("echo $dep_keys{$num} >> /root/1");
$ssh->system($cmd);

I expect that the file will contain new line in the end of file.

use String::ShellQuote qw( shell_quote );

defined( my $num = <STDIN> )
   or die("Invalid input");

chomp($num);

defined($dep_keys{$num})
   or die("Invalid input");

my $cmd = shell_quote('printf', '%s\n', $dep_keys{$num}) . ' >>/root/1';
$ssh->system($cmd);

Fixes:

  • Checks for EOF and other invalid inputs.
  • Removes trailing line feed from $num (if any).
  • Properly convert the text from the hash element into a shell literal.
  • Avoids echo because echo makes impossible to output some strings.

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