Ce blog — désormais archivé — est en lecture seule. Pour continuer à lire mes tribulations, rendez-vous sur le blog d'Akei, ma société.

Prendre un Café

L'espace d'expression de Nicolas Perriault

Aller au contenu | Aller au menu | Aller à la recherche

Keyword - cli

Fil des billets

mercredi 4 novembre 2009

An Interactive PHP Shell: phpsh

Just found this nice project contributed by the Facebook team, phpsh. Basically, it’s an interactive and advanced command line interface to php, a kind of super-php -r similar to what you can find in Python or Ruby. Ironically enough, phpsh is mostly written in Python, by the way.

To install phpsh, just get it from github[1]:

$ git clone git://github.com/facebook/phpsh.git
$ cd phpsh
$ python setup.py build
$ sudo python setup.py install

To run phpsh:

$ phpsh
Starting php
type 'h' or 'help' to see instructions & features
php> ="hello world"
hello world
php> =2 + 2
4
php> $a = 8

php> =$a
8

Note that you don’t have to open or close <?php tags, you can print something just by prepending an = sign to the command, and no need to type semicolum at the end of a call. Easy[2].

You can access a function documentation by just prepending the d keyword to its name:


php> d strlen

# strlen

(PHP 4, PHP 5)

strlen -- Get string length

### Description

int strlen ( string $string )

Returns the length of the given string . 

### Parameters

string     

The [string][1] being measured for length. 

### Return Values

The length of the string on success, and 0 if the string is empty. 

### 

   [1]: #language.types.string

You can define your own classes and functions, and run them:

php> function foo($a){echo $a.'!';}
php> foo('bar')
bar!

Same goes for classes:

php> class Foo {public function bar($a){echo $a.'!';}}
php> $f = new Foo()
php> $f->bar('baz')
baz!

You can even execute shell command from there:

php> ! ls -la

Of course, you can import include and require files. As a concrete example, let’s roughly play with the symfony API:

php> ! symfony -V
symfony version 1.2.2-DEV (/Users/niko/Sites/vendor/symfony12/lib)
php> c /Users/niko/Sites/vendor/symfony12/lib/autoload/sfCoreAutoload.class.php
Extra includes are: ['/Users/niko/Sites/vendor/symfony12/lib/autoload/sfCoreAutoload.class.php']
php> sfCoreAutoload::register()
php> =sfYaml::load('foo: bar')
array(
  "foo" => "bar",
)
php> =sfYaml::dump(array('foo' => array('bar' => 'baz')))
"foo:\n  bar: baz\n"

Last but not least, when enough playing, type q or press ctrl + d to quit phpsh.

Notes

[1] So yeah, so you need git. And Python, of course.

[2] Anybody putting "rather use python then" will have serious troubles, even if I strongly agree.

jeudi 18 janvier 2007

Synchro distante automatique via un daemon rsync sur Ubuntu

Et non ! Rsync n'est pas une voiture des années 90 mais bien un outil de synchronisation de fichiers à distance très puissant. Il peut notamment tourner en daemon afin d'autoriser les appels distants automatiques, via Cron par exemple.

Si par un hasard hallucinant vous ne disposiez pas de rsync sur votre Ubuntu, voici la démarche à suivre pour l'installer en ligne de commande :

$ sudo apt-get install rsync

Bien. Nous disposons de deux machines, la machine A - disposant d'un utilisateur jeanpierre - et la machine B - habitée par edmond. Nous devons automatiser la synchro d'un répertoire présent sur la machine A vers la machine B. On va donc installer un daemon rsync sur la machine A et le configurer en créant le fichier /etc/rsyncd.conf :

uid             = jeanpierre 
gid             = jeanpierre
use chroot      = no    
max connections = 4
syslog facility = local5
pid file        = /var/run/rsyncd.pid

[mon_partage]
  comment = Mon partage
  path = /home/jeanpierre/monpartage
  read only = yes
  list = yes
  hosts allow = *
  auth users = edmond, gerard
  secrets file = /etc/rsyncd.secrets

Je vous renvoie à cette page sur la syntaxe du fichier rsync.conf pour plus de renseignements.

On crée un fichier process vide :

jeanpierre@machineA:~$ sudo touch /var/run/rsyncd.pid

Les comptes utilisateurs se définissent dans le fichier /etc/rsyncd.secrets sous la traditionnelle forme login:motdepasse comme ci-dessous :

edmond:3dm0nd
gerard:g3r4rd

Par souci de sécurité et de confidentialité, ces fichiers ne devront pas être lisibles par un autre utilisateur que le root, donc :

jeanpierre@machineA:~$ sudo chmod 640 /etc/rsyncd.conf
jeanpierre@machineA:~$ sudo chmod 640 /etc/rsyncd.secrets

Lancer rsync en daemon sur la machine A (machinea.domain.tld) :

jeanpierre@machineA:~$ sudo rsync --daemon

Et depuis la machine B (machineb.domain.tld) :

edmond@machineB:~$ rsync -avz edmond@machinea.domain.tld::mon_partage ~/mon/repertoire/cible

La saisie du password de l'utilisateur edmond défini précedemment lancera la synchronisation effective des fichiers. Pour éviter la saisie manuelle du password, il faut envisager d'utiliser l'option --password-file=FILE. Pour se faire, il faut créer un fichier contenant juste le mot de passe en clair, par exemple dans /home/edmond/.rsync_passwd comme ceci :

3dm0nd

Ce fichier devra n'être accessible que par l'utilisateur système utilisant la commande rsync. Dans le cas de l'utilisateur connecté à la machine B :

edmond@machineB:~$ chmod 640 /home/edmond/.rsync_passwd

On peut alors lancer une synchro de cette façon :

edmond@machineB:~$ rsync -avz edmond@machinea.domain.tld::mon_partage ~/mon/repertoire/cible --password-file=/home/edmond/.rsync_passwd

L'avantage de cette ligne de commande est qu'elle peut être appellée depuis un Cron et donc ne pas nécessiter de saisie du mot de passe manuellement.

Enfin, sachez que les éventuelles erreurs d'authentification seront logguées dans le fichier /var/log/syslog.