How to pass password in bash without prompting to ask to type in?

0

What I wanted to do is: run a bash script from my local terminal, to run a jar file in an EC2 instance. But in order to run the script, I'll have to first log in with .pem credentials, and then do su admin and type admin password to gain run access. The command I've figured out so far is like this:

ssh -i ~/.ssh/my_credentials.pem -t ${EC2IP} 'su admin' "ADMIN_PASSWORD" 'cd /tmp' 'java -jar my-program.jar'

But it's still prompting me to type the ADMIN_PASSWORD, and then after that, my terminal will be logged into that EC2 machine, which is not what I wanted, I just want to finish all of the commands, and then continue with another EC2 instance.

How could I achieve that please? Thanks a lot!

Fisher Coder

Posted 2017-03-08T19:51:11.233

Reputation: 111

Why are you using su here at all? It's much easier to configure sudo for passwordless access, or configuring SSH to provide your private key direct access to the account you're trying to su to. – Charles Duffy – 2017-03-08T22:26:12.917

1

You seem to assume that the strings you paste are supplied as standard input to ssh but this is not at all the case. Maybe see also http://stackoverflow.com/questions/37586811/pass-commands-as-input-to-another-command-su-ssh-sh-etc

– tripleee – 2017-03-08T22:52:12.080

1

This might help: Allow user1 to “su - user2” without password

– Cyrus – 2017-03-08T20:54:10.490

Answers

0

there is no proper solution to your problem. Maybe you can change some security rules in /etc/pam.d/su. But I am not sure if and how.

I know why you cant use ssh in combination with su...
su is intended to run on the server terminal, or with other words:
su needs the correct TTY to communicate with.


Passing a password to su also doesnt work.
Most programms that use a password have a option to put the passowrd in it,
unfortunately su use promt only.

A NOT recommended method would be this.


You could avoid using su by connecting via ssh and username admin,
or giving the rights to the user that you were using in the first place.

Hope this gets you going.

suleiman

Posted 2017-03-08T19:51:11.233

Reputation: 103

Not quite accurate that there exists no possible approach -- with expect for instance, one has an emulated TTY that can be scripted. – Charles Duffy – 2017-03-08T22:24:53.897

Definitely agreed that expect is the Wrong Thing -- was just making a point that the original language speaking in terms of *possibilities" was inaccurate. – Charles Duffy – 2017-03-08T22:47:15.587

oops, ok. edited my post again. thx for the hint. – None – 2017-03-08T22:54:21.517

updated my post – None – 2017-03-08T22:25:29.027

0

A. Don't do this. Use sudo instead. You'll probably need to comment out the following line from /etc/sudoers on the target:

Defaults    requiretty

B. Assuming that you're going to do this anyway, use expect to do it for you: http://www.tcl.tk/man/expect5.31/expect.1.html

But, seriously, don't do it.

Jim Parks

Posted 2017-03-08T19:51:11.233

Reputation: