fabric/fabric — pull request #769 by ToddG (2012-11-03 23:04:53+00:00)
Title: Added tav's execute command as 'exec_remote'

Adds support for executing arbitrary code on the remote server. Shamelessly lifted out of tav's repo (https://github.com/tav/pylibs).

Here's an example:

```python
@task()
def get_ip():
"""get outbound ip address"""

remote_command="""
#! /usr/bin/python
import socket

def get_ip_address():
try:
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(('8.8.8.8', 80))
ip = s.getsockname()[0]
return ip
finally:
s.close()

# exec!
print get_ip_address()
"""
remote_ip = exec_remote(remote_command)
print "remote_ip: " + remote_ip
```

```sh
[todd@quebec-01 openvpn (master)]$ fab get_ip
[None] run: #! /usr/bin/python
import socket

def get_ip_address():
try:
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
#s.connect((GOOGLE_DNS_SERVER_IP_ADDRESS, GOOGLE_DNS_SERVER_PORT))
s.connect(('8.8.8.8', 80))
ip = s.getsockname()[0]
return ip
finally:
s.close()

# exec!
print get_ip_address()
No hosts found. Please specify (single) host string for connection: user@goo
remote_ip: 172.16.44.131

Done.
Disconnecting from goo... done.
```

See his writeup on this at http://tav.espians.com/fabric-python-with-cleaner-api-and-parallel-deployment-support.html.