Skip to content

Commit 8e85348

Browse files
authored
add SSH options (#19)
1 parent 12c250f commit 8e85348

File tree

2 files changed

+52
-3
lines changed

2 files changed

+52
-3
lines changed

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,20 @@ docker run -it --rm --link your-postgresql-container:dbhost jfcoz/postgresqltune
167167

168168
When using it remotly, postgresqltuner.pl will use ssh to collect OS informations. You must configure ssh to connect to remote host with private key authentication.
169169

170+
You can add options to ssh via two ways :
171+
172+
- Option :
173+
```
174+
--sshopt=Port=2200 --sshopt=IdentityFile=...
175+
```
176+
177+
- Configure your SSH client via ~/.ssh/config :
178+
```
179+
Host my-database-host
180+
IdentityFile=...
181+
Port=2200
182+
```
183+
170184
### Passwords
171185

172186
For better security use a `~/.pgpass` file containing passwords, so password will not be saved in the shell history nor in the process list. [.pgpass documentation](https://www.postgresql.org/docs/current/static/libpq-pgpass.html)

postgresqltuner.pl

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
exit 1;
4848
}
4949

50-
my $script_version="0.0.11";
50+
my $script_version="0.0.12";
5151
my $script_name="postgresqltuner.pl";
5252
my $min_s=60;
5353
my $hour_s=60*$min_s;
@@ -62,6 +62,7 @@
6262
my $pgpassfile=$ENV{HOME}.'/.pgpass';
6363
my $help=0;
6464
my $work_mem_per_connection_percent=150;
65+
my @Ssh_opts=('BatchMode=yes');
6566
GetOptions (
6667
"host=s" => \$host,
6768
"user=s" => \$username,
@@ -73,13 +74,20 @@
7374
"port=i" => \$port,
7475
"help" => \$help,
7576
"wmp=i" => \$work_mem_per_connection_percent,
77+
"sshopt=s" => \@Ssh_opts
7678
) or usage(1);
7779

7880
print "$script_name version $script_version\n";
7981
if ($help) {
8082
usage(0);
8183
}
8284

85+
# ssh options
86+
my $ssh_opts='';
87+
foreach my $ssh_opt (@Ssh_opts) {
88+
$ssh_opts.=' -o '.$ssh_opt;
89+
}
90+
8391
# host
8492
if (!defined($host)) {
8593
if (defined($ENV{PGHOST})) {
@@ -141,17 +149,44 @@
141149
}
142150
close(PGPASS);
143151
}
152+
153+
# default
154+
if (!defined($password)) {
155+
$password='';
156+
}
157+
}
158+
159+
if (!defined($host)) {
160+
print STDERR "Missing host\n";
161+
print STDERR "\tset \$PGHOST environnement variable\n";
162+
print STDERR "or\tadd --host option\n";
163+
usage(1);
164+
}
165+
166+
if (!defined($username)) {
167+
print STDERR "Missing username\n";
168+
print STDERR "\tset \$PGUSER environnement variable\n";
169+
print STDERR "or\tadd --user option\n";
170+
usage(1);
144171
}
145172

146-
usage(1) if (!defined($host) or !defined($username) or !defined($password));
173+
if (!defined($password)) {
174+
print STDERR "Missing password\n";
175+
print STDERR "\tconfigure ~/.pgpass\n";
176+
print STDERR "or\tset \$PGPASSWORD environnement variable\n";
177+
print STDERR "or\tadd --password option\n";
178+
usage(1);
179+
}
147180

148181
sub usage {
149182
my $return=shift;
150183
print STDERR "usage: $script_name --host [ hostname | /var/run/postgresql ] [--user username] [--password password] [--database database] [--port port] [--wmp 150]\n";
184+
print STDERR "\t[--sshopt=Name=Value]...\n";
151185
print STDERR "If available connection informations can be read from \$PGHOST, \$PGPORT, \$PGDATABASE, \$PGUSER, \$PGPASSWORD\n";
152186
print STDERR "For security reasons, prefer usage of password in ~/.pgpass\n";
153187
print STDERR "\thost:port:database:username:password\n";
154188
print STDERR " --wmp: average number of work_mem buffers per connection in percent (default 150)\n";
189+
print STDERR " --sshopt: pass options to ssh (example --sshopt=Port=2200)\n";
155190
exit $return;
156191
}
157192

@@ -189,7 +224,7 @@ sub usage {
189224
} elsif ($host =~ /^127\.[0-9]+\.[0-9]+\.[0-9]+$/) {
190225
$os_cmd_prefix='';
191226
} elsif ($host =~ /^[a-zA-Z0-9.-]+$/) {
192-
$os_cmd_prefix="ssh -o BatchMode=yes $host ";
227+
$os_cmd_prefix="ssh $ssh_opts $host ";
193228
} else {
194229
die("Invalid host $host");
195230
}

0 commit comments

Comments
 (0)