LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Creating a script that will log into root and enter password and then run commands. (https://www.linuxquestions.org/questions/linux-newbie-8/creating-a-script-that-will-log-into-root-and-enter-password-and-then-run-commands-766297/)

tananthulus 11-02-2009 02:07 PM

Creating a script that will log into root and enter password and then run commands.
 
I have been looking around for a way to make a simple bash script log into root at one point and enter the password then continue with more commands.

So far my script looks like this:

#! /bin/bash


mount /dev/sdd1 /mnt
rsync -av --stats /mnt/* /home/chunky/Desktop
umount /dev/sdd1
su chunky


Any help to get it to log into root so it will be able to mount just running one script would be great!

Thank you!

Mr-Bisquit 11-02-2009 02:25 PM

You can login as root by disabling the display manager.
You can also configure the display manager to allow root logins.
Out of curiosity, why don't you just use a root terminal within the user environment or use the virtual terminals?

You can also try your commands under single user mode.

i92guboj 11-02-2009 02:31 PM

Look into "sudo".

tananthulus 11-02-2009 02:34 PM

This needs to be a back up of a pc with no monitor, keyboard or mouse. The script will sit on the pc and a remote pc will tell it to run when someone plugs a usb hdd into it.

I guess it would be simpler to have the remote ssh as root and run it, but it is plaguing me now that i cant make the script switch between root and user.

Since a couple of your comments are new to me, can you show me a simple script example how to tell it to log into root and enter a password and press the enter key, that's where i'm stuck.

Thanks again!

tananthulus 11-02-2009 02:36 PM

I'll look at the man page for sudo for script examples also Mr.B.

i92guboj 11-02-2009 02:49 PM

You need to configure sudo correctly, to allow at least the commands you need. You can as well configure it so everyone can run anything as root just by doing "sudo <command>", then you can use "sudo <command>" in your script when it needs to run something as root.

You could as well run the whole script as root by using "sudo <script.sh>", in any case, the first step is learning how sudo works.

But, looking at your last post:

Quote:

The script will sit on the pc and a remote pc will tell it to run when someone plugs a usb hdd into it.
What exactly do you need it to do? You might be able to do this with udev rules and no further complications.

tananthulus 11-02-2009 03:05 PM

Just mount the drive and rsync then umount and email success.

i92guboj 11-02-2009 03:56 PM

Well, the basics of your script could be something like:

Code:

#!/bin/bash

die() {
  echo "$1"
  exit 1 # error
}

mount <whatever> || die "error while mounting"
rsync or cp or tar or <whatever else> || die "error while running rsync"
umount <whatever>

echo success
exit 0

You can change the echo commands or pipe them to any mail sender, so the output is sent to wherever you want instead of being echoed locally which would do you no good.



This is one piece. The other piece is the trigger. If your system uses udev, you should check how to write udev rules, this can trigger events when a device is added or removed.

http://www.reactivated.net/writing_udev_rules.html

A simple set of rules to launch an script when you attach a disk (or pendrive or whatever, it really doesn't matter) would look like this:

Code:

KERNEL=="sd[b-z]", SUBSYSTEMS=="scsi", ATTRS{model}=="DataTraveler 2.0", NAME="%k", SYMLINK+="kingston", GROUP="usb", MODE="0775" OPTIONS="last_rule"
ACTION=="add", KERNEL=="sd[b-z][0-9]", SUBSYSTEMS=="scsi", ATTRS{model}=="DataTraveler 2.0", SYMLINK+="kingston%n", GROUP="usb", MODE="0775" NAME="%k"
ACTION=="add", KERNEL=="sd[b-z][0-9]", SUBSYSTEMS=="scsi", ATTRS{model}=="DataTraveler 2.0", RUN+="/bin/mkdir -p /mnt/kingston%n"
ACTION=="add", KERNEL=="sd[b-z][0-9]", SUBSYSTEMS=="scsi", ATTRS{model}=="DataTraveler 2.0", RUN+="/bin/mount -t auto -o umask=000,users,rw,noauto,nodev,noatime /dev/%k /mnt/kingston%n", OPTIONS="last_rule"
ACTION=="add", KERNEL=="sd[b-z][0-9]", SUBSYSTEMS=="scsi", ATTRS{model}=="DataTraveler 2.0", RUN+="/path/to/yourscript"

The firsts lines will name and set the right permissions for the device nodes when a device with the given properties is detected. The third one will create the mount points, the 4th will mount it, the last one will run your script. You could as well simplify the script and write everything as udev rules directly as well.

This is just an example, you will need to read the link above to write correct rules for your device, since the defining characteristics for each device will be different. This is just a general idea.

tananthulus 11-02-2009 05:37 PM

Thank you very much for the detailed samples. Visual stimulation far exceeds all the chatter in the world.

I will test this out.

Thank you.


All times are GMT -5. The time now is 03:58 PM.