forked from mattmartini/tivotool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTivoUnit.pm
executable file
·221 lines (183 loc) · 4.34 KB
/
TivoUnit.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# Representation of a Tivo Unit
package TivoUnit;
#use strict;
use warnings;
use TTConfig;
use Data::Dumper;
use Net::Ping;
use Net::Telnet;
use IO::Socket;
use Net::Rendezvous;
use Wx;
$|++;
my $c = $TTConfig::config;
my $vs = $c->vstream();
sub new
{
my $class = shift;
my $self = {};
bless($self, $class);
return $self;
}
#######################################################
# Run a shell command on this tivo
#######################################################
sub DoTelnet
{
my $command = shift;
my @results = undef;
$host = $c->tivoip();
# If host is up
my $t = new Net::Telnet ( Errmode => "return",);
# Try to telnet in
$t->open("$host");
my $success = $t->cmd("/bin/touch"); # Sanity check
# If working, run and return command results
if ($success)
{
@results = $t->cmd( String => "$command", Prompt => '/bash-2.02#/');
}
return @results;
}
#######################################################
# Scan subnet for vserver (port 8074) using Net::Ping
#######################################################
sub ScanSubnet
{
my $self = shift;
my @ips = @_;
my @alive = ();
my @serving = ();
main::TTDebug("to scan @ips");
# Like tcp protocol, but with many hosts
$p = Net::Ping->new("syn");
$p->{port_num} = 8074;
foreach my $ipaddr (@ips)
{
foreach my $block (128..254)
{
$ipaddr =~ m/(\d+)\.(\d+)\.(\d+)\.(\d+)/;
my $h = "$1.$2.$3.$block";
$p->ping($h);
}
while (my ($host, $rtt, $ip) = $p->ack)
{
main::TTDebug("HOST: $host [$ip] ACKed in $rtt seconds.");
push(@alive,$ip);
}
foreach my $block (1..127)
{
$ipaddr =~ m/(\d+)\.(\d+)\.(\d+)\.(\d+)/;
my $h = "$1.$2.$3.$block";
$p->ping($h);
}
while (my ($host, $rtt, $ip) = $p->ack)
{
main::TTDebug("HOST: $host [$ip] ACKed in $rtt seconds.");
push(@alive,$ip);
}
}
foreach my $a (@alive)
{
my $sock = new IO::Socket::INET(PeerAddr => "$a", PeerPort => '8074', Proto => 'tcp', Timeout => '4');
if ($sock)
{
main::TTDebug("Vserver up on $a");
push @serving, $a;
}
}
return $serving[0];
}
#######################################################
# Check if vserver is up
#######################################################
sub IsVserverUp
{
my $self = shift;
$host = $c->tivoip();
my $sock = new IO::Socket::INET (
PeerAddr => "$host",
PeerPort => '8074',
Proto => 'tcp',
Timeout => '3',
);
if ($sock) # We are able to connect to port 8074, vserver is up
{
close($sock);
return 1;
}
else # vserver is not up
{
return 0;
}
}
#######################################################
# Delete recordings using RubbishObjectByFsId
#######################################################
sub Delete
{
my ($self, $fsid) = @_;
# Do some sanity checking on this
if ($fsid =~ m/^\d+$/)
{
my @r = DoTelnet("echo \'RubbishObjectByFsId $fsid\' | tivosh");
return @r;
}
return 0;
}
#######################################################
# Check if Tivo is pingable
#######################################################
sub IsUp
{
my $self = shift;
my $p = Net::Ping->new();
$host = $c->tivoip();
Wx::SafeYield();
if ($p->ping($host))
{
$p->close();
return 1;
}
else
{
$p->close();
return 0;
}
}
#######################################################
# Discover Series2 SA Tivos using Bonjour
#######################################################
sub Discover
{
my @ips = ();
my $res = Net::Rendezvous->new('tivo_videos');
$res->discover;
Wx::SafeYield();
foreach my $entry ($res->entries)
{
my $i = $entry->address;
push(@ips, $i);
}
return @ips;
}
#######################################################
# Start and Stop Vserver
#######################################################
sub StartVserver
{
my $host = $c->tivoip();
my $loc = $c->vserverpath();
my @results = DoTelnet("echo \"$loc &\" > /var/tmp/tmpscript && /bin/bash /var/tmp/tmpscript && sleep 2");
main::TTDebug("Remote vserver start:", @results);
return @results;
}
sub StopVserver
{
my $host = $c->tivoip();
my $bb = $c->bbpath();
my @results = DoTelnet("$bb killall vserver");
main::TTDebug("Remote vserver stop:", @results);
return $results[0];
}
1;