Exemple de code sur comment créer un tunnel ssh et lancer des commandes sur une serveur distant en utilisant pexpect de python:
import pexpecthost = "toto@machine_b.fr"child = pexpect.spawn('ssh toto@machine_b.fr')print('----- password ------')child.expect("password:")child.sendline('my_password')child.expect ('[#\$] ')print('----- command 1 ------')child.sendline('ls -l')child.expect ('[#\$] ')print(child.before)print('----- command 2 ------')child.sendline('pwd')child.expect ('[#\$] ')print(child.before)child.close()
Note: voir cet article pour convertir des octets (bytes) en chaîne de caractères (string) sous python.
Références
| Liens | Site |
|---|---|
| API Overview | github.com |
| pexpect-u 2.5.1 | pypi.python.org |
| Python how to read output from pexpect child? | stackoverflow |
| Execute a script located on a remote server over ssh using python | github.com |
| ssh_tunnel.py | github.com |
| pexpect — Spawn child applications and control them automatically | bx.psu.edu |
| Execute multiple commands with pexpect | stackoverflow |
| sending more commands to pexpect child after spawn | stackoverflow |
| pexpect python throw error | stackoverflow |
