Friday, 14 December 2012

PHP cheating sheet

Cope File to Remote Server Using PHP

1. install php ssh2 extension

in ubuntu

apt-get install libssh2-1-dev libssh2-php

 test if php ssh2 installed

file_exists('ssh2_connect')

2. find finger print of remote server
 connect to the server
 ssh-keygen -lf /etc/ssh/ssh_host_rsa_key.pub
the second part is fingerprint

3. using ssh2 to scp files

$conn = ssh2_connect('remote-server-name', 22);

$fingerprint = ssh2_fingerprint($connection,
SSH2_FINGERPRINT_MD5 | SSH2_FINGERPRINT_HEX);

if ($fingerprint != $knowprint) {
    die("middle man found");
}
ssh2_auth_password($conn, 'username', 'password');

ssh2_scp_send($conn, 'file_name_in_local, 'file_name_in_remote', 0777);

Use call_user_func function

class Hello {
    public static function say_hello()
    {
        echo "Hello World!\n";
    }
}

//There are three ways to use call_user_func

//seperate class name and function
call_user_func(array("Hello", 'say_hello'));

//combine class name and function into one string
call_user_func('Hello::say_hello');

//create a object
call_user_func(array(new Hello(), 'say_hello'));

PHP extension

what extensions have been loaded

php -m 

In Alpine, extension is here /usr/lib/php/modules

Thursday, 13 December 2012

Find a remote server ssh fingerprint

1. connect to the server
2. ssh-keygen -lf /etc/ssh/ssh_host_rsa_key.pub
3. should see three parts
the second part is fingerprint

Thursday, 6 December 2012

Linux Commands

Limit resource a user can access

edit file /etc/security/limits.conf

//show limits
ulimit -a

See differences between two folders

diff -rq folder1 folder2

Find file in mac or Linux

find ./ -name platform_check.php

Open image

open test.png
open test.jpg

curl

//curl to redirect content to file
curl  --output leo.jpg https://checkout.whahaha-content.com/images/unionpay.png

created nested directories

The -p flag will create nested directories if directories do not exist. If exist, do nothing.

//mkdir: leo: No such file or directory
mkdir leo/test

//this works
mkdir -p leo/test

sed command

replace all occurences of a pattern

//ie flag will replace in place and that file will be modified
sed  -ie 's/unix/linux/g' my.txt

//all leo in my.txt will be replaced by developer. Also will make a backup file called my.txt.bak
sed -i.bak "s/leo/developer/g" my.txt

find which process is using a port number in mac

lsof -nP -iTCP -sTCP:LISTEN | grep port-number