in reply to Re: Send password in Net::SSH::Expect
in thread Send password in Net::SSH::Expect

Unfortunately, this is a netscaler image without that function: -bash: ssh-copy-id: command not found I have been trying something a bit different though, and am stuck with this system call:
system("ssh -q $ssh_user\@$bkp_destination 'sed -i \"s#.*$hostname#$sk +ey#g\" /tmp/test.txt'");
The idea here is to replace the entry in /tmp/test.txt containing the $hostname with the $skey string. It works from the shell itself, but fails from the script, so I am pretty sure it is a escape issue.

Replies are listed 'Best First'.
Re^3: Send password in Net::SSH::Expect
by gafaman (Novice) on Jan 23, 2020 at 08:51 UTC
    I ran ssh with -v and the problem is that it is sending only a bit of the command:
    system("ssh -qv $ssh_user\@$bkp_destination 'sed -i \"s#.*$hostname#$s +key#g\" /tmp/test.txt'"); Variables: $hostname = "HOSTA"; $skey = "ssh-rsa AAAAB3NzaC1yc2 root@HOSTA"; SSH output: ... debug1: Sending env LANG = en_GB.UTF-8 #ssh-rsa AAAAB3NzaC1yc2 root@HOSTA#g" /tmp/test.txt ...
    So it is not sending the entire -- sed -i \"s#.*$hostname -- bit.

      I would first construct the complete command and use the same string for debugging and for running the command:

      my $cmd = "ssh -qv $ssh_user\@$bkp_destination 'sed -i \"s#.*$hostname +#$skey#g\" /tmp/test.txt'"; warn "Launching [[$cmd]]"; system($cmd) == 0 or die "Couldn't launch ssh connection via [[$cmd]]: $? / $!";

      That way, the command you use for debugging and the command you run cannot deviate.

      Maybe using the # char (shell comment indicator) somewhere messes up your shell quoting. Even for readability, I would construct the shell string differently:

      my $cmd = qq(ssh -qv $ssh_user\@$bkp_destination 'sed -i "s#.*$hostnam +e#$skey#g" /tmp/test.txt');
        I updated the script with the construction you suggested but the output was the same. I also noticed that there was an "I" missing from the sed statement and added it. Script:
        my $cmd = qq(ssh -qv $ssh_user\@$bkp_destination 'sed -i "s#.*$hostnam +e#$skey#gI" /tmp/test.txt'); warn "Launching [[$cmd]]"; system($cmd) == 0 or die "Couldn't launch ssh connection via +[[$cmd]]: $? / $!";
        Output:
        debug1: pledge: network debug1: Sending environment. debug1: Sending env LANG = en_GB.UTF-8 #ssh-rsa AAAAB3NzaC1yc2 root@HOSTA#g" /tmp/test.txt debug1: client_input_channel_req: channel 0 rtype exit-status reply 0 debug1: client_input_channel_req: channel 0 rtype eow@openssh.com repl +y 0 debug1: channel 0: free: client-session, nchannels 1 Transferred: sent 3312, received 2392 bytes, in 0.1 seconds Bytes per second: sent 51671.7, received 37318.5 debug1: Exit status 0
        From the $bkp_destination host:
        user@bkphost:/tmp$ cat test.txt As123312123 asdasdadasqsef3w4rfweweweffwerfew As123312123 asdasdadasqsef3w4rfweweweffwerfew i12311231231231 HOSTA As123312123 asdasdadasqsef3w4rfweweweffwerfew user@bkphost:/tmp$ echo $h HOSTA user@bkphost:/tmp$ echo $k ssh-rsa AAAAB3NzaC1yc2 root@HOSTA user@bkphost:/tmp$ sed -i "s#.*$h#$k#gI" test.txt user@bkphost:/tmp$ cat test.txt As123312123 asdasdadasqsef3w4rfweweweffwerfew As123312123 asdasdadasqsef3w4rfweweweffwerfew ssh-rsa AAAAB3NzaC1yc2 root@HOSTA As123312123 asdasdadasqsef3w4rfweweweffwerfew