Monday, October 14, 2013

Schtasks.exe with programs / script having spaces

When you use the Schtasks.exe command line tool to create a schedule for a task, the task does not run if the path of the scheduled task contains a space. For example, if you create a daily schedule for Chrome opening a web page as the following:
schtasks /create /tn "your task name" /tr "%programfiles(x86)%\Google\Chrome\Application\chrome http://www.awebsite.com/apage" /sc daily /st 12:01

In the example above, Schtasks.exe treats everything after the first space in the path as a command-line argument.

So we should apply a workaround like below:
schtasks /create /tn "your task name" /tr "\"%programfiles(x86)%\Google\Chrome\Application\chrome\" http://www.awebsite.com/apage" /sc daily /st 12:01

In which we add \" before and after the path of the program / script.

In the case you want to make the task  "Run whether user is logged or not", you can add option /ru "System" as the following:
schtasks /create /tn "your task name" /tr "\"%programfiles(x86)%\Google\Chrome\Application\chrome\" http://www.awebsite.com/apage" /ru "System" /sc daily /st 12:01

That's all. Nice day.

Wednesday, October 2, 2013

Oracle: check history of executed queries

The view v$sql contains almost of queries which are executed in your Oracle DB. Basically you have privileges to query this view, you can check all from it. Below are some useful queries for you to do on this view.

1. Get latest query
select sql_text from v$sql where first_load_time=(select max(first_load_time) from v$sql)

2. Sort executed queries by load time
select sql_text, first_load_time from v$sql order by first_load_time desc

3. Get executed queries in a schema which have special text and sort by load time
select * from v$sql
where parsing_schema_name like 'YOUR_SCHEMA' and sql_text like '%YOUR_TEXT%'
order by first_load_time desc

4. Get 100 last executed queries
select sql_fulltext from
(select * from v$sql where parsing_schema_name like 'VHA' order by first_load_time desc)
where rownum < 101

5. Get 100 executed UPDATE or DELETE queries in a specific time period and sort by load time
select sql_text,sql_fulltext, first_load_time, parsing_schema_name from
(
  select * from v$sql
  where parsing_schema_name like 'YOUR_SCHEMA'
    and (sql_text like '%UPDATE %' or sql_text like '%INSERT %')
    and to_timestamp(first_load_time, 'YYYY-MM-DD/HH24:MI:SS') > to_timestamp('2012-09-27/14:06:00', 'YYYY-MM-DD/HH24:MI:SS')
  order by first_load_time desc
)
where rownum < 101

You can create your own queries to find out what queries you need to check. Remember this view v$sql doesn't store prepared statements.

Wednesday, September 25, 2013

Solving access shared folder problem between Windows 7 and Windows 2003

By default Windows 7 cannot access shared folders on Windows 2003 / 2000 or vice versa because there is a difference in LAN manager authentication policy between them. To solve this problem, you can do the following steps.

1. In Windows 7:
     1.1 Click the Start button and type secpol.msc then click OK
     1.2 In Local Policies >> Security Options,  double click on Network Security: LAN Manager authentication level to open it, then select Send LM & NTLM - use NTLMv2 session security if negotiated.  Apply this setting.
     1.3 Open Control Panel >> All Control Panel Items >> Network and Sharing Center >> Advanced sharing settings, in your current network profile, choose below options:

  • Turn on network discovery
  • Turn on  file and print sharing
  • Use user accounts and passwords to connect to other computers

     1.4 Log off or restart Windows 7

2. In Windows 2003 / 2000:
2.1 Click the Start button and type gpedit.msc then click OK
2.1 In Computer Configuration >> Windows Settings >> Security Settings >> Local Policie >> Security Options, double click on Network Security: LAN Manager authentication level to open it, then select Send LM & NTLM Responses.  Apply this setting.
2.3 In Run box, type gpupdate /force then OK to update new policy, OR restart Windows 2003

That's all. Now you can try to access your shared folders between Windows 7 and Windows 2003/2000.

Wednesday, September 11, 2013

Install Asterisk and FreePBX easily by 1 script

To make a VoIP call center, Asterisk & FreePBX is a top choice. This article will show you how to install them just by 1 script. Below are steps & the script file (shell).
  • Step 1: Install Linux CentOS
  • Step 2: Create a script file, named as install_Asterisk_CentOS.sh enter content as following text.
  • Step 3: Run chmod +x install_Asterisk_CentOS.sh  - set executable to script file
  • Step 4: Run  install_Asterisk_CentOS.sh

