PHP Rodando Script Shell

Enviada por Felipe 
Felipe
PHP Rodando Script Shell
12 de September de 2006 às 06:14AM
Bom dia PHPers, =)

É o seguinte, eu tenho um script shell aqui, que roda blz na shell. Aí vaí o script:

#!/bin/sh
# Usage: ./adddomain.sh user pass domain webcname userquota trafquota
# where user = system user
# pass = system password
# domain = domain name
# webcname = name before domain name (mostly www)
# userquota = user quota in MB
# trafquota = traffic quota in GB
#

USER=$1
PASS=$2
DOMAIN=$3
WEBNAME=$4
QUOTA=$5
TRAFQUOTA=$6

SETQUOTA=/usr/local/scripts/bin/./setquota.sh
CRON=/etc/rc.d/./cron
HTPASSWD=htpasswd
MYSQL=/usr/local/bin/mysql
APACHECTL=/usr/local/sbin/apachectl
TOUCH=touch
CHOWN=chown
MKDIR=mkdir
PW=/usr/sbin/pw
ECHO=echo
CAT=cat
CP=cp
RM=rm
NAMED=/etc/rc.d/./named
WEBIP=xxxxxxxxxx
USERLIMIT=8
WEBVHOSTCONFDIR=/usr/local/etc/apache22/vhost
GROUP=web
HOME=/data/web
SKEL=/usr/share/skel
SHELL=/usr/local/bin/web
TMP=/tmp
DATETIME=`date +%d.%m.%y-%H.%M`
DATE=`date +%d.%m.%y`
HTTPDVHOST=/usr/local/etc/apache22/extra/httpd-vhosts.conf
CONFDIR=/usr/local/scripts/etc
NS1=ns3.ps5.com.br
NS2=ns4.ps5.com.br
MYADMIN=https://db.xxxxxxxx
MYHOST=127.0.0.1
MYUSER=root
MYPASS=xxxxxxxxxxxxxx
NAMEDCONF=/etc/namedb/named.conf
BANDLIMITURL=http://xxxxxxxxxxxx/bandwidth_exceeded.html
TRAFQUOTATYPE=Gi
TRAFQUOTALIMIT=$TRAFQUOTA$TRAFQUOTATYPE
TRAFQUOTAREFRESH=30D
SYSUSER=`cat /etc/passwd | awk -F: '{print $1}' | grep $USER`
SYSDOMAIN=`cat /etc/namedb/named.conf | grep zone | awk -F'"' '{print $2}' | grep $DOMAIN`
SYSUSERLIMIT=`echo $USER | wc -c`
SYSUSERLIMIT=`expr $SYSUSERLIMIT - 1`
echo "show databases;" >> /tmp/$USER.mysql
SYSMYSQLDB=`$MYSQL -u $MYUSER -h $MYHOST --password=$MYPASS < /tmp/$USER.mysql | grep $USER`
echo "use mysql; select User from user;" >> /tmp/$USER.mysqluser
SYSMYSQLUSER=`$MYSQL -u $MYUSER -h $MYHOST --password=$MYPASS < /tmp/$USER.mysqluser | grep $USER`

if [ "$USER" = "$SYSUSER" ]; then
$ECHO Erro 101: Usuario existente
exit 101
fi

if [ "$DOMAIN" = "$SYSDOMAIN" ]; then
$ECHO Erro 102: Dominio existente
exit 102
fi

if [ "$SYSUSERLIMIT" -gt "$USERLIMIT" ]; then
$ECHO Erro 103: Usuario com mais de 8 caracteres
exit 103
fi

if [ "$SYSMYSQLDB" = "$USER" ]; then
$ECHO Erro 105: Banco de Dados MySQL existente
$RM /tmp/$USER.mysql
exit 105
fi
$RM /tmp/$USER.mysql

if [ "$SYSMYSQLUSER" = "$USER" ]; then
$ECHO Erro 106: Usuario MySQL existente
$RM /tmp/$USER.mysqluser
exit 106
fi
$RM /tmp/$USER.mysqluser

