-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathpjl-file.pl
420 lines (365 loc) · 8.76 KB
/
pjl-file.pl
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
#!/usr/bin/perl
use IO::Socket;
use Getopt::Long;
# Do CLI options
my $useuel=1,$listdisk=0,$target;
$socket, $directory;
my $result=GetOptions
(
'useuel!' => \$useuel,
'listdisk!' => \$listdisk,
'host=s' => \$target,
);
if ((!defined $target) && (defined $ARGV[0])) { $target=$ARGV[0] };
if (defined $target)
{
# target passed on the CLI - open it immediately
$socket=opensocket($target);
}
my $finish=0;
my %cmdhash=
(
'open' => \&cmd_open,
'quit' => \&cmd_quit,
'bye' => \&cmd_quit,
'drives' => \&cmd_drives,
'dir' => \&cmd_dir,
'ls' => \&cmd_dir,
'cd' => \&cmd_cd,
'get' => \&cmd_get,
'put' => \&cmd_put,
'mkdir' => \&cmd_mkdir,
'del' => \&cmd_del,
'rm' => \&cmd_del,
);
while (!$finished)
{
print "Current dir: $directory\n";
print "pjlftp> ";
my $command=<STDIN>;
chomp($command);
my @cmdlist=split(/ /,$command);
$cmdlist[0]=lc($cmdlist[0]);
while(my($cmd,$func) = (each %cmdhash))
{
if ($cmd eq $cmdlist[0])
{
shift(@cmdlist);
$finished=&$func(@cmdlist);
}
}
}
exit(0);
foreach my $check (@checks)
{
@response=sendprinter($socket, $check->{PJL});
if ($check->{name} eq "Filesystem")
{
# Special parsing for the filesystem
shift(@response);
}
foreach my $line (@response)
{
# remove the \n
chomp($line);
$line =~ s/\"//g;
$line =~ s/^\s+//;
if ($check->{name} eq "Filesystem")
{
my @files=split(/\s+/,$line);
print "$check->{name} $files[0]\n";
print " Total Size: $files[1]\n";
print " Free Space: $files[2]\n";
print " Location: $files[3]\n";
print " Label: $files[4]\n";
if ($listdisk)
{
print " FILE\t\t\tTYPE\t\tSIZE\n";
my @objects=displaydir($socket,1,$files[0] . '\\');
}
}
else
{
print "$check->{name}: ${line}\n";
}
}
}
close($socket);
exit(0);
sub sendprinter
{
my $socket=$_[0];
my $command='@PJL ' . $_[1];
my @strings;
my $result;
my $escape=pack("ca8",0x1b,'%-12345X');
my $term = "\r\n";
my $send = $command . $term;
if ($useuel)
{
$send=$escape . $command . $term . $escape;
}
#print("$command\n");
$socket->send("@PJL\r\n");
$socket->send($send);
# New read the response from the printer
my $bytes;
my $firstnl=0;
if ($command =~ /PJL FSUPLOAD/)
{
# remember the number of bytes
$bytes=$command;
$bytes =~ s/^.*SIZE=//;
}
while (sysread($socket, $byte, 1) == 1)
{
# end on 0x0c
if ($command =~ /PJL FSUPLOAD/)
{
if ($firstnl == 0 && unpack("c",$byte) == 0x0a)
{
$firstnl=1;
}
# we need to keep a count of bytes
$bytes-- if ($firstnl==1);
last if ($bytes==0);
}
else
{
last if (unpack("c",$byte) == 0x0c);
}
$result .= $byte;
}
@strings=split(/\n/, $result);
# ignore the echo
return @strings;
}
sub displaydir
{
my $socket=$_[0];
my $path=$_[1];
my @objects=cataloguedir($socket,$path);
if ($#objects < 0)
{
print "<EMPTY>\n";
}
foreach $object (@objects)
{
my $sep=(substr($path,length($path)-1) ne '\\')?'\\':'';
print "$object->{type}\t$object->{size}\t$path$sep$object->{name}\n";
}
}
sub cataloguedir
{
my $socket=$_[0];
my $volume=$_[1];
my @response;
my $base="FSDIRLIST NAME=";
my @objects;
# First, get the list of objects
my $command=$base . '"' . $volume . '"' . " ENTRY=1 COUNT=128";
@response=sendprinter($socket, $command);
shift(@response);
# skip the entry line
shift(@response);
foreach my $line (@response)
{
chomp($line);
my @params=split(/\s+/,$line);
my $type=$params[1];
my $size=$params[2];
$type =~ s/^TYPE=//;
$size =~ s/^SIZE=//;
my $dirent = {
name => $params[0],
type => $type,
size => $size
};
push(@objects,$dirent);
}
return @objects;
}
sub opensocket
{
my $target=$_[0];
my $socket=new IO::Socket::INET (
PeerAddr => $target,
PeerPort => '9100',
Proto => tcp,
);
die "Error: $@\n" unless $socket;
return $socket;
}
sub cmd_open
{
$target=$_[0];
if (defined($target))
{
#close socket if open
if (defined($socket)) { close($socket) };
# open a socket
$socket=opensocket($target);
}
else
{
print("No host provided\n");
}
return 0;
}
sub cmd_close
{
if (defined($socket)) { close($socket) };
return 0;
}
sub cmd_quit
{
return 1;
}
sub cmd_drives
{
# List the drives in the printer
my @response=sendprinter($socket,"INFO FILESYS");
shift(@response);
if ($response[0] =~ '^VOLUME')
{ # skip the first line
shift(@response);
}
foreach my $line (@response)
{
# remove the \n
chomp($line);
$line =~ s/\"//g;
$line =~ s/^\s+//;
my @details=split(/\s+/,$line);
print("$details[0]\n");
}
}
sub cmd_dir
{
# List the contents of the current directory
if (!defined($directory))
{
print("No current directory\n");
return 0;
}
displaydir($socket,$directory);
return 0;
}
sub cmd_cd
{
# Change directory
my $dir=$_[0];
# Add a \ to the end to explicitly check for a \
$dir=$dir . "\\";
if (defined $directory) { $dir=$directory . $dir; };
if (defined $directory && $dir =~ /\.\.\\$/)
{
my $position=rindex($directory,"\\",length($directory)-2);
if ($position > 0)
{
$directory=substr($directory, 0, $position+1);
}
return 0
}
# Now check the directory exists
my @response=sendprinter($socket, 'FSQUERY NAME="' . $dir . '"');
shift(@response);
if (scalar(@response) == 0)
{
$directory=$dir;
}
else
{
print("Directory $dir is invalid\n");
}
return 0;
}
sub cmd_get
{
my $file=$_[0];
# Open the output file to see whether we can
open(OUTFILE,">","$file") or die $!;
binmode(OUTFILE);
# Now we have a file handle we can read the file from the printer
# First check that it exists.
my $size;
my $file = $directory . $file;
my @response=sendprinter($socket, 'FSQUERY NAME="' . $file . '"');
if (scalar(@response) != 1)
{
print("File $file doesn't exist\n");
return 0;
}
else
{
$size=$response[0];
$size =~ s/^.*SIZE=//;
}
my @response=sendprinter($socket, 'FSUPLOAD NAME="' . $file . '" SIZE=' . $size);
shift(@response);
foreach my $item (@response)
{
print OUTFILE "$item\n";
}
close(OUTFILE);
return 0;
}
sub cmd_get
{
my $file=$_[0];
# Open the output file to see whether we can
open(OUTFILE,">","$file") or die $!;
binmode(OUTFILE);
# Now we have a file handle we can read the file from the printer
# First check that it exists.
my $file = $directory . $file;
my @response=sendprinter($socket, 'FSQUERY NAME="' . $file . '"');
if (scalar(@response) != 1)
{
print("File $file doesn't exist\n");
return 0;
}
else
{
$size=$response[0];
$size =~ s/^.*SIZE=//;
}
my @response=sendprinter($socket, 'FSUPLOAD NAME="' . $file . '" SIZE=' . $size);
shift(@response);
foreach my $item (@response)
{
print OUTFILE "$item\n";
}
close(OUTFILE);
return 0;
}
sub cmd_put
{
my $file=$_[0];
# Open the output file to see whether we can
open(INFILE,"<","$file") or die $!;
binmode(INFILE);
# Now we have a file handle we can read the file from the printer
# First check that it exists.
my $size = -s "$file";
my $ofile = $directory . $file;
my $buffer;
my $count=read(INFILE,$buffer,$size);
my @response=sendprinter($socket, 'FSDOWNLOAD FORMAT:BINARY SIZE=' . $size . ' NAME="' . $ofile . '"' . "\r\n" . $buffer);
close(OUTFILE);
return 0;
}
sub cmd_mkdir
{
my $dir=$_[0];
my $dir=$directory . $dir;
my @response=sendprinter($socket, 'FSMKDIR NAME="' . $dir . '"');
return 0;
}
sub cmd_del
{
my $dir=$_[0];
my $dir=$directory . $dir;
my @response=sendprinter($socket, 'FSDELETE NAME="' . $dir . '"');
return 0;
}