(x)Debug PHP CLI script via SSH without port forwarding

There are lots of examples on the net which all tell you that you should forward port 9000 to your local machine. This is helpful and needed if you want to debug a "real" remote machine, like a production or staging server.

But I want to debug a CLI script on a virtual machine (VM) and want a script which connects back on my local network to my host machine.

I know puphpet is doing this with the builtin xdebug script, but I can't find it on the net, so here it is:

#!/bin/bash
XDEBUG_CONFIG="idekey=xdebug" php -dxdebug.remote_host=`echo $SSH_CLIENT | cut -d "=" -f 2 | awk '{print    $1}'` "$@"

Just put it in a file ~/bin/xdebug and make it executable, then you can run your cli file with

xdebug file.php

and it should reconnect to your PHPStorm (or whatever other IDE you use).