in reply to Quoting Solutions for Nested SSH Commands?

Don't do it by hand, make a sub to do the quoting... or move to Net::OpenSSH that already has a shell quoting method (or just copy the relevant code from there ;-)
use Net::OpenSSH; my $public_key = "..."; my $cmd1 = <<EOC; uname -a mkdir \$HOME/.ssh chmod go-w \$HOME \$HOME/.ssh echo "$public_key" >>\$HOME/.ssh/authorized_keys EOC $cmd1 =~ s/\n/;/g; $cmd1 = Net::OpenSSH->shell_quote($cmd1); my $cmd2 = '/usr/bin/ssh -t -l usr machineD ' . $cmd1; $cmd2 = Net::OpenSSH->shell_quote($cmd2); my $cmd3 = '/usr/bin/ssh -t -i $HOME/.ssh/LeadRNCkey -l usr machineC ' + . $cmd2; system $cmd3;