Coder94
Coder94

Reputation: 1025

How to automatically ssh into server and su with password using bash script?

I need to ssh into a server and su with password within that server and then ssh into another server from there. I am not able to su with password after I ssh into my first server via bash script. Normally what I do in terminal is,

$ ssh server1

logged in to server1$ su user

password: ******

switched to corresponding user$ ssh server2

How can I automate this process by switching user with password in bash?

Upvotes: 0

Views: 697

Answers (2)

Eran Ben-Natan
Eran Ben-Natan

Reputation: 2615

Can sudo without password works for you?

ssh abc@$xyz "
    echo '
        set -vxe
        # Your script here
    ' | sudo /bin/bash"

Upvotes: 1

Matt Taylor
Matt Taylor

Reputation: 219

Have you tried this?

sshpass -f <(printf '%s\n' "yourpassword") ssh username@server "\"sshpass -f <(printf '%s\n' "yourpassword") ssh username@server"

Upvotes: 1

Related Questions