19

I've connected over ssh2 using ssh2_connect to a server, but I don't see any method in the manpages for how I should end the connection.

I'm not exactly a fan of waiting for a script to end before I disconnect.

Can I use fclose? That doesn't sound right...

Incognito
  • 20,537
  • 15
  • 80
  • 120
  • I don't know the answer, but from what I got reading the docs `fclose()` should be used in combination with `ssh2_exec()` and `ssh2_shell()` calls, but I don't think it applies to `ssh2_connect()`. – Alix Axel Apr 28 '11 at 14:47

3 Answers3

25

Just unset($connection) your connection variable or ssh2_exec($connection, 'exit'); might do it.

You could probably do the following in order to be even more convincing!

ssh2_exec($connection, 'exit');
unset($connection);
Treffynnon
  • 21,365
  • 6
  • 65
  • 98
  • +1, The `unset()` could be done only if `ssh2_exec()` is not a resource (or returns `false`). – Alix Axel Apr 28 '11 at 14:45
  • 2
    Alternatively: `ssh2_exec($connection, 'exit') or unset($connection);` should do it. – Alix Axel Apr 28 '11 at 14:48
  • 1
    `ssh2_exec( $conn, 'exit' )` and `ssh2_exec( $conn, 'logout' )` do _not_ kill the session. All they do is exit from the temporary shell used to execute the command. The SSH session still exists. – Jesse Nov 19 '13 at 07:19
1

If you were using phpseclib, a pure PHP SSH implementation, you could do $ssh->disconnect(). phpseclib's destructor calls it but you could call it manually as well.

-4

ssh2_exec('logout') should kill your session…

Treffynnon
  • 21,365
  • 6
  • 65
  • 98
Flask
  • 4,966
  • 1
  • 20
  • 39
  • In the event where command execution is allowed. I'm looking to kill the TCPIP, not use the protocol, because I can end up with `Warning: ssh2_exec() [function.ssh2-exec]: Unable to request command execution on remote host` – Incognito Apr 28 '11 at 14:33
  • `system('kill -9 ' . getmypid())`, hideous as it is, works in this instance. – Aaron Miller Mar 26 '13 at 02:45