0

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?

2
  • This seems like an odd setup to begin with. Why can't you just login to user2@server1? If you can't, for whatever reason, then you can use the .bashrc to accomplish your task. Commented Jul 9, 2019 at 13:24
  • I need to login to user @server1 using a bash script but am not sure about doing that! Its raising issues like "su must be run from another terminal" Commented Jul 9, 2019 at 13:43

2 Answers 2

1

Have you tried this?

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

Sign up to request clarification or add additional context in comments.

6 Comments

Maybe something along these lines.... I would test but I haven't got any servers that I can use atm.
Thanks but how can I pass the password?
What about using sshpass? sshpass -p 'YourPassword' ssh user@host
So, I tried sshpass -p 'YourPassword' ssh user@host and then "su user" its not it is asking password in the terminal
ssh username@server1 "su -c \"sshpass -p 'YourPassword' ssh user@server2"su -c"" is there any syntax issues? is is showing unexpected EOF
|
1

Can sudo without password works for you?

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.