Scripts | Codes

All languages in three languages :-)
Showing posts with label extraire. Show all posts

Extraire des liens d'un fichier (images, pages HTML, pdf, ...) et l'affiche dans le Terminal ou les télécharger avec wget (en une seul ligne).
Vous pouvez choisir les extensions que vous voulez extraire entre les dernières parenthèses séparées par des |
Dans l'exemple on extrait les lien des images d'un fichier index.php puis on les télécharge
On utilise un RegExp de type Perl avec -P

Extract links from a file (images, HTML pages, pdf, ...) and displays them in the Terminal or downloads them with wget (in only one line of code).
You can choose which extensions you want to extract by specifying them inside the last parentheses separated by |
In the example we extract the images link to a index.php file and then downloads them
It uses a Perl-type RegExp with -P

يستخرج كل أنواع الروابط من ملف في Terminal
ثم يمكن تحميلها بإستعمال wget

يمكنك إختيار التمديدات بكتابتها بين القوسين مفصولة ب |
في هذا المثال نستخرج كل روابط الصور ثم نحملها 
يستخدم ريجيكس من نوع Perl 

Open in a new window
wget `cat index.php | grep -P -o 'http:(\.|-|\/|\w)*\.(gif|jpg|png|bmp)'`


Extrait les liens de chaque page et retrouves les liens dans ces nouvelles pages...
Il faut créer un fichiers links.dat dans le même répertoire et y mettre les liens

Extract links from each page and find the links in these news pages ...
One should create a file links.dat and put links inside

يستخرج الروابط من كل صفحة يجدها في links.dat ثم يستخرج الروابط الجديدة الموجودة في هذه الصحف 

ينبغي إنشاء ملف links.dat و وضع الروابط فيه 

Open in a new window
<?php
//################################################
// for more codes scripts-n-codes.blogspot.com
//################################################
//
// put the links to crawl in a links.dat file; you can put one site utl for example
//
$datafile = "links.dat"; // file to keep the list of links in
$regex = "/<\s*a\s+[^>]*href\s*=\s*[\"']?([^\"' >]+)[\"' >]/isU";  // regex to search for hrefs

$handle = fopen($datafile, "r"); // open the data file
$buffer = fgets($handle, 4096);
$oldlinks[] = $buffer; // read the first link into an array
while (!feof($handle)) {
 $buffer = fgets($handle, 4096);
 array_push($oldlinks,$buffer); // read the rest of the links into an array
}
fclose($handle); // close the data file

foreach($oldlinks as $value) { // for every link in the array
 print $value; // print it out
 $remote = fopen(trim($value), "r") or die(); //open it or fail nicely
 while (!feof($remote)) {
  $html = fread($remote, 8192); // read in the remote page
 }
 fclose($remote); // close it
 if (preg_match_all($regex, $html, $links)) { // if we find new links
  $local = fopen($datafile, "a+"); // open the data file
  foreach($links[1] as $value) { // for every new link
   $value.="\n"; // append a new line
   if(!in_array($value,$oldlinks)) { // if we haven't seen it before (nb - case sensitive)
    print($value); // print it out
    fwrite($local, $value); // and write it to file
   }
  }
  fclose($local); // close the data file
 }
 else {
  print("No links."); // we didn't find any links in the new file
 }
}
?>


Permet de récupérer les cookies d'un site dans Firefox...
Utilisation:
- D'abord l'enregistrer sous un fichier appelé par exemple cooky.py
- Modifier dans les deux lignes suivantes:
cookiedb = '{$HOME}/.mozilla/firefox/vkuuxfit.default/cookies.sqlite'
targetfile = '{$HOME}/Bureau/cookies.txt'
- {$HOME} par le chemin vers votre dossier personnel : par exemple pour moi /home/ubu
- le nom un peu bizzare "vkuuxfit.default" par celui que vous trouverez dans le repertoire de ff, c-à-d ici : {$HOME}/.mozilla/firefox , normalement il ne devrait y'en avoir qu'un seul, celui finissant par "default" sinon si vous avez crée d'autres profiles, mettre le nom du répertoire du profile par lequel vous vous être connécté au site que vous voulez télécharger (par login et pwd) ... vous devriez facilement reconnaitre le dossier, car son nom finit par ".nom_du_profile".
Si vous voulez changer le nom du repertoire où sera stocké le fichier cookies.txt libre à vous.
- Exécuter le script avec le nom du site en premier argument, par exemple je veux récupérer les cookies du site "siteduzero.com" je tape: python cooke.py siteduzero.com
et j'aurais un fichier cookies.txt comme ceci :
-----------------------------------------------------
www.siteduzero.com TRUE / FALSE 1279648744 pseudo ubu
www.siteduzero.com TRUE / FALSE 1279648744 pass 8da0432739423d4ab36c3108a3abahc0187e7c27
www.siteduzero.com TRUE / FALSE 1279648744 hash 02af28288e9c0747be5402f48fg17788863bf423
www.siteduzero.com TRUE / FALSE 1279648744 mid2 11569111567
-----------------------------------------------------
Ce qui veut dire que l'on a les 4 variables avec les valeurs:
pseudo=ubu
pass=8da0432739423d4ab36c3108a3abahc0187e7c27
hash=02af28288e9c0747be5402f48fg17788863bf423
mid2=115691115677

