-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathnx_template2session_file.pl
executable file
·56 lines (51 loc) · 1.58 KB
/
nx_template2session_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
#!/usr/bin/perl
#####
# Usage nx_template2session_file.pl <template.nxs> <remote_ip> <remote_username> <remote_password> > <session_file.nxs>
#####
use warnings;
use strict;
use XML::Twig;
use Getopt::Long;
use File::Basename; # fordirname()
my $template_file;
my $host_ip;
my $username;
my $password;
my $output_file;
GetOptions (
'template=s' => \$template_file,
'host=s' => \$host_ip,
'username=s' => \$username,
'password=s' => \$password,
'output=s' => \$output_file
) or die("Error in command line arguments\n");
die "output filename ($output_file) cannot contain illegal characters: =,_\n" if $output_file =~ /[=,_]/;
my $dir_containing_this_script = dirname(__FILE__);
my $scrambled_password = `perl $dir_containing_this_script/nx_password_scrambler.pl $password`;
my $twig = XML::Twig->new(
pretty_print => 'indented',
keep_encoding => 1,
);
$twig->parsefile($template_file);
my $root = $twig->root;
my @groups = $root->children;
foreach my $group (@groups) {
if ($group->{'att'}->{'name'} eq 'General') {
my @options = $group->children;
foreach my $option (@options) {
if ($option->{'att'}->{'key'} eq 'Server host') {
$option->set_att('value' => $host_ip);
}
}
} elsif($group->{'att'}->{'name'} eq 'Login') {
my @options = $group->children;
foreach my $option (@options) {
if ($option->{'att'}->{'key'} eq 'Auth') {
$option->set_att('value' => $scrambled_password);
} elsif ($option->{'att'}->{'key'} eq 'User') {
$option->set_att('value' => $username);
}
}
}
}
$twig->print_to_file($output_file);