Archive

Archive for the ‘Useful commands’ Category

mega.nz direct download from command line

22/06/2017 Leave a comment

Found somewhere:

#!/bin/bash
# Updated by Derek "Cyph3r"
# Syntax ./mega.sh
url=$1

# get id and key from url
id=`echo $url | awk -F '!' '{print $2}'`
key=`echo $url | awk -F '!' '{print $3}' | sed -e 's/-/+/g' -e 's/_/\//g' -e 's/,//g'`

# decode key
b64_hex_key=`echo -n $key | base64 --decode --ignore-garbage 2> /dev/null | xxd -p | tr -d '\n'`
key[0]=$(( 0x${b64_hex_key:00:16} ^ 0x${b64_hex_key:32:16} ))
key[1]=$(( 0x${b64_hex_key:16:16} ^ 0x${b64_hex_key:48:16} ))
key=`printf "%016x" ${key[*]}`
iv="${b64_hex_key:32:16}0000000000000000"

# send the request
json_data=`curl --silent --request POST --data-binary '[{"a":"g","g":1,"p":"'$id'"}]' https://eu.api.mega.co.nz/cs`

# get the download url
#Formerly $12
new_url=`echo $json_data | awk -F '"' '{print $14}'`

# get the file name, have to do a lot of weird things because openssl is tricky
tmp=`echo $json_data | awk -F '"' '{print $6}' | sed -e 's/-/+/g' -e 's/_/\//g' -e 's/,//g' | base64 --decode --ignore-garbage 2> /dev/null | xxd -p | tr -d '\n' > enc_attr.mdtmp`
tmp=`xxd -p -r enc_attr.mdtmp > enc_attr2.mdtmp`
openssl enc -d -aes-128-cbc -K $key -iv 0 -nopad -in enc_attr2.mdtmp -out dec_attr.mdtmp
#Changed this too, can't remember where from
file_name=`cat dec_attr.mdtmp | awk -F '"' '{print $8}'`
rm -f *.mdtmp

# download the file and decrypt it
enc_file=${file_name}.enc

curl --output $enc_file $new_url
openssl enc -d -aes-128-ctr -K $key -iv $iv -in $enc_file -out $file_name
rm -f $enc_file

Advertisements
Categories: Bash, Linux, Useful commands

Backup remote server using tar and ssh

13/07/2013 1 comment
ssh user@server "sudo tar cvzp - / --same-owner --exclude=/proc/* --exclude=/media/* --exclude=/dev/* --exclude=/mnt/* --exclude=/sys/* --exclude=/tmp/* --ignore-failed-read" > backup.tgz

user should have sudo rights without password on remote machine.

Categories: Linux, Useful commands

How to get fresh file/mysql backup from remote on minimal effort.

19/02/2013 Leave a comment

Lets assume that we have a remote web server (with ssh access) and we want fresh backup of files and mysql databases. Of course we can login, type some commands and then sftp those files but lets do this on minimum effort:

Backup files (tar, gzip and ‘download’ them):

ssh user2@remote "tar cf - /var/www/somepage | gzip -c" > files.tar.gz

Backup MySQL database:

ssh user2@remote "mysqldump -pXXXXXX database | gzip -c" > mysql.gz

Lets merge those command and write a little bash script that will automate those activities:

#!/bin/bash
now=$(date +"%Y%m%d%H%M")
bckdir="/home/user/backups"
ssh user2@remote "mysqldump -pXXXXXX databases | gzip -c" > $bckdir/$now.mysql.gz
ssh user2@remote "tar cf - /var/www/somepage | gzip -c" > $bckdir/$now.files.tar.gz

where:
XXXXXXX is a mysql password,
database is our db to backup.

Categories: Bash, Linux, Useful commands

How to download archived videos from twitch.tv/justin.tv

19/02/2013 1 comment

Working source here.

This php script will download all archived videos from twitch.tv (and probably from justin.tv – because there is same api as on twitch) using wget. All videos would be saved to download folder (create it first) with date as a name and ‘show title’. File name is limited to 50 chars.

Downloading is done by parsing json files from justin, more info: Channel/archives api wiki.

<?php
$url = "http://api.justin.tv/api/channel/archives/xxxx.json?limit=100"; //where xxxx is a channel id or login
$c = curl_init($url);
$options = array(
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_HTTPHEADER => array('Content-type: application/json')
);
curl_setopt_array( $c, $options );
$json = curl_exec($c);
$json_a = json_decode($json,true);
$json_a = array_reverse($json_a);
 
foreach($json_a as $k => $v) {
    if(!history_check(format_date($v['created_on']))){
        exec('wget -c '.$v['video_file_url'].' -O "downloads/'.format_date($v['created_on']).' - '.format_title($v['title']).'.flv"', $array);
        history_add(format_date($v['created_on']));
        echo implode('"\n"', $array);
    }
}
 
function format_date($date){
    $out = preg_replace('/[^0-9\.]/', '', $date);
    return $out;
}
function format_title($title){ //format title and limit to 50 chars
    $out = preg_replace('/[^a-zA-Z \.]/','', $title);
    $out = substr($out, 0, 50);
    return $out;
}
function history_check($date){
    $data = file_get_contents('history');
 
    if(strpos($data, $date) !== FALSE)
    {
        $out = TRUE;
    }
    else
    {
        $out = FALSE;
    }
return $out;
}
 
function history_add($date){
    file_put_contents('history', $date."\n", FILE_APPEND);
}
?>

Usage:
Create download dir, save this source as get.php file and simply run it from command line (php-cli need to be installed):

php get.php

Take in mind that information about downloaded videos is stored in history file. This will avoid downloading videos which was downloaded before.

Run command on each file in directory

19/02/2013 Leave a comment

Most useful when you have many files in directory and bash doesn’t want to cooperate:

-bash: -: Argument list too long

First one:

for file in *; do ls -al "$file"; done

where * could be a file name or extension ex.: *.jpg

And much more easy to remember:

find . -exec ls -al {} \;
Categories: Bash, Linux, Useful commands

PHP5 + suhosin warning

05/01/2013 1 comment

Im using Debian GNU/Linux wheezy/sid with latest PHP available from apt. Some time ago I found weird warning that shows up in logs when php file is loaded:

napi@debian:/home/napi$ php test.php 
PHP Warning:  PHP Startup: Unable to load dynamic library '/usr/lib/php5/20100525+lfs/suhosin.so' - /usr/lib/php5/20100525+lfs/suhosin.so: cannot open shared object file: No such file or directory in Unknown on line 0

Its harmful, but annoying.
To get rid of it just run this command as root:

dpkg -P php5-suhosin
Categories: Linux, Useful commands

Remove special characters recursively from file names.

31/12/2012 Leave a comment
e='s/[ #,;]//g'; find . | while read f;do c=`basename "$f"`; b=`echo ${f%$c} | sed "$e"`; rename -v "$e" "$b`basename "$f"`"; done

This command will scan current directory and change file/folder name when found bellow characters: ” #,;”

Very useful if you plan e.g. copy Maildir from linux (extX) to windows (ntfs):


./Maildir/cur/1332130272.18962_0.debian:2, renamed as ./Maildir/cur/1332130272.18962_0.debian2
./Maildir/cur/1332389482.29532_0.debian:2, renamed as ./Maildir/cur/1332389482.29532_0.debian2
./Maildir/cur/1332098770.29631_0.debian:2, renamed as ./Maildir/cur/1332098770.29631_0.debian2
./Maildir/cur/1330798043.25409_0.debian:2, renamed as ./Maildir/cur/1330798043.25409_0.debian2

Categories: Linux, Useful commands