From f7fc6fd54a5f750de8144b9b05d5ac173470c70a Mon Sep 17 00:00:00 2001 From: Daniel Weipert Date: Tue, 20 Apr 2021 12:45:44 +0200 Subject: Adds Connection wrapper for SSH2 --- src/Connection.php | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 src/Connection.php (limited to 'src/Connection.php') diff --git a/src/Connection.php b/src/Connection.php new file mode 100644 index 0000000..2052cd1 --- /dev/null +++ b/src/Connection.php @@ -0,0 +1,78 @@ +ssh = new SSH2($host); + $key = PublicKeyLoader::load(file_get_contents($key)); + if (! $this->ssh->login($user, $key)) { + throw new \Exception('SSH Login failed'); + } + + $this->sftp = new SFTP($host); + if (! $this->sftp->login($user, $key)) { + throw new \Exception('SFTP Login failed'); + } + } + + /** + * Calls SSH2 methods statically + * + * @param string $name + * @param array $arguments + * + * @return mixed + */ + public static function __callStatic(string $name, array $arguments): mixed + { + $self = self::getInstance(); + + if (! method_exists($self->ssh, $name)) { + return $self->sftp->$name(...$arguments); + } + + return $self->ssh->$name(...$arguments); + } + + /** + * @see SFTP::put + */ + public static function put($remote_file, $data, $mode = SFTP::SOURCE_STRING, $start = -1, $local_start = -1, $progressCallback = null): bool + { + $tmp = bin2hex(random_bytes(10)); # work around sftp sudo put restrictions + + return + self::getInstance()->sftp->put("/tmp/$tmp", $data, $mode, $start, $local_start, $progressCallback) && + self::getInstance()->ssh->exec("sudo mv /tmp/$tmp $remote_file"); + } +} -- cgit v1.2.3