Use sudo command for script SSH server

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.

vicky

Posted 2012-05-30T09:58:12.440

Reputation: 11

possible duplicate of SSH: execute sudo command

– slhck – 2012-05-30T10:02:41.797

Answers

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

Bram

Posted 2012-05-30T09:58:12.440

Reputation: 582

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

Hamed JML

Posted 2012-05-30T09:58:12.440

Reputation: 387

The double quotes are not necessary here. – slhck – 2012-05-30T10:25:49.183

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.

Antonis Christofides

Posted 2012-05-30T09:58:12.440

Reputation: 373

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 – 2012-05-30T10:37:27.903