Last active
August 29, 2015 14:04
-
-
Save atatarn/1b70a7357f3b80a68f0f to your computer and use it in GitHub Desktop.
Parse Apache.conf, find domainName from args and sudo into VirtualHost user
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/local/bin/bash | |
DOM="$1" | |
# Базовая логика вызовов sed | |
# sed -nE '/^START$/h;/^END$/{x;/^START.*\nFLAG 0(\n|$)/{p;x;p;x;};};/^START$/!{x;/^START/!x;/^START/{x;H;};};' filename | |
# Логика следующая: | |
# Если текущая строка START - сохраняем её в hold space (сбрасывая прежнее содержимое); | |
# Если текущая строка END - проверяем, если в hold space присутствует и START и FLAG 0, | |
# то печатаем hold space, печатаем текущую строку и "сбрасываем" hold space (для того чтоб не попасться на START-FLAG 0-END-END) | |
# Eсли текущая строка не STARТ и в hold space уже пристутствует START, то дописываем текущую строку к hold space# | |
#В нашем случае вызов sed будет иметь вид: | |
#sed -nE '/^\<VirtualHost.*$/h;/^\<\/VirtualHost\>$/{x;/^\<VirtualHost.*\n(ServerAlias[- .a-zA-Z0-9]* DOMAIN[- .a-zA-Z0-9]*|ServerName DOMAIN)(\n|$)/{p;x;p;x;};};/^\<VirtualHost.*$/!{x;/^\<VirtualHost.*/!x;/^\<VirtualHost.*/{x;H;};};' | |
#Где: | |
#START == \<VirtualHost.* | |
#FLAG 0 == (ServerAlias[- .a-zA-Z0-9]* DOMAIN[- .a-zA-Z0-9]*|ServerName DOMAIN) | |
#END = \<\/VirtualHost\> | |
tline=`sudo cat /usr/local/apache/conf/httpd.conf | sed -nE "/^\<VirtualHost.*$/h;/^\<\/VirtualHost\>$/{x;/^\<VirtualHost.*\n(ServerAlias[- .a-zA-Z0-9]* ${DOM}[- .a-zA-Z0-9]*|ServerName ${DOM})(\n|$)/{p;x;p;x;};};/^\<VirtualHost.*$/!{x;/^\<VirtualHost.*/!x;/^\<VirtualHost.*/{x;H;};};"` | |
if [ "`echo $tline`" = "" ]; then | |
echo 'Unknown domain' | |
exit 1 | |
fi | |
sudo cat /usr/local/apache/conf/httpd.conf | sed -nE "/^\<VirtualHost.*$/h;/^\<\/VirtualHost\>$/{x;/^\<VirtualHost.*\n(ServerAlias[- .a-zA-Z0-9]* ${DOM}[- .a-zA-Z0-9]*|ServerName ${DOM})(\n|$)/{p;x;p;x;};};/^\<VirtualHost.*$/!{x;/^\<VirtualHost.*/!x;/^\<VirtualHost.*/{x;H;};};" | |
if [ "`basename $0`" = "domo" ]; then | |
user=`echo $tline|egrep -o 'User [a-z0-9]+'|awk '{print $2}'|uniq` | |
echo "-> Sudoeing to $user" | |
sudo -s -H -u $user | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment