Last active
October 8, 2015 20:57
-
-
Save rubemlrm/3387796 to your computer and use it in GitHub Desktop.
Script do manage Apache virtualHosts
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/bin/perl | |
| use warnings; | |
| use strict; | |
| my $userhome = "nome de utilizador"; | |
| ###################################################### | |
| # Script para gerir VirtualHosts no Apache # | |
| # # | |
| # Permite adicionar , remover , verificar , listar, # | |
| # desactivar os vhosts que temos na mossa máquina. # | |
| # # | |
| # Developed by Rubem Mota # | |
| # Twitter: @rubemlrm # | |
| # Site: rubemlrm.com # | |
| ###################################################### | |
| #funcoes | |
| sub AddEntry($){ | |
| my $info = $_[0]; | |
| #valida o número de valores do array | |
| if($info eq '2'){ | |
| die("Erro: Número de argumentos inválido\n"); | |
| }else{ | |
| my $dir = @$info[1]; | |
| my $url = @$info[0]; | |
| #validação do url | |
| if($url !~ /(\w+\.){2,}[a-z]{2,4}$/){ | |
| print("Erro: URL inválido\n"); | |
| exit(1); | |
| } | |
| #extrai a informação relativa ao dominio do url | |
| my $domain = substr($url,4); | |
| #verifica se a directoria existe | |
| if(!-d "/home/rubem/public_html/$dir/"){ | |
| print "erro directoria não existe\n"; | |
| DirOp($dir,'newFolder'); | |
| }else{ | |
| #verifica se o ficheiro existe | |
| if(-f "/etc/apache2/sites-enabled/$url.conf"){ | |
| print "Ficheiro ja existe!\n"; | |
| exit(1); | |
| } | |
| DirOp($dir,'repairDirTree'); | |
| open(FILE, ">/etc/apache2/sites-enabled/$url.conf") ||die($!); | |
| print FILE "<VirtualHost *:80>\n"; | |
| print FILE "\tServerAdmin webmaster\@$domain\n"; | |
| print FILE "\tServerName $url\n"; | |
| print FILE "\tServerAlias $domain\n"; | |
| print FILE "\t#Index + Directory Root\n"; | |
| print FILE "\tDirectoryIndex index.html\n"; | |
| print FILE "\tDocumentRoot /home/$userhome/public_html/$dir/htdocs/\n"; | |
| print FILE "\t#CGI Directory\n"; | |
| print FILE "\tScriptAlias /cgi-bin/ /home/$userhome/public_html/$dir/cgi-bin/\n"; | |
| print FILE "\t<Location /cgi-bin>\n"; | |
| print FILE "\t\toptions +ExecCGI\n"; | |
| print FILE "\t</Location>\n"; | |
| print FILE "\t#LogFiles\n"; | |
| print FILE "\tErrorLog /home/$userhome/public_html/$dir/logs/error.log\n"; | |
| print FILE "\tCustomLog /home/$userhome/public_html/$dir/logs/access.log combined\n"; | |
| print FILE "</VirtualHost>"; | |
| close(FILE); | |
| system("/etc/init.d/apache2 restart"); | |
| print "VirtualHost $url adicionado\n"; | |
| } | |
| } | |
| } | |
| sub DirOp($$){ | |
| my $dir = $_[0]; | |
| my $opt = $_[1]; | |
| if($opt eq 'newFolder'){ | |
| print "Deseja que o script crie a directoria ?(S/N)\n"; | |
| chomp(my $choice=<STDIN>); | |
| if($choice !~ /[S|s|n|N]/){ | |
| print "opcao inválida"; | |
| }elsif($choice eq 'S' || $opt eq 's'){ | |
| mkdir("/home/$userhome/public_html/$dir") || die "$!"; | |
| mkdir("/home/$userhome/public_html/$dir/htdocs/") || die "$!"; | |
| mkdir("/home/$userhome/public_html/$dir/cgi-bin/") || die "$!"; | |
| mkdir("/home/$userhome/public_html/$dir/logs/") || die "$!"; | |
| print "Pastas criadas\n"; | |
| }else{ | |
| print "O programa irá encerrar\n"; | |
| exit(1); | |
| } | |
| }elsif($opt eq 'repairDirTree'){ | |
| if(!-d "/home/rubem/public_html/$dir/htdocs/"){ | |
| mkdir("/home/$userhome/public_html/$dir/htdocs/") || die"$!"; | |
| }elsif(!-d "/home/$userhome/public_html/$dir/cgi-bin/"){ | |
| mkdir("/home/$userhome/public_html/$dir/cgi-bin/") || die "$!"; | |
| }elsif(!-d "/home/$userhome/public_html/$dir/logs/"){ | |
| mkdir("/home/$userhome/public_html/$dir/logs/") || die "$!"; | |
| }else{ | |
| print ""; | |
| } | |
| } | |
| } | |
| sub RemEntry($){ | |
| my $url = $_[0]; | |
| if($url !~ /(\w+\.){2,}[a-z]{2,4}$/){ | |
| print("Erro: Parametro invalido\n"); | |
| exit(1); | |
| }else{ | |
| my $flag = `ls /etc/apache2/sites-enabled/ || grep $url.conf`; | |
| if(!$flag){print"Ficheiro não existe\n";die(1);}else{ | |
| unlink("/etc/apache2/sites-enabled/$url.conf"); | |
| system("/etc/init.d/apache2 restart"); | |
| print "VirtualHost $url removido\n "; | |
| } | |
| } | |
| } | |
| sub CheckEntry($){ | |
| my $check = $_[0]; | |
| if($check =~ /0/){ | |
| my $sites = `ls /etc/apache2/sites-enabled/ || grep *.config`; | |
| print $sites; | |
| }else{ | |
| my $siteconf = `cat /etc/apache2/sites-enabled/$check.conf`; | |
| print $siteconf."\n"; | |
| } | |
| } | |
| sub DisEntry($){ | |
| my $url = $_[0]; | |
| if($url !~ /(\w+\.){2,}[a-z]{2,4}$/){ | |
| print "Erro: Paramentro invalido\n"; | |
| exit(1); | |
| }else{ | |
| my $flag = `ls /etc/apache2/sites-enabled/$url.conf`; | |
| if (!$flag){print "Ficheiro não existe";}else{system("mv /etc/apache2/sites-enabled/$url.conf /etc/apache2/sites-available/$url.conf");}; | |
| system("/etc/inid.d/apache2 restart"); | |
| } | |
| } | |
| sub Main(){ | |
| my $opt; | |
| if((@ARGV == 0 || $ARGV[0] !~ /^-(ad|rem|c|h|dis)$/)){ | |
| print "Primeiro Argumento inválido\n"; | |
| }else{ | |
| $opt = $ARGV[0]; | |
| shift(@ARGV); | |
| if($opt eq '-ad'){ | |
| AddEntry(\@ARGV); | |
| }elsif($opt eq '-rem'){ | |
| RemEntry($ARGV[0]); | |
| }elsif($opt eq '-c'){ | |
| if(@ARGV == 1){ | |
| CheckEntry($ARGV[0]); | |
| }elsif(@ARGV == 0){ | |
| CheckEntry(0); | |
| }else{ | |
| print "Número de paramentros inválidos\n"; | |
| } | |
| }elsif($opt eq '-h'){ | |
| print ":=====================================:\n"; | |
| print ": MENU DE Ajuda :\n"; | |
| print ": -add <url> <directoria> :\n"; | |
| print ": ex: -add www.rubemlrm.com rubemlrm :\n"; | |
| print ": -rem <url> :\n"; | |
| print ": ex: -rem www.rubemlrm.com :\n"; | |
| print ": -c <url> :\n"; | |
| print ": ex: -c www.rubemlrm.com :\n"; | |
| print ": -c (Esta opção lista todos os .conf :\n"; | |
| print ": -dis <url> :\n"; | |
| print ": ex: -dis www.rubemlrm.com :\n"; | |
| print ":=====================================:\n"; | |
| }else{ | |
| DisEntry($ARGV[0]); | |
| } | |
| } | |
| } | |
| Main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment