1
|
import sys, getopt, paramiko, socket
|
2
|
|
3
|
class HDHomeRun(object):
|
4
|
def __init__(self, deviceId=None):
|
5
|
self.deviceId = deviceId
|
6
|
self.app = 'hdhomerun_config'
|
7
|
|
8
|
def getDiscoverCommand(self):
|
9
|
ret = '{0} discover'.format(self.app)
|
10
|
return ret
|
11
|
|
12
|
def getRebootCommand(self):
|
13
|
ret = False
|
14
|
if self.deviceId is not None:
|
15
|
ret = '{0} {1} set /sys/restart self'.format(self.app, self.deviceId)
|
16
|
return ret
|
17
|
|
18
|
def main(argv):
|
19
|
#-- process the arguments --#
|
20
|
settings = {}
|
21
|
try:
|
22
|
opts, args = getopt.getopt(argv, 's:u:p:', ['ssh-host=', 'ssh-user=', 'ssh-pass='])
|
23
|
except getopt.GetoptError:
|
24
|
sys.exit('Invalid arguments')
|
25
|
|
26
|
for opt, arg in opts:
|
27
|
if opt in ('-s', '--ssh-host'):
|
28
|
settings['ssh-host'] = arg
|
29
|
if opt in ('-u', '--ssh-user'):
|
30
|
settings['ssh-user'] = arg
|
31
|
if opt in ('-p', '--ssh-pass'):
|
32
|
settings['ssh-pass'] = arg
|
33
|
|
34
|
if 'ssh-host' in settings:
|
35
|
if all(itm in settings.keys() for itm in ['ssh-user', 'ssh-pass']):
|
36
|
ssh = paramiko.SSHClient()
|
37
|
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
38
|
try:
|
39
|
print 'Connecting to {0}... '.format(settings['ssh-host']),
|
40
|
ssh.connect(settings['ssh-host'], username=settings['ssh-user'], password=settings['ssh-pass'])
|
41
|
except (paramiko.ssh_exception.AuthenticationException, socket.error) as e:
|
42
|
print '{0}'.format(e)
|
43
|
else:
|
44
|
print 'Done'
|
45
|
print 'Discovering HDHomeRun devices... ',
|
46
|
stdin, stdout, stderr = ssh.exec_command(HDHomeRun().getDiscoverCommand())
|
47
|
lstDevices = stdout.readlines()
|
48
|
lstErr = stderr.readlines()
|
49
|
if len(lstErr):
|
50
|
print 'Failed ({0})'.format('--'.join(lstErr).strip())
|
51
|
elif len(lstDevices):
|
52
|
if lstDevices[0].strip() == 'no devices found':
|
53
|
print '{0}'.format(lstDevices[0].strip())
|
54
|
else:
|
55
|
print '{0} device{1} found'.format(len(lstDevices), 's' if len(lstDevices) != 1 else '')
|
56
|
for l in lstDevices:
|
57
|
if l.strip() != 'no devices found':
|
58
|
hdDeviceId = l.split(' ')[2].strip()
|
59
|
hdDeviceAddress = l.split(' ')[5].strip()
|
60
|
print 'Sending restart to {0} with address {1}... '.format(hdDeviceId, hdDeviceAddress),
|
61
|
cmd = HDHomeRun(hdDeviceId).getRebootCommand()
|
62
|
if cmd:
|
63
|
stdin, stdout, stderr = ssh.exec_command(cmd)
|
64
|
lstOut = stdout.readlines()
|
65
|
lstErr = stderr.readlines()
|
66
|
if len(lstErr):
|
67
|
print 'Failed ({0})'.format('--'.join(lstErr).strip())
|
68
|
else:
|
69
|
print 'Done'
|
70
|
ssh.close()
|
71
|
else:
|
72
|
print 'You must provide a username and password for the SSH connection'
|
73
|
else:
|
74
|
print 'Currently not implemented using localhost to execute'
|
75
|
|
76
|
if __name__ == "__main__":
|
77
|
main(sys.argv[1:])
|