'How to pass passphrase of ssh in bash script? [duplicate]

I am discovering the bash scripting. I need to write a bash script to automatically connects my remote server with ssh. I am using MACOSX.

I were able to do with sudo as below

echo <root_pass> | sudo -S ls

However all my attempts were unsuccessful to pass the passphrase. I have tried these below already:

echo <my_passphrase> | sudo ssh -i /Users/path_to_ssh_public_key/ssh <my_username>@<remote_ip> 

sudo ssh -i /Users/path_to_ssh_public_key/ssh <my_username>@<remote_ip> <<< echo <my_passphrase>

The command uses "-i" to get public key from a custom folder

Any help is welcome...

EDIT: I want to fully control the terminal outputs and inputs. I don't want to use sshpass or declare any variables to the shell.



Solution 1:[1]

As others mentioned in comments, you can use sshpass like so:

sshpass -p !4u2tryhack ssh [email protected]

But using .ssh/config file is much more convenient.

Sample

Host fedora
    Hostname 192.168.1.60
    Port 22
    User shm

With which I can do

ssh fedora

And since it does not have any key - it uses the default id_rsa.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Shakiba Moshiri