### Adiciona usuario
$ECHO $PASS >> $TMP/$USER.pass
$PW useradd -n $USER -c $WEBNAME.$DOMAIN -d $HOME/$USER -g $GROUP -m -k $SKEL -s $SHELL -h 0 < $TMP/$USER.pass
$RM $TMP/$USER.pass

### Adiciona dominio APACHE
$MKDIR $WEBVHOSTCONFDIR/$WEBNAME.$DOMAIN

# Cria conf HTTP
$ECHO "<VirtualHost *:80>" >> $WEBVHOSTCONFDIR/$WEBNAME.$DOMAIN/http
$ECHO " ServerAdmin sysadm@ps5.com.br" >> $WEBVHOSTCONFDIR/$WEBNAME.$DOMAIN/http
$ECHO " DocumentRoot /data/web/$USER/public_html" >> $WEBVHOSTCONFDIR/$WEBNAME.$DOMAIN/http
$ECHO " ScriptAlias /cgi-bin/ /data/web/$USER/cgi-bin" >> $WEBVHOSTCONFDIR/$WEBNAME.$DOMAIN/http
$ECHO " ServerName $WEBNAME.$DOMAIN" >> $WEBVHOSTCONFDIR/$WEBNAME.$DOMAIN/http
$ECHO " ErrorLog /var/log/apache/$WEBNAME.$DOMAIN-error_log" >> $WEBVHOSTCONFDIR/$WEBNAME.$DOMAIN/http
$ECHO " CustomLog /var/log/apache/$WEBNAME.$DOMAIN-access_log common" >> $WEBVHOSTCONFDIR/$WEBNAME.$DOMAIN/http
$ECHO " CBandLimit $TRAFQUOTALIMIT" >> $WEBVHOSTCONFDIR/$WEBNAME.$DOMAIN/http
$ECHO " CBandExceededURL $BANDLIMITURL" >> $WEBVHOSTCONFDIR/$WEBNAME.$DOMAIN/http
$ECHO " CBandScoreboard /usr/local/scripts/etc/cband_scoreboard/$WEBNAME.$DOMAIN" >> $WEBVHOSTCONFDIR/$WEBNAME.$DOMAIN/http
$ECHO " CBandPeriod $TRAFQUOTAREFRESH" >> $WEBVHOSTCONFDIR/$WEBNAME.$DOMAIN/http
$CAT $CONFDIR/http >> $WEBVHOSTCONFDIR/$WEBNAME.$DOMAIN/http

# Cria conf HTTPS
$ECHO "<IfModule ssl_module>" >> $WEBVHOSTCONFDIR/$WEBNAME.$DOMAIN/https
$ECHO "<VirtualHost *:443>" >> $WEBVHOSTCONFDIR/$WEBNAME.$DOMAIN/https
$ECHO " ServerAdmin sysadm@ps5.com.br" >> $WEBVHOSTCONFDIR/$WEBNAME.$DOMAIN/https
$ECHO " DocumentRoot /data/web/$USER/public_html" >> $WEBVHOSTCONFDIR/$WEBNAME.$DOMAIN/https
$ECHO " ScriptAlias /cgi-bin/ /data/web/$USER/cgi-bin" >> $WEBVHOSTCONFDIR/$WEBNAME.$DOMAIN/https
$ECHO " ServerName $WEBNAME.$DOMAIN" >> $WEBVHOSTCONFDIR/$WEBNAME.$DOMAIN/https
$ECHO " ErrorLog /var/log/apache/$WEBNAME.$DOMAIN-error_log" >> $WEBVHOSTCONFDIR/$WEBNAME.$DOMAIN/https
$CAT $CONFDIR/https >> $WEBVHOSTCONFDIR/$WEBNAME.$DOMAIN/https

# Adiciona conf no httpd-vhost.conf
$CP $HTTPDVHOST $HTTPDVHOST.$DATETIME
$ECHO "Include etc/apache22/vhost/$WEBNAME.$DOMAIN/* " >> $HTTPDVHOST

### Cria arquivo de scoreboard do cband
$TOUCH /usr/local/scripts/etc/cband_scoreboard/$WEBNAME.$DOMAIN
$CHOWN www /usr/local/scripts/etc/cband_scoreboard/$WEBNAME.$DOMAIN

