Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have to do following steps on several servers:

  1. log in with my username
  2. type in sudo su -
  3. type in amCLI -l 32/1 | grep Firmware

So I would like to write this all in one script, my idea was:

#!bin/bash
for hostname in DPM-BZ0201 DPM-BZ0202
do
ssh -n vneudeck@$hostname "sudo su-; amCLI -l 32/1 | grep Firmware"
done

or

#!bin/bash
for hostname in DPM-BZ0201 DPM-BZ0202
do
ssh -n vneudeck@$hostname "sudo amCLI -l 32/1 | grep Firmware"
done

Both don't work though.

share|improve this question
    
possible duplicate of SSH: execute sudo command – slhck May 30 '12 at 10:02

A better solution is to set up sudo so that your user is allowed to execute /full/path/to/amCLI without providing a password.

Then you can simply

#!bin/bash
for hostname in DPM-BZ0201 DPM-BZ0202
do
    ssh -n vneudeck@$hostname sudo /full/path/to/amCLI -l 32/1 | grep Firmware
done
share|improve this answer

Maybe if you put vneudeck@$hostname in double quotation, it will help. And there is no space between su and - I hope it is helpfull

share|improve this answer
    
The double quotes are not necessary here. – slhck May 30 '12 at 10:25

The first one won't work:

sudo su -; amCLI -l 32/1 | grep Firmware

This would execute sudo su -, and when this returned (i.e. when you were done processing commands as root and exited) it would execute amCLI. (Note also that an alternative to sudo su is sudo -s, though sudo su - does more I think.)

The second version might work with some conditions: sudo should not be asking for a password, and amCLI should be in PATH (not root's path, which is probably set when you type sudo su -, but your path, because sudo uses your path). Try specifying the full path to amCLI, such as /usr/local/sbin/amCLI or whatever it is.

share|improve this answer
    
For security reasons, you should always give full path when running things under sudo script-driven. Otherwise, anyone capable of putting a file named amCli somewhere into the PATH of user vneudeck (which might be comparatively easy) has access to root priviledges. – DevSolar May 30 '12 at 10:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .