File: itest.pl

package info (click to toggle)
libnet-scp-expect-perl 0.13-1
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 42,328 kB
  • ctags: 17
  • sloc: perl: 438; makefile: 41
file content (270 lines) | stat: -rw-r--r-- 7,214 bytes parent folder | download | duplicates (2)
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
########################################################
# Interactive Test - assumes *nix machine on remote end
########################################################
BEGIN{
   use Cwd;
   use File::Basename;
   use vars qw/@INC $dir/;
   $dir = dirname(cwd()) . "/blib/lib";
   unshift(@INC,$dir);
}

use strict;
use Net::SCP::Expect;
use Term::ReadPassword;
use Digest::MD5;
use File::Copy;

print "Using version: ", $Net::SCP::Expect::VERSION, "\n";

my $errors = 0; # Tracks total number of failures.

my $small_file  = "small_file.test";
my $rsmall_file  = "small_file.remote";

my $medium_file = "medium_file.test";
my $rmedium_file = "medium_file.remote";

my $large_file  = "large_file.test";
my $rlarge_file  = "large_file.remote";

print "\nHost to copy test files to/from: ";
my $host = <STDIN>;
chomp $host;

print "Telnet or SSH (t/s): ";
my $proto = <STDIN>;
chomp $proto;

if($proto =~ /[tT]/)
{
   eval{ require Net::Telnet };
   if($@){
      print "You don't appear to have Net::Telnet installed; aborting\n";
      exit;
   }
   print "\n(Using Net::Telnet)\n\n";
}
elsif($proto =~ /[sS]/)
{
   eval{ require Net::SSH::Perl };
   if($@){
      print "You don't appear to have Net::SSH::Perl installed; aborting\n";
      exit;
   }
   print "\n(Using Net::SSH::Perl)\n\n";
}
else
{
   print "Unknown option: [$proto]; aborting\n";
   exit;
}

print "Login: ";
my $login = <STDIN>;
chomp $login;

my $password = read_password('Password: ');
chomp $password;

print "\nAttempting to copy files to [$host] as [$login]...\n\n";

# Modify this as you see fit
my $nse = Net::SCP::Expect->new(
   host     => $host,
   user     => $login,
   password => $password,
   auto_yes => 1,
);

##############################
# Setup our remote connection
##############################
my $rsh;
if($proto eq 's')
{
   $rsh = Net::SSH::Perl->new($host);
}
else
{
   $rsh = Net::Telnet->new($host);
}
$rsh->login($login,$password);

###############################
# login@host:file style syntax
###############################

print "1)Testing 'login\@host:filename' syntax\n";
print "=" x 40;
print "\n";

##################
# local to remote
##################
print "\nAttempting to copy $small_file to remote host\n";
$nse->scp($small_file,"$login\@$host:$rsmall_file");
verify($small_file,$rsmall_file,0);

print "\nAttempting to copy $medium_file to remote host\n";
$nse->scp($medium_file,"$login\@$host:$rmedium_file");
verify($medium_file,$rmedium_file,0);

print "\nAttempting to copy $large_file to remote host\n";
$nse->scp($large_file,"$login\@$host:$rlarge_file");
verify($large_file,$rlarge_file,0);

##################
# remote to local
##################
print "\nAttempting to copy $rsmall_file from remote host\n";
$nse->scp("$login\@$host:$rsmall_file",".");
verify($small_file,$rsmall_file,1);

print "\nAttempting to copy $rmedium_file from remote host\n";
$nse->scp("$login\@$host:$rmedium_file",".");
verify($medium_file,$rmedium_file,1);

print "\nAttempting to copy $rlarge_file from remote host\n";
$nse->scp("$login\@$host:$rlarge_file",".");
verify($large_file,$rlarge_file,1);

###############################################################################
# Note - I only test the small files from here on.  I figure if it worked
# once it will work again.  The primary reason for these tests is to verify
# that the syntax works properly.
###############################################################################

###############################
# host:file style syntax
###############################

print "\n2) Testing 'host:filename' syntax\n";
print "=" x 40;
print "\n";

# local to remote
print "\nAttempting to copy $small_file to remote host\n";
$nse->scp($small_file,"$host:$rsmall_file");
verify($small_file,$rsmall_file,0);

# remote to local
print "\nAttempting to copy $rsmall_file from remote host\n";
$nse->scp("$host:$rsmall_file",".");
verify($small_file,$rsmall_file,1);

#####################
# :file style syntax
#####################

print "\n3) Testing ':filename' syntax\n";
print "=" x 40;
print "\n";

# local to remote
print "\nAttempting to copy $small_file to remote host\n";
$nse->scp($small_file,":$rsmall_file");
verify($small_file,$rsmall_file);

# remote to local
print "\nAttempting to copy $small_file from remote host\n";
$nse->scp(":$rsmall_file",".");
verify($small_file,$rsmall_file,1);

###########################################################################
# : style syntax (note that this syntax is impossible in a remote-to-local
# scenario, so no test is done for that.
###########################################################################

print "\n4) Testing ':' syntax\n";
print "=" x 40;
print "\n";

# local to remote
print "\nAttempting to copy $small_file to remote host\n";
$nse->scp($small_file,":");
verify($small_file,$small_file); # Yes, this is correct.

##############################################
# If no ':' is found, assume local to remote.
##############################################
print "\n5) Testing assumed local to remote\n";
print "=" x 40;
print "\n";

# local to remote (only)
print "\nAttempting to copy $small_file to remote host\n";
$nse->scp($small_file,$rsmall_file);
verify($small_file,$rsmall_file); # Yes, this is correct.

################
# Test globbing
################
print "\n6) Testing glob copy\n";
print "=" x 40;
print "\n";

$nse->scp("small*.test",":");
verify("small_file.test","small_file.test");
verify("small_file2.test","small_file2.test");
verify("small_file3.test","small_file3.test");

# Move the .test files to .orig
move("small_file.test","small_file.orig");
move("small_file2.test","small_file2.orig");
move("small_file3.test","small_file3.orig");

# The text displayed here will be slightly erroneous, but the test is correct
$nse->scp(":small*.test",".");
verify("small_file.orig","small_file.test");
verify("small_file2.orig","small_file2.test");
verify("small_file3.orig","small_file3.test");

print "\nALL TESTS FINISHED\n";
print "=" x 40;
print "\n\n";

if($errors){
   print "TOTAL NUMBER OF FAILD TESTS: $errors\n";
}
else{
   print "ALL TESTS SUCCESSFUL!\n\n";
   print "Do you want to delete the test files (recommended) ? (y/n): ";
   my $ans = <STDIN>;
   chomp($ans);
   if($ans =~ /[Yy]/){
      unlink("*.test");
      unlink("*.orig");
      unlink("*.remote");
   }
}

##########################################################################
# Verifies checksum for two files.  The third argument is merely used to
# determine whether or not the second filename should be deleted.
##########################################################################
sub verify
{
   my($filename,$rfilename,$delete) = @_;

   my $cmd = "md5sum $rfilename";

   my($out,$err,$exit) = $rsh->cmd($cmd);

   chomp($out);
   my($rdigest) = split(/\s+/,$out);

   open(FH,$filename) or die "Couldn't open [$filename]: $!\n";
   my $ldigest = Digest::MD5->new->addfile(*FH)->hexdigest;
   close FH;

   if($rdigest == $ldigest){
      print "The file [$filename] was copied successfully\n";
   }
   else{
      print "The file [$filename] did NOT copy successfully\n";
      $errors++;
   }

   if($delete){ $rsh->cmd("rm -f $rfilename") }
}