# Verifica conf do Apache
$APACHECTL configtest
if [ "$?" != "0" ]; then
$ECHO Erro 104: Problema na configuracao do Apache. Contate o administrador.
exit 104
fi

# Reinicia apache
$APACHECTL restart

### Adiciona usuario e banco MySQL
# Adiciona banco de dados
$ECHO "create database $USER;" >> /tmp/$USER.mysqldb
$MYSQL -u $MYUSER -h $MYHOST --password=$MYPASS < /tmp/$USER.mysqldb
$RM /tmp/$USER.mysqldb

# Adiciona usuario e privilegios
$ECHO "CREATE USER '$USER'@'localhost' IDENTIFIED BY '$PASS'; " >> /tmp/$USER.mysqluserpriv
$ECHO "GRANT USAGE ON *.* TO '$USER'@'localhost' IDENTIFIED BY '$PASS' WITH MAX_QUERIES_PER_HOUR 0 MAX_CONNECTIONS_PER_HOUR 0 MAX_UPDATES_PER_HOUR 0 MAX_USER_CONNECTIONS 0 ; " >> /tmp/$USER.mysqluserpriv
$ECHO "GRANT SELECT , INSERT , UPDATE , DELETE , CREATE , DROP , INDEX , ALTER , CREATE TEMPORARY TABLES , CREATE VIEW , SHOW VIEW , CREATE ROUTINE, ALTER ROUTINE, EXECUTE ON $USER.* TO '$USER'@'localhost'; " >> /tmp/$USER.mysqluserpriv
$MYSQL -u $MYUSER -h $MYHOST --password=$MYPASS < /tmp/$USER.mysqluserpriv
$RM /tmp/$USER.mysqluserpriv

### Adiciona dominio NS
# Adiciona dominio no named.conf
$ECHO "/// $DOMAIN - Adicionado $DATE pelo script automatico de adicao de dominios" >> /tmp/$USER.ns
$ECHO "zone \"$DOMAIN\" { " >> /tmp/$USER.ns
$ECHO " type master; " >> /tmp/$USER.ns
$ECHO " file \"master/$DOMAIN\"; " >> /tmp/$USER.ns
$ECHO "}; " >> /tmp/$USER.ns
$ECHO " " >> /tmp/$USER.ns
$CP $NAMEDCONF $NAMEDCONF.$DATETIME
$CAT /tmp/$USER.ns >> $NAMEDCONF
$RM /tmp/$USER.ns

# Cria arquivo de zona do dominio
$ECHO "\$TTL 3600 " >> /etc/namedb/master/$DOMAIN
$ECHO "@ IN SOA $DOMAIN. sysadm.$DOMAIN. ( " >> /etc/namedb/master/$DOMAIN
$ECHO " 1 ; serial " >> /etc/namedb/master/$DOMAIN
$ECHO " 1H ; refresh " >> /etc/namedb/master/$DOMAIN
$ECHO " 15M ; retry " >> /etc/namedb/master/$DOMAIN
$ECHO " 14D ; expire " >> /etc/namedb/master/$DOMAIN
$ECHO " 1H ; default_ttl " >> /etc/namedb/master/$DOMAIN
$ECHO " ) " >> /etc/namedb/master/$DOMAIN
$ECHO " " >> /etc/namedb/master/$DOMAIN
$ECHO "@ IN NS gordo " >> /etc/namedb/master/$DOMAIN
$ECHO "@ IN NS sputinik " >> /etc/namedb/master/$DOMAIN
$ECHO "@ IN MX 1 spirit " >> /etc/namedb/master/$DOMAIN
$ECHO " " >> /etc/namedb/master/$DOMAIN
$ECHO "sun IN A 200.199.251.131 " >> /etc/namedb/master/$DOMAIN
$ECHO "sputinik IN A 200.199.251.153 " >> /etc/namedb/master/$DOMAIN
$ECHO "spirit IN A 200.199.251.151 " >> /etc/namedb/master/$DOMAIN
$ECHO " " >> /etc/namedb/master/$DOMAIN
$ECHO "$WEBNAME IN CNAME sun " >> /etc/namedb/master/$DOMAIN
$ECHO "ftp IN CNAME sun " >> /etc/namedb/master/$DOMAIN
$ECHO "smtp IN CNAME spirit " >> /etc/namedb/master/$DOMAIN
$ECHO "mx IN CNAME spirit " >> /etc/namedb/master/$DOMAIN
$ECHO "pop3 IN CNAME spirit " >> /etc/namedb/master/$DOMAIN
$ECHO "pop IN CNAME spirit " >> /etc/namedb/master/$DOMAIN
$ECHO "email IN CNAME spirit " >> /etc/namedb/master/$DOMAIN
$ECHO "webmail IN CNAME spirit " >> /etc/namedb/master/$DOMAIN
$ECHO "supermail IN CNAME spirit " >> /etc/namedb/master/$DOMAIN
$ECHO " " >> /etc/namedb/master/$DOMAIN

