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
1=== modified file 'rpi2-sample-provkit/provision'
2--- rpi2-sample-provkit/provision 2015-10-16 20:17:20 +0000
3+++ rpi2-sample-provkit/provision 2015-10-22 20:37:04 +0000
4@@ -6,10 +6,9 @@
5 import codecs
6 import json
7 import os
8+import subprocess
9 import time
10
11-import paramiko
12-
13 MAX_RETRIES = 5
14
15
16@@ -29,27 +28,48 @@
17 """
18
19
20+def run_sshrobot(device_address, port, cmd, password='ubuntu'):
21+ """Run a command using sshrobot helper."""
22+ sshrobot = os.path.join(os.path.dirname(__file__), 'sshrobot')
23+ invoke = [sshrobot, '-l', 'ubuntu', '-p', str(port), device_address, cmd]
24+ p = subprocess.Popen(invoke, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
25+ buf = ""
26+ while True:
27+ r = p.poll()
28+ if r is not None:
29+ out = (buf + p.stdout.read().decode("utf8")).strip()
30+ if out:
31+ print(out)
32+ return r
33+ ch = p.stdout.read(1).decode("utf8")
34+ buf += ch
35+ if buf.endswith(" password: "):
36+ break
37+ p.stdin.write(password + "\n")
38+ p.stdin.flush()
39+ r = p.wait()
40+ out = p.stdout.read().decode("utf8").strip()
41+ if out:
42+ print(out)
43+ return r
44+
45+
46 def wait_device(device_address, port):
47 """Wait for device to answer SSH."""
48 ssh_identity = os.getenv('SSH_IDENTITY', '~/.ssh/id_rsa.pub')
49- client = paramiko.SSHClient()
50- client.set_missing_host_key_policy(paramiko.client.MissingHostKeyPolicy())
51+ pubpath = os.path.expanduser(ssh_identity)
52+ with open(pubpath, "rb") as fh:
53+ pubcontent = fh.read()
54+ cmd = "echo '" + pubcontent + b"' >> ~/.ssh/authorized_keys"
55 for attempt_number in range(1, MAX_RETRIES + 1):
56- try:
57- client.connect(device_address, port=port, username="ubuntu", password="ubuntu",
58- look_for_keys=False, allow_agent=False)
59- except:
60- if attempt_number == MAX_RETRIES:
61- break
62- print(" device still not answering, waiting and retrying #{}...".format(
63- attempt_number))
64- time.sleep(10)
65- else:
66- pubpath = os.path.expanduser(ssh_identity)
67- with open(pubpath, "rb") as fh:
68- pubcontent = fh.read()
69- client.exec_command(b"echo '" + pubcontent + b"' >> ~/.ssh/authorized_keys")
70+ res = run_sshrobot(device_address, port, cmd)
71+ if res == 0:
72 return
73+ if attempt_number == MAX_RETRIES:
74+ break
75+ print(" device still not answering, waiting and retrying #{}...".format(
76+ attempt_number))
77+ time.sleep(10)
78
79 # we never reached the raspi in all attempts
80 print(" device never answered, quitting!")
81
82=== added file 'rpi2-sample-provkit/sshrobot'
83--- rpi2-sample-provkit/sshrobot 1970-01-01 00:00:00 +0000
84+++ rpi2-sample-provkit/sshrobot 2015-10-22 20:37:04 +0000
85@@ -0,0 +1,14 @@
86+#!/usr/bin/python2
87+import os
88+import pty
89+import sys
90+
91+argv = sys.argv[:]
92+argv[0] = '/usr/bin/ssh'
93+pty.spawn(argv)
94+_, r = os.wait()
95+if os.WIFEXITED(r):
96+ r = os.WEXITSTATUS(r)
97+else:
98+ r = 255
99+sys.exit(r)

Subscribers

People subscribed via source and target branches

to all changes: