Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SSH to server, Sudo su - then run commands in bash [duplicate]

I have the following

#!/bin/bash

USER='scott'
PASS='tiger'

ssh -t [email protected] "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

like image 966
Ben Coughlan Avatar asked Apr 02 '14 13:04

Ben Coughlan


People also ask

How do I run sudo su?

In most Linux distributions, the sudo package is installed by default. To use sudo, let's just type sudo and press enter. If sudo is installed, the sudo package usage details will be displayed. If it's not, a “command not found” message will be displayed.

Can you sudo SSH?

Admins can SSH into any host and run sudo commands. Normal users can SSH into hosts that they have contributed to and run sudo commands.


1 Answers

Try

ssh -t [email protected] '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.

like image 175
Patrik Kullman Avatar answered Sep 28 '22 10:09

Patrik Kullman