# Reinicia named
$NAMED reload

### Adiciona estatisticas
# Adiciona entrada no webalizer.sh
$ECHO "/usr/local/bin/webalizer -c /usr/local/scripts/etc/webalizer/$WEBNAME.$DOMAIN" >> /usr/local/scripts/bin/webalizer.sh

# Cria arquivo de conf do dominio
$ECHO "LogFile /var/log/apache/$WEBNAME.$DOMAIN-access_log" >> /usr/local/scripts/etc/webalizer/$WEBNAME.$DOMAIN
$ECHO "OutputDir /data/web/$USER/public_html/webalizer" >> /usr/local/scripts/etc/webalizer/$WEBNAME.$DOMAIN
$ECHO "#Incremental no" >> /usr/local/scripts/etc/webalizer/$WEBNAME.$DOMAIN
$ECHO "HostName $WEBNAME.$DOMAIN" >> /usr/local/scripts/etc/webalizer/$WEBNAME.$DOMAIN
$ECHO "PageType htm*" >> /usr/local/scripts/etc/webalizer/$WEBNAME.$DOMAIN
$ECHO "PageType cgi" >> /usr/local/scripts/etc/webalizer/$WEBNAME.$DOMAIN
$ECHO "Quiet yes" >> /usr/local/scripts/etc/webalizer/$WEBNAME.$DOMAIN
$ECHO "ReallyQuiet yes" >> /usr/local/scripts/etc/webalizer/$WEBNAME.$DOMAIN
$ECHO "HideURL *.gif" >> /usr/local/scripts/etc/webalizer/$WEBNAME.$DOMAIN
$ECHO "HideURL *.GIF" >> /usr/local/scripts/etc/webalizer/$WEBNAME.$DOMAIN
$ECHO "HideURL *.jpg" >> /usr/local/scripts/etc/webalizer/$WEBNAME.$DOMAIN
$ECHO "HideURL *.JPG" >> /usr/local/scripts/etc/webalizer/$WEBNAME.$DOMAIN
$ECHO "HideURL *.png" >> /usr/local/scripts/etc/webalizer/$WEBNAME.$DOMAIN
$ECHO "HideURL *.PNG" >> /usr/local/scripts/etc/webalizer/$WEBNAME.$DOMAIN
$ECHO "HideURL *.ra" >> /usr/local/scripts/etc/webalizer/$WEBNAME.$DOMAIN
$ECHO "SearchEngine yahoo.com p=" >> /usr/local/scripts/etc/webalizer/$WEBNAME.$DOMAIN
$ECHO "SearchEngine altavista.com q=" >> /usr/local/scripts/etc/webalizer/$WEBNAME.$DOMAIN
$ECHO "SearchEngine google.com q=" >> /usr/local/scripts/etc/webalizer/$WEBNAME.$DOMAIN
$ECHO "SearchEngine eureka.com q=" >> /usr/local/scripts/etc/webalizer/$WEBNAME.$DOMAIN
$ECHO "SearchEngine lycos.com query=" >> /usr/local/scripts/etc/webalizer/$WEBNAME.$DOMAIN
$ECHO "SearchEngine hotbot.com MT=" >> /usr/local/scripts/etc/webalizer/$WEBNAME.$DOMAIN
$ECHO "SearchEngine msn.com MT=" >> /usr/local/scripts/etc/webalizer/$WEBNAME.$DOMAIN
$ECHO "SearchEngine infoseek.com qt=" >> /usr/local/scripts/etc/webalizer/$WEBNAME.$DOMAIN
$ECHO "SearchEngine webcrawler searchText=" >> /usr/local/scripts/etc/webalizer/$WEBNAME.$DOMAIN
$ECHO "SearchEngine excite search=" >> /usr/local/scripts/etc/webalizer/$WEBNAME.$DOMAIN
$ECHO "SearchEngine netscape.com search=" >> /usr/local/scripts/etc/webalizer/$WEBNAME.$DOMAIN
$ECHO "SearchEngine mamma.com query=" >> /usr/local/scripts/etc/webalizer/$WEBNAME.$DOMAIN
$ECHO "SearchEngine alltheweb.com query=" >> /usr/local/scripts/etc/webalizer/$WEBNAME.$DOMAIN
$ECHO "SearchEngine northernlight.com qr=" >> /usr/local/scripts/etc/webalizer/$WEBNAME.$DOMAIN
$ECHO " " >> /usr/local/scripts/etc/webalizer/$WEBNAME.$DOMAIN