To retrieve cookies of a site in Firefox ...
Usage: 
- First, save it under a file called eg. cooky.py 
- Modify the following two lines: 
cookiedb = '$(HOME)/.mozilla/firefox/vkuuxfit.default/cookies.sqlite' 
targetfile = '$(HOME)/Desktop/cookies.txt' 
- $(HOME) with the path to your home directory: for example for me /home/ubu 
- The name a bit weired "vkuuxfit.default" by the one you found in the repertoire of ff, ie here: $ (HOME)/.mozilla/firefox, you should find only one, the one ending with the string "default", if not you probalby created some other profiles, put the name of the directory of the profile which you'll use to be connected to the site. You should easily recognize the file because its name ends with ".name_of_profile. 
If you want to rename the directory where the file will be stored cookies.txt free to you. 
- Now to run the script: 
- Run the script with the name of the site as a first argument, for example I want to retrieve cookies from the site "siteduzero.com" I type: python cooky.py siteduzero.com 
and I would have a cookies.txt file like this: 
------------------------------------------------------ 
www.siteduzero.com TRUE / FALSE 1279648744 nickname ubu 
www.siteduzero.com TRUE / FALSE 1279648744 pass 8da0432739423d4ab36c3108a3abahc0187e7c27 
www.siteduzero.com TRUE / FALSE 1279648744 hash 
------------------------------------------------------
This means that we have the 4 variables with values:
nickname = ubu
pass = 8da0432739423d4ab36c3108a3abahc0187e7c27
hash = 02af28288e9c0747be5402f48fg17788863bf423
Mid2 = 115,691,115,677

لإستخراج ملفات تعريف الارتباط Cookies لموقع ما في فايرفوكس...

أولا احفظه تحت ملف مثلا cooky.py
ثانيا تعديل السطرين التاليين :
cookiedb = '{$HOME}/.mozilla/firefox/vkuuxfit.default/cookies.sqlite'

targetfile = '{$HOME}/Bureau/cookies.txt'
حيث تعوض $HOME  بمسار ملفك الشخصي و vkuuxfit بإسم ملف Firefox 
ثالثا تشغيل البرنامج النصي python cooky.py mysite.com
نتحصل على ملف cookies.txt

Open in a new window
#!/usr/bin/python
import sqlite3 as db
import sys
cookiedb = '{$HOME}/.mozilla/firefox/vkuuxfit.default/cookies.sqlite'
targetfile = '{$HOME}/Bureau/cookies.txt'
what = sys.argv[1]
connection = db.connect(cookiedb)
cursor = connection.cursor()
contents = "host, path, isSecure, expiry, name, value"
cursor.execute("SELECT " +contents+ " FROM moz_cookies WHERE host LIKE '%" 
+what+ "%'")
file = open(targetfile, 'w')
index = 0
for row in cursor.fetchall():
file.write("%s\tTRUE\t%s\t%s\t%d\t%s\t%s\n" % (row[0], row[1],
str(bool(row[2])).upper(), row[3], str(row[4]), str(row[5])))
index += 1
file.close()
connection.close()
print "Keywords: %s" % what
print "Exported: %d" % index



Extraire le système d'exploitation à partir de l'entête HTTP reçu...

... À appeler sans arguments

Extract the operating system from the received HTTP header...

... Call without arguments.

HTTP استخراج نظام التشغيل من رأس الطلب 

تنادى بدون معطيات

Open in a new window
<?
//#################################################
// find more codes on scripts-n-codes.blogspot.com
//#################################################
function extract_os() {
 $user_parts = explode(";", $_SERVER['HTTP_USER_AGENT']);
 $os = trim($user_parts[2]);
 $os = str_replace(')', '', $os);
 if($os == '') $os = 'unknown';
 return $os;  
}
?>

Subscribe to: Posts (Atom)
attendez....