-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathcreate_user.pl
More file actions
executable file
·138 lines (101 loc) · 2.89 KB
/
create_user.pl
File metadata and controls
executable file
·138 lines (101 loc) · 2.89 KB
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
#!/usr/bin/env perl
=head1 NAME
create_user.pl
=head1 SYNOPSIS
create_user.pl [--username root] --password sekret --host 10.10.10.10 [--newusername|--nu] foobar [--newuserpassword|--np] evenmoresekret [--addtogroup] [--help|-h] [--man]
=head1 DESCRIPTION
Creates a user in ESX that can SSH into the host. If the user exists, then change the password to that given and make sure the user can log in via SSH.
=head1 OPTIONS
=over
=item --username root
User to connect to ESX as, defaults to root.
=item --password
Password for --username
=item --host
ESX host IP/hostname.
=item --newusername | --nu
ESX new user username.
=item --newusepassword | --np
Password for --newusername.
=item --addtogroup
If the user already exists, force the addition of the user to the root group.
This appears to be required to allow users to ssh into ESX 4.0
(/etc/security/access.conf). The argument is optional because if the user is
already a member of a group then it will cause an error message in ESX and it
doesn't appear to be possible to find out beforehand what the group membership
of a user is.
=item --help | -h
Command line help.
=item --man
Show the man page.
=back
=head1 AUTHOR
Jonathan Barber - <jonathan.barber@gmail.com>
=cut
use strict;
use warnings;
use Getopt::Long;
use Pod::Usage;
use VMware::VIRuntime;
my ($user, $passwd, $host, $newuser, $newpasswd, $addgroup, $help, $man);
GetOptions(
"username=s" => \$user,
"password=s" => \$passwd,
"host=s" => \$host,
"newusername|nu=s" => \$newuser,
"newuserpassword|np=s" => \$newpasswd,
"addtogroup" => \$addgroup,
"help|h" => \$help,
"man" => \$man,
) or pod2usage(2);
$help && pod2usage(-verbose => 0);
$man && pod2usage(-verbose => 2);
$user ||= "root";
$passwd || die "Missing --password argument\n";
$host || die "Missing ESX --host argument\n";
$newuser ||= $ENV{USER};
$newpasswd || die "Missing --newuserpassword argument\n";
my $login = Vim::login(
service_url => "https://$host/sdk/vimService",
user_name => $user,
password => $passwd,
) || die $!;
END { $login && $login->logout; }
my $user_info = HostPosixAccountSpec->new(
id => $newuser,
password => $newpasswd,
shellAccess => 1,
);
my $sc = Vim::get_service_content;
my $ud = Vim::get_view(mo_ref => $sc->userDirectory());
my $am = Vim::get_view(mo_ref => $sc->accountManager());
my ($search) = @{ $ud->RetrieveUserGroups(
searchStr => $user_info->id,
exactMatch => 1,
findUsers => 1,
findGroups => 0,
) };
if ($search) {
warn "User exists, updating.\n";
$am->UpdateUser(user => $user_info);
if ($addgroup) {
my $group = "root";
eval {
$am->AssignUserToGroup(
user => $user_info->id,
group => $group,
);
};
if ($@) {
warn "User was already a member of the group '$group'\n";
}
}
}
else {
warn "User missing, creating.\n";
$am->CreateUser(user => $user_info);
$am->AssignUserToGroup(
user => $user_info->id,
group => "root"
);
}