15

I have the following

#!/bin/bash

USER='scott'
PASS='tiger'

ssh -t $USER@server006.web.com "sudo su - http" 

This Works, but I was trying to get it to run a script afterwards, and if I do, using -c or <

The script does a grep like this:

grep -i "Exception:" /opt/local/server/logs/exceptions.log | grep -e "|*-*-*:*:*,*|" | tail -1 | awk -F'|' '{print $2}' >> log.log

This also works fine on it's own, but I need to be http to do it.

I cannot SCP the output of the script back to server001 either, so I'm stuck here,

Any ideas would be relay appreciated. Ben

Ben Coughlan
  • 555
  • 3
  • 12
  • 23

3 Answers3

12

Try

ssh -t $USER@server006.web.com 'sudo -u http grep -i "Exception:" /opt/local/server/logs/exceptions.log | grep -e "|*-*-*:*:*,*|" | tail -1 | awk -F"|" "{print $2}" >> log.log'

Sudo already runs the command as a different user to there's no need to su again.

Only reason to do sudo su is to have a fast way to start a new shell with another user.

Patrik Kullman
  • 721
  • 5
  • 6
  • Thanks for the reply, I tried this and got ben@server006.server.com's password: [sudo] password for ben: Sorry, user benis not allowed to execute '/bin/grep -i Exception:...... as http on server006.server.com – Ben Coughlan Apr 02 '14 at 13:58
  • 1
    Well the error pretty much says it all, the 'ben' user needs the proper permissions in /etc/sudoers. Usually be a member of the "sudo" or "admin" group. – Patrik Kullman Apr 02 '14 at 14:19
5

You probably want sudo -u instead of sudo su -:

ssh -t $USER@server006.web.com sudo -u http script 
glenn jackman
  • 238,783
  • 38
  • 220
  • 352
3

Guess I'm late to the party. My solution:

ssh -t $USER@server006.web.com "sudo cat /etc/shadow"

and replace cat /etc/shadow with your desired program.

wbt11a
  • 798
  • 4
  • 12