0

In my script: 1. I have to do ssh to a machine by username and password 2. Than I have to do Su - user, it prompts for password 3. Than I have to run my command i.e ls .

Earlier, There was no step2 and I was happily able to use sshpass & fire my queries through my script in a one liner command. something like this. sshpass -p pass user@host "command or ls"

But now I have one additional step of doing su after ssh. So Can someone please help me in understanding , how can I use all 3 together i.e sshpass, su and command in one line query.

Thanks

Tajinder Singh
  • 205
  • 1
  • 5
  • 12

1 Answers1

0

Assuming you only want to change users for that one command, you need the "-c" flag.

su - user -c "[command]"

Nesting the quotes can get a little complicated, but in this case you just need to escape the inner ones.

su - user -c "sshpass -p pass user@host \"command or ls\""
ScottyC
  • 1,467
  • 1
  • 15
  • 22
  • I will explain how I do it manually. I am not sure if your solution work or not. cmd1 : ssh hostname -l user it prompts me for the password. I enter it. hostname> su - user It again prompts for the password. then I run my command here , like ls. looks like you are first doing su and then sshpass, and your cmd have no place to enter the su password. – Tajinder Singh Feb 26 '16 at 02:00
  • @TajinderSingh - why would a user need to su to himself? Remember `ssh -l user host` logs in as the user `user`. – alvits Feb 26 '16 at 02:22
  • Not to himself. Commands I run are 1. sshpass -p abc123 ssh alpha@tyco@company.com 2. su - beta and than enter password for this user beta ***** 3. than I run my actual command. forexample cat log.txt I first have to do ssh with use alpha, and than have to do su to user beta and than have to run command as beta. Is it possible in one liner ? Thanks for your time :) – Tajinder Singh Feb 26 '16 at 02:36
  • @TajinderSingh - what's stopping you from running `sshpass -p betapass beta@tyco.company.com`? Clearly you are running command as user `beta` but not as user `alpha` and user `beta` successively. – alvits Feb 26 '16 at 02:38
  • Approach1 step1: I tried this sshpass -p betapass beta@tyco.company.com "command" I get error - "command" command not found. Approach2 step1: But when I do this sshpass -p betapass beta@tyco.company.com step2: [betauser@tyco.com ~]$ command ...... It works. Approach1 is not working and approach2 works, but I want something in one liner, because I want to run this one line command from my code. with in my code, I can't do this 2 steps process mentioned in approach2. – Tajinder Singh Feb 26 '16 at 02:45
  • @TajinderSingh - The difference between your approaches is the environment, specifically the `PATH`. You can simply source the environment files of user `beta`. Try `sshpass -p betapass beta@tyco.company.com "source .bash_profile; command"`. – alvits Feb 26 '16 at 02:47
  • I tried it now andI got this ....... bash: command : permission denied. Looks like it tried to run , but doesn't have permission, any idea what I can do ? I think we are close :) Thanks – Tajinder Singh Feb 26 '16 at 02:55