pwnies/pwntools — pull request #38 by zachriggle (2014-07-25 21:43:11+00:00)
Title: Permit .command and [command] access to SSH channels

Adds a few enhancements. I know this is now the outdated branch, but it's hopefully something that'll get ported.

I find myself doing this a lot:

```python
result = channel.run('command arg1 arg2 arg3').recvall().split()
```

I have made this easier, and made a few ways to do it. All of the following are functionally equivalent to the above line.

```python
result = channel.command('arg1 arg2 arg3')
result = channel.command(['arg1', 'arg2 arg3'])
result = channel['command arg1 arg2 arg3']
```

The obvious example is something like `channel.ls(dir)` or `channel.ps('aux')`.

Each case has its uses. The first is the simplest. The second is useful when one of the arguments is in a variable, and you don't want to mess with escaping the characters. The third is just a shorthand for `.run(...).recvall().split()`.

Additionally, I've added scoping for the `with` statement. I've run into a few situations where the number of channels were administratively limited, and adding `channel.close()` messed up the code flow due to the need to do it in exception handlers. Now the following is possible, and `.close()` is automatically called if anything in the block throws an exception.

```python
for i in range(1000):
with channel.run(binary) as bin:
bin.sendline(stuff)
throw_exception
```