install_Asterisk_CentOS.sh  file
----------------------------------------------------------
 #!/bin/bash

function pear_db_install(){
    echo -e "\e[32mStarting Install php-pear-DB\e[m"
    cd /usr/src
    if [ ! -e ./DB-1.7.13.tgz ]; then
        wget http://download.pear.php.net/package/DB-1.7.13.tgz
    fi
    pear install DB-1.7.13.tgz
}

function AMP_install() {
    echo -e "\e[32mStarting Install Apache+PHP+MySQL\e[m"
    yum -y install php-gd kernel-headers kernel-devel kernel-PAE kernel-PAE-devel httpd php php-mysql php-pear-DB doxygen mysql-server libtermcap-devel php-gd gcc gcc-c++ libxml2-devel php-pear php-posix sox make
    #Install LAMP (Apache, PHP and MySQL in Linux) using yum.
    echo -e "\e[32mAMP Install OK!\e[m"
    sed -i "s/post_max_size = 8M/post_max_size = 20M/" /etc/php.ini
    sed -i "s/upload_max_filesize = 2M/upload_max_filesize = 20M/" /etc/php.ini
    sed -i "s/short_open_tag = Off/short_open_tag = On/" /etc/php.ini
    sed -i "s/memory_limit = 16M /memory_limit = 128M /" /etc/php.ini
    service mysqld start
}


function asterisk_install() {
    echo -e "\e[32mStarting Install Asterisk\e[m"
    useradd -c "Asterisk PBX" -d /var/lib/asterisk asterisk
    #Define a user called asterisk.
    mkdir /var/run/asterisk /var/log/asterisk /var/spool/asterisk -p
    #Change the owner of this file to asterisk.
    sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
    setenforce 0
    #shutdown selinux
    sed -i 's/^User apache/User asterisk/;s/^Group apache/Group asterisk/' /etc/httpd/conf/httpd.conf
    sed -i "s/AllowOverride None/AllowOverride All/" /etc/httpd/conf/httpd.conf
    service httpd restart
    #Change User apache and Group apache to User asterisk and Group asterisk.
    #Change the default AllowOverride All to AllowOverride None to prevent .htaccess permission problems.
    cd /usr/src
    if [ ! -e ./asterisk-$asteriskver.tar.gz ]; then
        wget http://downloads.asterisk.org/pub/telephony/asterisk/asterisk-$asteriskver.tar.gz
        if [ ! -e ./asterisk-$asteriskver.tar.gz ]; then
            wget http://downloads.asterisk.org/pub/telephony/asterisk/old-releases/asterisk-$asteriskver.tar.gz
        fi
    fi
    tar xf asterisk-$asteriskver.tar.gz
    if [ $? != 0 ]; then
        echo "fatal: dont have valid asterisk tar package"
        exit 1
    fi

    cd asterisk-$asteriskver
    ./configure '-disable-xmldoc'
    make
    make install
    make samples
    #This command will  install the default configuration files.
    #make progdocs
    #This command will create documentation using the doxygen software from comments placed within the source code by the developers.
    make config
    #This command will install the startup scripts and configure the system (through the use of the chkconfig command) to execute Asterisk automatically at startup.
    echo -e "\e[32mAsterisk Install OK!\e[m"
}

function libpri_install() {
    echo -e "\e[32mStarting Install LibPRI\e[m"
    cd /usr/src
    if [ ! -e ./libpri-$libpriver.tar.gz ]; then
        wget http://downloads.asterisk.org/pub/telephony/libpri/releases/libpri-$libpriver.tar.gz
    fi
    tar xf libpri-$libpriver.tar.gz
    if [ $? != 0 ]; then
        echo -e "fatal: dont have valid libpri tar package\n"
        exit 1
    fi

    cd libpri-$libpriver
    make
    make install
    echo -e "\e[32mLibPRI Install OK!\e[m"
}

function dahdi_install() {
    echo -e "\e[32mStarting Install DAHDI\e[m"
    cd /usr/src
    if [ ! -e ./dahdi-linux-complete-$dahdiver.tar.gz ]; then
        wget http://downloads.asterisk.org/pub/telephony/dahdi-linux-complete/dahdi-linux-complete-$dahdiver.tar.gz
        if [ ! -e ./dahdi-linux-complete-$dahdiver.tar.gz ]; then
            wget http://downloads.asterisk.org/pub/telephony/dahdi-linux-complete/releases/dahdi-linux-complete-$dahdiver.tar.gz
        fi
    fi
    tar xf dahdi-linux-complete-$dahdiver.tar.gz
    if [ $? != 0 ]; then
        echo -e "fatal: dont have valid dahdi tar package\n"
        exit 1
    fi

    cd dahdi-linux-complete-$dahdiver
    make
    if [ $? != 0 ]; then
        yum -y upgrade
        echo -e "\e[32mplease reboot your server and run this script again\e[m\n"
        exit 1
    fi

    make install
    make config
    /usr/sbin/dahdi_genconf
    service dahdi start
    echo -e "\e[32mDAHDI Install OK!\e[m"
}


