Shell Script Voodoo

Push Transfer Directories Through SSH/SCP Using TAR For Added Compression

tar cz <move_me>/ | ssh tar xz -C <move_here>/

Pull Transfer Directories Through SSH/SCP Using TAR For Added Compression

ssh tar xz -C <move_here>/ | tar cz <move_me>/

Modify many files in shell with ‘find’ ‘xargs’ and ‘sed’:

find . -name "*.php" | xargs -n 1 echo

will show you the power of xargs.

After that, on a sample file, you can test the regexp, with an inplace change (-i) or a backup change (-i.bak). You can also use an other character to replace ‘/’ if your pattern/replacement already have one.

At the end, it should looks like :

pattern=`cat /path/to/pattern`; replacement=`cat /path/to/replacement`
find . -name "*.php" | xargs -n 1 sed -i -e 's|$pattern|$replacement|g'

Clear Memory Cache

Put this line in a script or as a oneliner in crontab to clear clogged up cache:

sync && echo 3 > /proc/sys/vm/drop_caches

Non-interactive SSH One-Liner

If you restricted direct root access via ssh and have a user with visudo set, you can spin things with the following ssh line (alternatively you could alias this in .bashrc or set the parameteres via ~/.ssh/config):

ssh -qtt -oStrictHostKeyChecking=no -oCheckHostIP=no -oConnectTimeout=1 -oConnectionAttempts=1 -oPreferredAuthentications=publickey <HOSTNAMEorIP>  'sudo su - root -c "yum -y update opsview-agent; yum -y erase rhn-setup rhn-check rhnsd yum-rhn-plugin subscription-manager *-firmware"'

This will update the opsview agent (nrpe) and strip down the last bits of unused packages from your RHEL minimal install (removing RHN related packages as well, granted you have a local repo server).

Non-interactive visudo One-Liner

Take this and add it to your favorite SSH oneliner (proven to work on RHEL and derivatives ):

cp -f /etc/sudoers /tmp && sed -i -e /team/d /tmp/sudoers && echo %webteam ALL=\(webadm:webteam\) ALL >> /tmp/sudoers && EDITOR=mv\ /tmp/sudoers visudo

To break it down; we take a copy of the sudoers file as directly working on it won’t work by design, we make sure the line we’re about to add is not already in there, we add our line (for more detail ).

Now the fun part :), set the move command as an EDITOR will tell visudo to use this for editing, visudo will check and compare the given file with /etc/sudoers and modify accordingly.

Double quotes inside double quotes inside single quotes in Bash scripting

ssh -qtt -oStrictHostKeyChecking=no -oCheckHostIP=no -oConnectionAttempts=1 $hip 'sudo su - -c "sed -i -e /^ErrorDocument/d /etc/httpd/conf/httpd.conf && echo -e "\""ErrorDocument 400 \042Bad Request!! Your browser sent a request that this server could not understand.\042"\"" >> /etc/httpd/conf/httpd.conf && echo ErrorDocument 404 >> /etc/httpd/conf/httpd.conf && service httpd restart"'

Could come in handy if you need to remotely execute commands which require loads of double quotes 🙂
The \042 in there fed with the -e option of echo will provide an extra double quote with the least amount of fuss…

Leave a Reply

Your email address will not be published.


This site uses Akismet to reduce spam. Learn how your comment data is processed.