SaloT_T
SaloT_T

Reputation: 1

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.

Upvotes: 0

Views: 223

Answers (1)

ikegami
ikegami

Reputation: 385809

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.

Upvotes: 1

Related Questions