This depends on magic escape characters and can possibly suffer from injection.

Please refactor to something like this:

```
self.hosts.run(self._get_management_command('shell --plain'), initial_input = '%s\nimport sys; sys.exit(0)' % code)
```
That should already work out of the box.

Better would even be, to add a boolean option to Host._run and Host._posix_shell called "send_EOF_after_initial_input"
If True, send Ctrl-D after the initial input.

Then it becomes:
```
self.hosts.run(self._get_management_command('shell --plain'), initial_input=code, send_EOF_after_initial_input=True)
```

Not sure about the naming, but that's the idea.