1

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.

2

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
0

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

  • The double quotes are not necessary here. – slhck May 30 '12 at 10:25
0

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.

  • 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.