-1

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.

  • 2
    ssh reads the passphrase from `/dev/tty` rather than `stdin` so `echo ... | ssh ...` would not work here. you can use tools like [tag:expect] (for Tcl), [tag:pexpect] (for Python) or my [sexpect](https://github.com/clarkwang/sexpect/) (for shells). – sexpect - Expect for Shells May 07 '22 at 11:02
  • 4
    Or you can use `sshpass`, which was designed for exactly this purpose. Or better yet, stop using passwords and configure ssh key-based authentication. – larsks May 07 '22 at 13:01
  • I see, thanks for info @larsks . So let me know if I can feed dev/tty in c program or python or even bash script as providing the password. – MUSAB AKICI May 08 '22 at 13:06
  • @cachius no it is not, actually I have seen it before, but my intention is to take control the terminal fully in a bash script. But the issue you mentioned suggests me to use sshpass or some other extra variable declaring to access ssh. – MUSAB AKICI May 08 '22 at 13:13
  • BTW, I wonder why people downgrade the question immediately if they do not like it :D rather than asking why he asked this question while there are questions similar – MUSAB AKICI May 08 '22 at 13:16
  • For your Info After voting one can only change the vote after the post was edited. So good you did – cachius May 08 '22 at 15:01
  • Not my downvote, but this type of question is very common, and your question contains no indication that you searched existing questions before asking. – tripleee May 09 '22 at 05:54

1 Answers1

1

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

sshpass -p !4u2tryhack ssh username@host.example.com

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.

Shakiba Moshiri
  • 21,040
  • 2
  • 34
  • 44