Showing posts with label Linux. Show all posts
Showing posts with label Linux. Show all posts
0

Changing the date and time on all files within a folder structure

Source : http://www.bradleymedia.org/touch-subfolders/

This is easily achieved by combining the touch command with the Linux find command using the dash exec action like :-

find * -exec touch {} \;
 
 
A better approach is to use the touch command with the dash t switch and specify 
an actual date and time. The example below will set all files to being modified at
ten a.m. on the 29th of Feb 2012.
 
find * -exec touch -t 201202291000 {} \; 
0

Linux Backup / Clone Disk

I want to clone my entire disk to to new disk using Linux.
I just need to write a simple command using dd and specify if (input file / disk) and of (output file).

dd if=/dev/sda of=/dev/sdb
 
To get get the current progress of disk copy / cloning 
I need to know the process id of dd process. 
Using command belows I can get the process id.

ps aux | grep -i dd

To make the progress shown on the console I need to write the following command

kill -SIGUSR1 


Reference :

http://www.thegeekstuff.com/2010/10/dd-command-examples/
http://serverfault.com/questions/4906/using-dd-for-disk-cloning
0

Linux - Sync time using google web server

Reference : http://superuser.com/questions/307158/how-to-use-ntpdate-behind-a-proxy

I got problem to sync time on my Linux controllers using ntpdate because they are under proxies & firewall. I found a simple solution and it's using google web server time.
Execute this
sudo date -s "$(wget -S "http://www.google.com/" 2>&1 | grep -E '^[[:space:]]*[dD]ate:' | sed 's/^[[:space:]]*[dD]ate:[[:space:]]*//' | head -1l | awk '{print $1, $3, $2, $5 ,"GMT", $4 }' | sed 's/,//')"
if the timezone is not configured yet, simply run this command
ln -sf /usr/share/zoneinfo/America/Los_Angeles /etc/localtime 
or using wizard
dpkg-reconfigure tzdata
0

SSH login without password

Source : http://linuxproblem.org/art_9.html
Note : Aku dah try. Jalan.

Your aim

You want to use Linux and OpenSSH to automize your tasks. Therefore you need an automatic login from host A / user a to Host B / user b. You don't want to enter any passwords, because you want to call ssh from a within a shell script.

How to do it

First log in on A as user a and generate a pair of authentication keys. Do not enter a passphrase:

a@A:~> ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/a/.ssh/id_rsa):
Created directory '/home/a/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/a/.ssh/id_rsa.
Your public key has been saved in /home/a/.ssh/id_rsa.pub.
The key fingerprint is:
3e:4f:05:79:3a:9f:96:7c:3b:ad:e9:58:37:bc:37:e4 a@A

Now use ssh to create a directory ~/.ssh as user b on B. (The directory may already exist, which is fine):

a@A:~> ssh b@B mkdir -p .ssh

b@B's password:

Finally append a's new public key to b@B:.ssh/authorized_keys and enter b's password one last time:

a@A:~> cat .ssh/id_rsa.pub | ssh b@B 'cat >> .ssh/authorized_keys'
b@B's password:

From now on you can log into B as b from A as a without password:

a@A:~> ssh b@B hostname
B
A note from one of our readers: Depending on your version of SSH you might also have to do the following changes:

* Put the public key in .ssh/authorized_keys2
* Change the permissions of .ssh to 700
* Change the permissions of .ssh/authorized_keys2 to 640
0

Web Auto Update (Linux + PHP)

In my project, the controller will be place at the remote site (like sabah & sawarak) and having sluggish internet connection. I need the controller to automatic update the firmware during the transfer.

How to do it?
  1. download the update script.

    wget http://192.168.0.8/ipvision-client/tools/autoupdate.php?id=IPV0001 -O autoupdate.sh

  2. Change the script file permission to allow execute.
    /bin/chmod 777 autoupdate.sh

  3. Execute the script

    bin/sh /tmp/autoupdate.sh

  4. update the status, so this operation not reoccur.

    wget http://192.168.0.8/ipvision-client/tools/autoupdate.php?id=IPV0001&action=done

  5. put the script on cron job.

0

Fileupload firmware update (linux + php)

I would like to perform the firmware update over file upload in my current project.
In my case I'm using Linux + PHP running on my vortex86SX controller.

How to do it ??

  1. Just pack all the updated files together and create a installation script (shell script) called installupdate.sh.
  2. Since shell script created is created on Windows, Linux is having problem to execute the script since it don't understand the additional trail ("\r") for new line. I use this command to remove the "\r" character.

    /bin/cat /tmp/installupdate/installupdate.sh | /bin/tr -d "\r" > /tmp/installupdate
  3. change permission of installation script to enable execute.
  4. run the installation script
  5. remove all the temporary files
Full code :
function installupdate(){
$uploadfile = '/tmp/installupdate.zip';
//phpinfo();
if (move_uploaded_file($_FILES['uploadfile']['tmp_name'], $uploadfile)) {
$cmd='/bin/mkdir /tmp/installupdate
/bin/unzip -o /tmp/installupdate.zip -d /tmp/installupdate
cd /tmp/installupdate/
/bin/cat /tmp/installupdate/installupdate.sh | /bin/tr -d "\r" > /tmp/installupdate/installupdate.sh
/bin/chmod 777 /tmp/installupdate/installupdate.sh
/bin/sh /tmp/installupdate/installupdate.sh
/bin/rm /tmp/installupdate.zip
/bin/rm /tmp/installupdate -r
'
;
echo "File is valid, and was successfully uploaded.\n";
system("$cmd");
} else {
echo "Possible file upload attack!\n";
}
}
0

PHP Exec function Alternative

Dalam projek linux aku ni, aku nak exec command linux untuk control proses x dapat nak buat sebab masalah dengan php exec function. Dia run tapi disebabkan priviledge problem x jadi2 aku buat. Habis aku configure safe_mode option kat php semua x jadi.

Last aku teringat Jimin cerita dia buat VPN pakai SSH. Aku pun try. Alhamdulillah jadik.
Script akan sentiasa looping.
Koronologinya :
  1. Start loop
  2. Ada satu script yang sentiasa monitor file command
  3. kalau file tu ada, dia exec
  4. sleep 2 saat
  5. delete file tersebut lepas dia execute
  6. kalau x de file tu, sleep 2 saat
  7. Loop lagi
  8. Tammat.
Contohnya mcm kat bawah :

#!/bin/sh
while [ 1 -ne 2 ]
do
if [ -f /opt/lampp/htdocs/ipvision-client/cmd.sh ]; then
/bin/chmod 777 /opt/lampp/htdocs/ipvision-client/cmd.sh
/bin/sh /opt/lampp/htdocs/ipvision-client/cmd.sh
sleep 2
/bin/rm /opt/lampp/htdocs/ipvision-client/cmd.sh
fi
sleep 2
done

p/s : boleh pendekkan lagi script ni.
0

Linux auto reboot on Kernel panic

Tertinggal henset kat rumah hari ni.
Takpe, kerja diteruskan. Aku tengah buat projek on Linux on tiny board.
Boot pakai usb. Kadang2 terjadi kernel panic. Carik punya jumpa cara yang simple.

pada bootloader config just tambah satu lagi paramter panic=5 , untuk saat. Nak lebih tambah sendirik. Hemo..hemoo..

Contonhnya macam kat bawah (sysLinux)

LABEL LINUX
KERNEL linux
APPEND root=/dev/sda3 rootdelay=10 panic=5
 
Copyright © peyotest