function freepbx_install() {
    echo -e "\e[32mStarting Install FreePBX\e[m"
    cd /usr/src
    if [ ! -e ./freepbx-$freepbxver.tar.gz ]; then
        wget http://mirror.freepbx.org/freepbx-$freepbxver.tar.gz
    fi
    tar xf freepbx-$freepbxver.tar.gz
    if [ $? != 0 ]; then
        echo -e "fatal: dont have valid freepbx tar package\n"
        exit 1
    fi

    cd freepbx-$freepbxver
    . /tmp/.mysql_root_pw.$$

    #Set mysql initial password.
    mysqladmin create asterisk -uroot -p$mysql_root_pw
    if [ $? != 0 ]; then
        echo -e "fatal: failed to create asterisk database\n"
        exit 1
    fi

    mysqladmin create asteriskcdrdb -uroot -p$mysql_root_pw
    mysql asterisk < SQL/newinstall.sql -uroot -p$mysql_root_pw
    mysql asteriskcdrdb < SQL/cdr_mysql_table.sql -uroot -p$mysql_root_pw
mysql -uroot -p$mysql_root_pw <<EOF
GRANT ALL PRIVILEGES ON asteriskcdrdb.* TO asterisk@localhost IDENTIFIED BY 'amp109';
GRANT ALL PRIVILEGES ON asterisk.* TO asterisk@localhost IDENTIFIED BY 'amp109';
flush privileges;
EOF
    #Create databases and import tables.
    ./start_asterisk start
    echo -e "\e[32mYour IP Is `ifconfig|grep -oP \(\\\\d+?\\\\.\){3}\\\\d+|head -n1`,Password is $mysql_root_pw\nRemember This And press 'Enter' to start installation of FreePBX!\e[m"
    read pause
    ./install_amp --username=asterisk --password=amp109 --webroot=/var/www/html
    #chmod -R 777 /var/www/html;
    #Set permissions or HTTP error 403 forbidden.
    echo `wc -l /etc/asterisk/manager.conf` | awk '{system("head -n "$1-2" "$2">manager.tmp&&mv manager.tmp /etc/asterisk/manager.conf")}'
    #Delete last two lines of manager.conf or freepbx can't connect to the asterisk manager.
    sed -i 's/read = system,call,log,verbose,command,agent,user,config,command,dtmf,reporting,cdr,dialplan,originate/read = system,call,agent/' /etc/asterisk/manager.conf
    touch /etc/asterisk/sip_general_additional.conf
    touch sip_general_custom.conf
    touch /etc/asterisk/sip_general_custom.conf
    touch /etc/asterisk/sip_nat.conf
    touch /etc/asterisk/sip_registrations_custom.conf
    touch /etc/asterisk/sip_custom.conf
    touch /etc/asterisk/sip_custom_post.conf
    service asterisk restart
    sleep 1
    asterisk -rx 'module load chan_sip.so'
    asterisk -rx 'manager reload'
    #Load sip module then you can use sip command.
    asterisk -rx 'core set verbose 10'
    #Set level of verboseness.
    echo -e "\e[32mFreePBX Install OK!\e[m"
}

function lame_install(){
    echo -e "\e[32mStarting Install Lame for mp3 monitor\e[m"
    cd /usr/src
    if [ ! -e ./lame-$lamever.tar.gz ]; then
        wget http://sourceforge.net/projects/lame/files/lame/$lamever/lame-$lamever.tar.gz/download
    fi
    tar xf lame-$lamever.tar.gz
    if [ $? != 0 ]; then
        echo -e "\e[32mdont have valid lame tar package, you may lose the feature to check recordings on line\e[m\n"
        return 1
    fi

    cd lame-$lamever
    ./configure && make && make install
    if [ $? != 0 ]; then
        echo -e "\e[32mfailed to install lame, you may lose the feature to check recordings on line\e[m\n"
        return 1
    fi
    ln -s /usr/local/bin/lame /usr/bin/
    return 0;
}

