use Net::OpenSSH; my $public_key = "..."; my $cmd1 = <>\$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;