LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 11-02-2009, 02:07 PM   #1
tananthulus
LQ Newbie
 
Registered: Nov 2009
Distribution: SuSE, CentOS, RedHat, Solaris, Debian, Ubuntu, Fedora
Posts: 17

Rep: Reputation: 0
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!
 
Old 11-02-2009, 02:25 PM   #2
Mr-Bisquit
Member
 
Registered: Feb 2009
Distribution: FreeBSD, OpenBSD, NetBSD, Debian, Fedora
Posts: 770
Blog Entries: 52

Rep: Reputation: 68
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.

Last edited by Mr-Bisquit; 11-02-2009 at 02:26 PM.
 
Old 11-02-2009, 02:31 PM   #3
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,040

Rep: Reputation: 373Reputation: 373Reputation: 373Reputation: 373
Look into "sudo".
 
Old 11-02-2009, 02:34 PM   #4
tananthulus
LQ Newbie
 
Registered: Nov 2009
Distribution: SuSE, CentOS, RedHat, Solaris, Debian, Ubuntu, Fedora
Posts: 17

Original Poster
Rep: Reputation: 0
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!
 
Old 11-02-2009, 02:36 PM   #5
tananthulus
LQ Newbie
 
Registered: Nov 2009
Distribution: SuSE, CentOS, RedHat, Solaris, Debian, Ubuntu, Fedora
Posts: 17

Original Poster
Rep: Reputation: 0
I'll look at the man page for sudo for script examples also Mr.B.
 
Old 11-02-2009, 02:49 PM   #6
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,040

Rep: Reputation: 373Reputation: 373Reputation: 373Reputation: 373
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.
 
Old 11-02-2009, 03:05 PM   #7
tananthulus
LQ Newbie
 
Registered: Nov 2009
Distribution: SuSE, CentOS, RedHat, Solaris, Debian, Ubuntu, Fedora
Posts: 17

Original Poster
Rep: Reputation: 0
Just mount the drive and rsync then umount and email success.
 
Old 11-02-2009, 03:56 PM   #8
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,040

Rep: Reputation: 373Reputation: 373Reputation: 373Reputation: 373
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.
 
Old 11-02-2009, 05:37 PM   #9
tananthulus
LQ Newbie
 
Registered: Nov 2009
Distribution: SuSE, CentOS, RedHat, Solaris, Debian, Ubuntu, Fedora
Posts: 17

Original Poster
Rep: Reputation: 0
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Need to enter root password for wireless to connect fuzzyBuzz Ubuntu 1 11-18-2008 11:17 AM
Creating a script to run two commands anjaan Programming 11 06-15-2004 02:51 AM
Why won't RH9 let me enter root password during install ? davepal Linux - Newbie 1 03-26-2004 08:18 PM
I am screwed...Cant enter Root password. bruce1271 Linux - Hardware 8 09-24-2003 02:53 PM
Can't enter root password Dave255 Linux - Newbie 6 06-12-2003 12:56 AM


All times are GMT -5. The time now is 04:10 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration