0

My command is

echo "deb http://www.apache.org/dist/cassandra/debian 311x main" | sudo tee -a /etc/apt/sources.list.d/cassandra.sources.list

I want to pass my password user-password as an argument in the above command similar to the one shown below:

echo user-password | sudo -S apt-get update

My issue is that i don't know how to pass both the URL as well as the sudo password simultaneously.

Also i am ensuring that the password is not visible on the terminal as this command will be called by a python script which reads the password from a file (or from user)

AksTester
  • 119
  • To Close Voters and @Melebius this isn't a question about calling sudo and using the password as a parameter. The OP already has this part figured out. This is a question about piping to sudo tee with an extra argument -S and where does the echo <password> get placed in this chain? – WinEunuuchs2Unix May 17 '18 at 00:14
  • @WinEunuuchs2Unix I (as one of the close voters) don’t agree. I think the question is an XY problem and if the question is not a duplicate, OP should clarify their actual needs, particularly why editing sudoers is not acceptable in their case. I believe using plaintext password on the command line should be avoided. – Melebius May 17 '18 at 05:48
  • 2
    @Melebius Seeing Muru's answer below it's safe to say it's not a duplicate. Calling it an XY problem doesn't seem right unless you can get the same output as Muru's answer using a different process that doesn't involve a password. Yours or my believe about someone going about something the wrong way ie. "plain text password in CLI" isn't justification for closing ie "I don't like that" isn't a close vote option. I guess when all else fails that's what down voting is for. – WinEunuuchs2Unix May 17 '18 at 06:01
  • 1
    @ Aksh Using password in plain text on the command line or in scripts should be generally avoided, see https://askubuntu.com/q/147241/250300 for other options. What are you trying to achieve? Do you want to use that line in a script? @WinEunuuchs2Unix OK, you’re right I prematurely called this question a duplicate. Now I am requalifying it to “unclear what you’re asking” and therefore not retracting my close vote now. – Melebius May 17 '18 at 06:16

1 Answers1

0

Shuffle the command a bit, moving the echo for the source inside the sudo command:

echo password | sudo -S sh -c 'echo "deb http://www.apache.org/dist/cassandra/debian 311x main" >> /etc/apt/sources.list.d/cassandra.sources.list' 
muru
  • 197,895
  • 55
  • 485
  • 740
  • I wouldn’t recommend providing a password on the command line. See https://stackoverflow.com/q/3830823/711006 for details. – Melebius May 16 '18 at 12:52
  • 2
    Neither would I, but there are uses for it. – muru May 16 '18 at 13:09
  • Put a space before "echo"; otherwise it will be exposed to "history". And "ps" will always show it. A redirect should be the better method. – Rinzwind May 17 '18 at 06:48