function get_mysql_passwd(){
    service mysqld start
    while true;do
        echo -e "\e[32mplease enter your mysql root passwd\e[m";
        read mysql_passwd;
        # make sure it's not a empty passwd
        if [ "X${mysql_passwd}" != "X" ]; then
            mysqladmin -uroot -p$mysql_passwd password $mysql_passwd    # try empty passwd
            if [ $? == 0  ]; then
                break;
            fi

            mysqladmin password "$mysql_passwd"
            if [ $? == 0  ]; then
                break;
            fi

            echo -e "\e[32minvalid password,please try again\e[m"
        fi
    done
    echo mysql_root_pw=$mysql_passwd > /tmp/.mysql_root_pw.$$
}


function run() {

    asteriskver=1.6.2.20
    libpriver=1.4.12
    freepbxver=2.9.0
    dahdiver=2.6.2+2.6.2
    lamever=3.99

    AMP_install
    libpri_install
    dahdi_install
    asterisk_install
    pear_db_install
    get_mysql_passwd
    freepbx_install
    lame_install

    #Run all system after install.
    chkconfig dahdi on && chkconfig httpd on && chkconfig mysqld on && chkconfig asterisk on
    #Run all automatically at linux startup.
    service iptables stop && chkconfig iptables off
    #Stop the internal firewall now and forever.
    service asterisk restart
    chown asterisk.asterisk /var/lib/php -R
    /bin/rm -rf /tmp/.mysql_root_pw.$$
    # update index.html
cat > /var/www/html/index.html << EOF
<HTML>
<HEAD>
<head>
    <title>FreePBX</title>
    <meta http-equiv="Content-Type" content="text/html">
    <link href="mainstyle.css" rel="stylesheet" type="text/css">
</head>

<body>
<div id="page">

<div class="header">

    <a href="index.php"><img src="admin/images/freepbx.png"/></a>

</div>

<div class="message">
    Welcome
</div>

<div class="content">

<h4><a href="recordings/">Voicemail & Recordings (ARI)</a></h4>
<h4><a href="panel/">Flash Operator Panel (FOP)</a></h4>
<h4><a href="admin/">FreePBX Administration</a></h4>

<br><br><br><br><br><br>
</div>

    </div>

</body>
</html>
EOF

}

run

Wednesday, August 21, 2013

PHP: step by step example to do a download manager web page

Some info should be concerned before you begin to do a download manager program:
1. Bandwidth of your host
2. Download speed
3. Capacity of server
4. File is stored in folder or Database? If it is store in DB, should concern about capacity of DB
5. Download statistic

The first program
//file download.php
<?php
 $filename = "document.zip";
 $fp = fopen($filename, "rb");

 header("Content-type: application/octet-stream");
 header("Content-length: " . filesize($filename));

 fpassthru($fp);
 fclose($fp);
 ?>

It's too simple, sure! The first program will read document.zip file in server then save it on client (PC of user) as download.php file as default. You must rename it with correct extension name to open it in client PC.

The second program
Next step, we'll make the program download a file on a specific folder on server then save it in the same name in client, e.g. we want to download and save file with name document.zip on server, we will call download.php?file=document.zip
//file download.php
<?php
 //default folder on server that contains files which are allowed to download
 $upload_dir = "../upload/";

 //get download file name
 $filename = isset($_GET['file'])?$_GET['file']:'';

 //check the file
 if ( !preg_match('/^[a-z0-9\_\-][a-z0-9\_\-\. ]*$/i', $filename) )
         || !is_file($upload_dir.$filename) || !is_readable($upload_dir.$filename) ) {
     echo "Error: invalid file name or not existing!";
     exit(-1);
 } //end if

 //open in binary mode and read the file
 $fp = fopen($upload_dir.$filename, "rb");

 //send header to the browser in client side
 header('Content-type: application/octet-stream');
 header('Content-disposition: attachment; filename="'.$filename.'"');
 header('Content-length: ' . filesize($upload_dir.$filename));

 //get and save file
 fpassthru($fp);
 fclose($fp);
?>

It is still too easy, sure!

Where I can use this download manager
-Use it in the case of you don't want publish the direct location to download the file
-Use it in next step of a login progress, in which you allow user download a file after paying credit
-Use it if you want to track activity of user. This requires small modification to save time stamp + user + client ip + download file name to a log file or DB.

When we shouldn't use it
-Server is overloading
-File is too big
-File is not important

Hope it can help you start a bigger program -:)
Subscribe to RSS Feed Follow me on Twitter!