#!/bin/bash ###################################################### # # # vrun -- run a command on one or more virtual hosts # # as root (default) or the administrative # # user for the host (--as-admin switch) # # # # --verbose Shows a banner with the host, user, and # # command for each host the specified # # command is applied to. # # # # --as-admin run the command as the admin user of # # of the specified site(s) # # # # --on Specify which host(s) to run the command on; # # defaults to ALL virtual hosts on the system. # # Can use shell wildcards, e.g. "foo*" or a # # space-separated list .e.g "foobar.net net.com # # # ###################################################### # # Examples: # # List long format all files under each virtual root # ------------------------ # $ vrun --verbose ls -al / # # Substitute foo with bar in every hosts file on every domain # ------------------------ # $ vrun perl -p -i -e 's/foo/bar' /etc/hosts # # Do a less on the error log for virtual host foobar.net # ------------------------ # $ vrun less /var/log/httpd/error_log --on foobar.net # # Same as above for several hosts # # Do a less on the error log for virtual host foobar.net # ------------------------ # $ vrun less /var/log/httpd/error_log --on "foobar.net barbar.com" # # vi /home/userx/.forward as admin on hostn.org # ------------------------ # $ vrun --as-admin --on hostn.org vi /home/userx/.forward # HOSTS="$(/usr/local/bin/sitelookup -a domain)" CMD= VERBOSE=0 ASADMIN=0 for arg do if [ "$SKIP" = "1" ] then SKIP=0 continue fi case $arg in --on) shift SKIP=1 HOSTS=$1;; --verbose) VERBOSE=1;; --help) echo \ "$0 [--verbose] [--help] [--as-admin] [--on hostpat] CMD ARG0..ARGN" echo "Example: $0 --as-admin --on foobar.net less /etc/passwd" exit 1;; --as-admin) ASADMIN=1;; *) CMD="$CMD $1" ;; esac shift done CMD=$(echo $CMD | sed -e 's/^ //') cd /home/virtual for host in $(echo $HOSTS) do runas="root" if [ $ASADMIN -eq 1 ] then user=$(/usr/local/bin/sitelookup -d $host site_admin) if [ -z $user ] then echo "Skipping $domain, no admin found (domain alias?)" continue else runas="$user" RUN="su - $user#$host -c" fi else RUN="/usr/sbin/chroot /home/virtual/$host/ bash -c" fi [ $VERBOSE -eq 1 ] && { echo "----> $host (user: $runas) $CMD " } $RUN "$CMD" done