Merge lp:~pedronis/tanuki-agent/rpi2-provkit-drive-ssh-like-a-robotot-w-pty into lp:tanuki-agent

Proposed by Samuele Pedroni
Status: Merged
Approved by: Celso Providelo
Approved revision: 124
Merged at revision: 125
Proposed branch: lp:~pedronis/tanuki-agent/rpi2-provkit-drive-ssh-like-a-robotot-w-pty
Merge into: lp:tanuki-agent
Diff against target: 99 lines (+52/-18)
2 files modified
rpi2-sample-provkit/provision (+38/-18)
rpi2-sample-provkit/sshrobot (+14/-0)
To merge this branch: bzr merge lp:~pedronis/tanuki-agent/rpi2-provkit-drive-ssh-like-a-robotot-w-pty
Reviewer Review Type Date Requested Status
Celso Providelo (community) Approve
Review via email: mp+275455@code.launchpad.net

Commit message

use a wrapper for ssh using pty to be able to pass passwords programmatically, avoid requiring paramiko

Description of the change

use a wrapper for ssh using pty to be able to pass passwords programmatically, avoid requiring paramiko

To post a comment you must log in.
Revision history for this message
Celso Providelo (cprov) wrote :

Works like a charm! thanks Samuele.

review: Approve
Revision history for this message
Celso Providelo (cprov) wrote :

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'rpi2-sample-provkit/provision'
--- rpi2-sample-provkit/provision 2015-10-16 20:17:20 +0000
+++ rpi2-sample-provkit/provision 2015-10-22 20:37:04 +0000
@@ -6,10 +6,9 @@
6import codecs6import codecs
7import json7import json
8import os8import os
9import subprocess
9import time10import time
1011
11import paramiko
12
13MAX_RETRIES = 512MAX_RETRIES = 5
1413
1514
@@ -29,27 +28,48 @@
29"""28"""
3029
3130
31def run_sshrobot(device_address, port, cmd, password='ubuntu'):
32 """Run a command using sshrobot helper."""
33 sshrobot = os.path.join(os.path.dirname(__file__), 'sshrobot')
34 invoke = [sshrobot, '-l', 'ubuntu', '-p', str(port), device_address, cmd]
35 p = subprocess.Popen(invoke, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
36 buf = ""
37 while True:
38 r = p.poll()
39 if r is not None:
40 out = (buf + p.stdout.read().decode("utf8")).strip()
41 if out:
42 print(out)
43 return r
44 ch = p.stdout.read(1).decode("utf8")
45 buf += ch
46 if buf.endswith(" password: "):
47 break
48 p.stdin.write(password + "\n")
49 p.stdin.flush()
50 r = p.wait()
51 out = p.stdout.read().decode("utf8").strip()
52 if out:
53 print(out)
54 return r
55
56
32def wait_device(device_address, port):57def wait_device(device_address, port):
33 """Wait for device to answer SSH."""58 """Wait for device to answer SSH."""
34 ssh_identity = os.getenv('SSH_IDENTITY', '~/.ssh/id_rsa.pub')59 ssh_identity = os.getenv('SSH_IDENTITY', '~/.ssh/id_rsa.pub')
35 client = paramiko.SSHClient()60 pubpath = os.path.expanduser(ssh_identity)
36 client.set_missing_host_key_policy(paramiko.client.MissingHostKeyPolicy())61 with open(pubpath, "rb") as fh:
62 pubcontent = fh.read()
63 cmd = "echo '" + pubcontent + b"' >> ~/.ssh/authorized_keys"
37 for attempt_number in range(1, MAX_RETRIES + 1):64 for attempt_number in range(1, MAX_RETRIES + 1):
38 try:65 res = run_sshrobot(device_address, port, cmd)
39 client.connect(device_address, port=port, username="ubuntu", password="ubuntu",66 if res == 0:
40 look_for_keys=False, allow_agent=False)
41 except:
42 if attempt_number == MAX_RETRIES:
43 break
44 print(" device still not answering, waiting and retrying #{}...".format(
45 attempt_number))
46 time.sleep(10)
47 else:
48 pubpath = os.path.expanduser(ssh_identity)
49 with open(pubpath, "rb") as fh:
50 pubcontent = fh.read()
51 client.exec_command(b"echo '" + pubcontent + b"' >> ~/.ssh/authorized_keys")
52 return67 return
68 if attempt_number == MAX_RETRIES:
69 break
70 print(" device still not answering, waiting and retrying #{}...".format(
71 attempt_number))
72 time.sleep(10)
5373
54 # we never reached the raspi in all attempts74 # we never reached the raspi in all attempts
55 print(" device never answered, quitting!")75 print(" device never answered, quitting!")
5676
=== added file 'rpi2-sample-provkit/sshrobot'
--- rpi2-sample-provkit/sshrobot 1970-01-01 00:00:00 +0000
+++ rpi2-sample-provkit/sshrobot 2015-10-22 20:37:04 +0000
@@ -0,0 +1,14 @@
1#!/usr/bin/python2
2import os
3import pty
4import sys
5
6argv = sys.argv[:]
7argv[0] = '/usr/bin/ssh'
8pty.spawn(argv)
9_, r = os.wait()
10if os.WIFEXITED(r):
11 r = os.WEXITSTATUS(r)
12else:
13 r = 255
14sys.exit(r)

Subscribers

People subscribed via source and target branches

to all changes: