Skip to content

Commit c4e4988

Browse files
committed
enable logging ssh messages
1 parent 946ad5b commit c4e4988

File tree

3 files changed

+22
-11
lines changed

3 files changed

+22
-11
lines changed

README.md

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ Access a service on a remote host, via an SSH Tunnel! For example, people have b
66
- [Connect to a mysql database via SSH through PHP](http://stackoverflow.com/questions/18069658/connect-to-a-mysql-database-via-ssh-through-php)
77
- [Connect to remote MySQL database with PHP using SSH](http://stackoverflow.com/questions/4927056/connect-to-remote-mysql-database-with-php-using-ssh)
88
- [Laravel MySql DB Connection with SSH](http://stackoverflow.com/questions/25495364/laravel-mysql-db-connection-with-ssh)
9-
9+
1010
We had a similar challenge, specifically accessing a MySQL database over an SSH Tunnel and all of the Questions and Answers were helpful in finding a solution. However, we wanted something that would just plug and play with our Laravel applications and Lumen Services.
1111

1212
So we wrote this package. We hope you enjoy it!
1313

1414
## Requirements
15-
This package has been tested against Laravel/Lumen versions 5.2. 5.3, and 5.4.
15+
This package has been tested against Laravel/Lumen versions 5.2. 5.3, and 5.4.
1616

1717
We do not support version <=5.1.
1818

@@ -37,6 +37,10 @@ TUNNELER_SSH_PATH=/usr/bin/ssh
3737
; Path to the nohup executable
3838
TUNNELER_NOHUP_PATH=/usr/bin/nohup
3939

40+
; Log messages for troubleshooting
41+
SSH_VERBOSITY=
42+
NOHUP_LOG=/dev/null
43+
4044
; The identity file you want to use for ssh auth
4145
TUNNELER_IDENTITY_FILE=/home/user/.ssh/id_rsa
4246

@@ -62,7 +66,7 @@ TUNNELER_ON_BOOT=false
6266
```
6367

6468
## Quickstart
65-
The simplest way to use the Tunneler is to set `TUNNELER_ON_BOOT=true` in your `.env` file. This will ensure the tunnel is in place everytime the framework bootstraps.
69+
The simplest way to use the Tunneler is to set `TUNNELER_ON_BOOT=true` in your `.env` file. This will ensure the tunnel is in place everytime the framework bootstraps.
6670

6771
However, there is minimal performance impact because the tunnel will get reused. You only have to bear the connection costs when the tunnel has been disconnected for some reason.
6872

@@ -90,7 +94,7 @@ And there you have it. Go set up your Eloquent models now.
9094
php artisan tunneler:activate
9195
```
9296

93-
This artisan command will either verify the connection is up, or will create the connection. This probably isn't of great benefit for running manually, apart for testing your configuration.
97+
This artisan command will either verify the connection is up, or will create the connection. This probably isn't of great benefit for running manually, apart for testing your configuration.
9498

9599
However, if you would like to ensure that the tunnel is available all the time, and not do the work on bootstrap, you can use the [Laravel Scheduler](https://laravel.com/docs/5.3/scheduling) to schedule the artisan command to run at whatever interval you think is best to maintain your connection. In your `App\Console\Kernel` for example:
96100

@@ -109,11 +113,11 @@ Perhaps your application rarely needs to do this, but when it does, you'd like t
109113
```php
110114
$app->get('/mysql_tunnel', function () use ($app) {
111115
dispatch(new STS\Tunneler\Jobs\CreateTunnel());
112-
116+
113117
$users = DB::connection('mysql_tunnel')
114118
->table('users')
115119
->get();
116-
120+
117121
dd($users);
118122
});
119123

config/tunneler.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,7 @@
1616
'port' => env('TUNNELER_PORT'),
1717
'wait' => env('TUNNELER_CONN_WAIT', '500000'),
1818

19-
'on_boot' => filter_var(env('TUNNELER_ON_BOOT', false), FILTER_VALIDATE_BOOLEAN)
20-
];
19+
'on_boot' => filter_var(env('TUNNELER_ON_BOOT', false), FILTER_VALIDATE_BOOLEAN),
20+
'ssh_verbosity' => env('SSH_VERBOSITY',''),
21+
'nohup_log' => env('NOHUP_LOG', '/dev/null'),
22+
];

src/Jobs/CreateTunnel.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,9 @@ public function __construct()
2929
config('tunneler.local_port')
3030
);
3131

32-
$this->sshCommand = sprintf('%s -N -i %s -L %d:%s:%d -p %d %s@%s',
32+
$this->sshCommand = sprintf('%s %s -N -i %s -L %d:%s:%d -p %d %s@%s',
3333
config('tunneler.ssh_path'),
34+
config('tunneler.ssh_verbosity'),
3435
config('tunneler.identity_file'),
3536
config('tunneler.local_port'),
3637
config('tunneler.bind_address'),
@@ -64,7 +65,11 @@ public function handle(): int
6465
*/
6566
protected function createTunnel()
6667
{
67-
$this->runCommand(sprintf('%s %s > /dev/null &', config('tunneler.nohup_path'), $this->sshCommand));
68+
$this->runCommand(sprintf('%s %s >> %s 2>&1 &',
69+
config('tunneler.nohup_path'),
70+
$this->sshCommand,
71+
config('tunneler.nohup_log')
72+
));
6873
// Ensure we wait long enough for it to actually connect.
6974
usleep(config('tunneler.wait'));
7075
}
@@ -91,4 +96,4 @@ protected function runCommand($command)
9196
}
9297

9398

94-
}
99+
}

0 commit comments

Comments
 (0)