0

I am currently working on a java application to get "su -" access to a Linux host. I already know the password, but I want to be able to execute the "su -" command from a bash script and have the command saved to the history. I know that it is bad practice and a security risk, but it is apart of the design of my application. I have tried many variations though, and although people have said they work, I have yet to find one that works.

Tried actions:

  • echo "password" | su -
  • su <user> -p <password>
  • sudo -S su

I have tried many more but non of them seem to work. All help is appreciated. Thanks
P.S I am using the JSCH library, so I am also looking into that as a possible answer.

oguz ismail
  • 1
  • 16
  • 47
  • 69
tharriott
  • 101
  • 2
  • 11
  • 1
    "I know that it is a bad practice and a security risk, but it is a part of the design of my application" -- have you asked a separate question about how to avoid that design? – Charles Duffy Apr 15 '19 at 15:19
  • Hello Charles, Yes I have. Though like I said prior, for my application to work I need the password to print to the history. – tharriott Apr 15 '19 at 15:27
  • Could you link to that question? – Charles Duffy Apr 15 '19 at 15:42
  • Oh my bad, I didn't read your comment thoroughly. No I did not ask a question about how to avoid the design. – tharriott Apr 15 '19 at 15:50
  • 1
    If you _really_ need to do this, the classic way to do it is to use [expect](https://en.wikipedia.org/wiki/Expect). See [Trouble logging in user using su and expect script](https://stackoverflow.com/q/29174337/4154375) for example code. Another better option (almost all other options are better) may be to use passwordless sudo. See [How to setup passwordless `sudo` on Linux?](https://serverfault.com/q/160581/476049). – pjh Apr 15 '19 at 18:12
  • See [Automatically input a password when a bash script is run](https://stackoverflow.com/q/10606958/4154375) and [How to provide password to a command that prompts for one in bash?](https://stackoverflow.com/q/4857702/4154375). – pjh Apr 15 '19 at 19:06

0 Answers0