# Cria .htaccess no diretorio do webalizer
$ECHO "AuthType Basic" >> /data/web/$USER/public_html/webalizer/.htaccess
$ECHO "AuthName \"Acesso restrito. Identifique-se.\"" >> /data/web/$USER/public_html/webalizer/.htaccess
$ECHO "AuthUserFile /usr/local/scripts/etc/htpasswd" >> /data/web/$USER/public_html/webalizer/.htaccess
$ECHO "Require User $USER" >> /data/web/$USER/public_html/webalizer/.htaccess

# Adiciona usuario ao htpasswd
$HTPASSWD -b /usr/local/scripts/etc/htpasswd $USER $PASS

# Reinicia cron
$CRON restart

### Altera QUOTA do Usuario
$SETQUOTA $USER $QUOTA

### Imprime informacoes do usuario
$ECHO "---- Informacoes do dominio $DOMAIN ---- <br>"
$ECHO "Servidor FTP: ftp.$DOMAIN ($WEBIP) <br>"
$ECHO "Usuario FTP: $USER <br>"
$ECHO "Senha FTP: $PASS <br>"
$ECHO "Endereco HTTP: $WEBNAME.$DOMAIN <br>"
$ECHO "Administracao MySQL: $MYADMIN <br>"
$ECHO "Banco MySQL: $USER <br>"
$ECHO "Usuario MySQL: $USER <br>"
$ECHO "Senha MySQL: $PASS <br>"
$ECHO "Endereco Estatisticas WEB: http://$WEBNAME.$DOMAIN/webalizer <br>"
$ECHO "Endereco Estatisticas de Trafego WEB: http://$WEBNAME.$DOMAIN/webalizer/band <br>"
$ECHO "Usuario Estatisticas WEB: $USER <br>"
$ECHO "Senha Estatisticas WEB: $PASS <br>"
$ECHO "Servidor NS1: $NS1 <br>"
$ECHO "Servidor NS2: $NS2 <br>"
$ECHO "---- ATENCAO ---- <br>"
$ECHO " Antes da publicacao DNS acesse <br>"
$ECHO "o servidor atraves do ip $WEBIP."
exit



Se eu rodar ./adddomain.sh usuario senha dominio www 20 20 funciona tudo ok, mas se eu rodo através do PHP, com um sudo, não funciona. Alguém sabe como fazer para o script rodar pelo PHP?

Felipe.
Glauco
Re: PHP Rodando Script Shell
13 de November de 2006 às 03:23AM
opa

mais como que você vai executar? com shell_exec ?

um script desse tamanho você tem que analisar linha por linha quando for jogar no php, por ser diferente do modo de como agente joga no shell

passa o jeito que vc ta tentando joga no php que agente já ve o erro

abraço,
Você precisa estar logado no PHPBrasil.com para poder enviar mensagens para os nossos fóruns.

Faça o login aqui.