Archive

Archive for the ‘Bash’ 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

Send email when medical results appear on lam24.pl

24/09/2015 Leave a comment

Bellow php script will check every 15 min if medical results appear on lam24.pl and send email with notification.


<?php
$url = "https://lam24.pl/wyniki.aspx&quot;;
$zlecenie = "xxxxxxxxx";
$pesel = "xxxxxxxxxxx";

$f = "lam24.ini";
$size = filesize($f);
$fH = fopen($f,"r");
$data = fread($fH,$size);
fclose($fH);
if(strpos($data,'true') !== false) {
exit();
}

$ckfile = tempnam("/tmp", "CURLCOOKIE");
$useragent = 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/533.2 (KHTML, like Gecko) Chrome/5.0.342.3 Safari/533.2';

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_COOKIEJAR, $ckfile);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, $useragent);

$html = curl_exec($ch);

curl_close($ch);

preg_match('~~', $html, $viewstate);
preg_match('~~', $html, $eventValidation);

$viewstate = $viewstate[1];
$eventValidation = $eventValidation[1];

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_COOKIEJAR, $ckfile);
curl_setopt($ch, CURLOPT_COOKIEFILE, $ckfile);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_REFERER, $url);
curl_setopt($ch, CURLOPT_USERAGENT, $useragent);

$postfields = array();
$postfields['__EVENTTARGET'] = "";
$postfields['__EVENTARGUMENT'] = "";
$postfields['__VIEWSTATE'] = $viewstate;
$postfields['__EVENTVALIDATION'] = $eventValidation;
$postfields['Login$UserName'] = $zlecenie;
$postfields['Login$Password'] = $pesel;
$postfields['Login$loginButton'] = 'Pokaż wyniki';

curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
$ret = curl_exec($ch);
if(strpos($ret,'Nie znaleziono wynik') !== true) {
$fp = fopen($f, 'w');
fwrite($fp, 'true');
fclose($fp);

$to = 'user@gmail.com';
$subject = 'Wyniki Lam24';
$message = 'Automatyczny skrypt, ktory sprawdza dostepnosc wynikow co 15 minut informuje za sa one juz dostepne:' . "\r\n" . 'https://lam24.pl/wyniki.aspx' . "\r\n" .
'Zlecenie: ' . $zlecenie . "\r\n" .
'Pesel: ' . $pesel;
$headers = 'From: user@domain.pl' . "\r\n" .
'Reply-To: user@domain.pl' . "\r\n" .
'X-Mailer: PHP/' . phpversion();

mail($to, $subject, $message, $headers);
}
curl_close($ch);
?>

crontab:
*/15 * * * * /usr/bin/php /home/n4p1/lam24/lam24.php

Categories: Bash, PHP + MySQL

Bebiko Junior competition (bebiklub.pl)

16/07/2013 Leave a comment

Friend of mine participate in competition on bebiklub.pl – just a simple vote system. Unfortunately I can’t see on which place she is, there is no such a functionality, so I need to implement it by myself. As usual I choice php for that:

<?php
for($z=0;$z<=2960;$z=$z+10){
$url='https://www.bebiklub.pl/home/nowe-bebiko-junior/odpowiedzi-konkursowe/?start='.$z;
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
$in = curl_exec($ch);
curl_close($ch);

preg_match_all('/data-votes="(.*)" data-name="(.*)" data-id/i', $in, $out);
$k=count($out[1]);
for($i=0;$i<=$k-1;$i++){
        echo $out[2][$i].';';
        echo $out[1][$i]."\n";
}
}
?>

It will output a list of nicknames and votes in format:
nick;number_of_votes
Afterwards that output can be imported to excel and sorted.

Btw. Don’t forget to vote!

Categories: Bash

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

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

How to download all videos from youtube playlist

26/02/2011 4 comments
#!/bin/sh
n=1
for i in 1 26 51 76 101
do
for video in $(curl -s http://gdata.youtube.com/feeds/api/playlists/PLAYLISTID?start-index="$i" | grep -o "url='http://www.youtube.com/watch?v=[^']*'")
do
id=$(echo $video | sed "s|url\='http://www.youtube.com/watch?v=\(.*\)&amp;.*'|\1|")
wget http://www.youtube.com/watch?v=$id -qO- | sed -n "/fmt_url_map/{s/[\'\"\|]/\n/g;p}" | sed -n '/^fmt_url_map/,/videoplayback/p' | sed -e :a -e '$q;N;5,$D;ba' | tr -d '\n' | sed -e 's/\(.*\),\(.\)\{1,3\}/\1/' | wget -i - -O $n.flv
n=$(( $n + 1 ))
done
done

where PLAYLISTID is a ID taken from playlist url (ex.: http://www.youtube.com/my_playlists?p=1EC20BA340CD627B)

Categories: Bash, Linux, Useful commands

rsync with custom ssh port

24/02/2011 Leave a comment

rsync -r -h –partial –progress -e “ssh -p 43213” napi@9.9.9.9:/home/napi/files/* .

Categories: Bash, Linux, Useful commands