0

How can I pass password to su command in shell script from another text file.

password.txt is like

password=tryme

shell script need to be ran by user gucq1

shell script is like

#!/bin/bash

cd /pstools/85419/jre/bin
java -Xms1024M -Xmx1024M -Dhttps.protocols=TLSv1.2 -Djdk.tls.client.protocols=TLSv1.2 -jar /app1/nohr/soft/85419/gucq1/cust/classes/SVC_TestS.jar 

I need to run this script with different id (gucq1) than I am logged in with (autoid).

I dont want to use SUDO as I dont have access to it.

Charles Duffy
  • 235,655
  • 34
  • 305
  • 356
  • 2
    https://stackoverflow.com/questions/233217/how-to-pass-the-password-to-su-sudo-ssh-without-overriding-the-tty is a superset of this question. The OP there accepted an answer that doesn't cover explicitly your aspect, but other answers do address your needs. That said, from a security perspective, you should *never ever* do this. – Charles Duffy Nov 28 '17 at 17:21
  • 1
    It's safer to generate a SSH keypair and install an `authorized_keys` entry in `gucq1/.ssh` that allows that key to only run the one specific script -- that way if the key is stolen the user can only run that script as `gucq1`, not have complete and total control of the account. – Charles Duffy Nov 28 '17 at 17:24
  • 1
    If you have `password.txt` and access to `su`, you can *give* yourself access to `sudo`. Make use of it. – chepner Nov 28 '17 at 17:27
  • @chepner, that's only if the OP can su to root; they're su'ing to a different user here. But that *does* let them set up `authorized_keys`. – Charles Duffy Nov 28 '17 at 20:09
  • Clearly I've forgotten how `su` works – chepner Nov 28 '17 at 20:23

2 Answers2

-1

https://unix.stackexchange.com/questions/113754/allow-user1-to-su-user2-without-password#115090

if you change your /etc/pam.d/su, you can use

su gucq1 -c <command>

without su prompting for password.

Dominic M
  • 166
  • 7
-1

Although I dont recommend it, you can use SUDO like this.

echo "mypassword" | sudo -S ./somescript ;

Regards!

Matias Barrios
  • 3,700
  • 2
  • 12
  • 33