Skip to content

Commit

Permalink
Initial Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
faraweilyas committed Jul 9, 2019
0 parents commit 46f18ac
Show file tree
Hide file tree
Showing 11 changed files with 578 additions and 0 deletions.
14 changes: 14 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# top-most EditorConfig file
root = true

# unix-style newlines with a newline ending every file
[*]
end_of_line = lf
insert_final_newline = true

# set default charset
charset = utf-8

# tab indentation
indent_style = tab
indent_size = 4
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
composer.lock
vendor/
index.php
76 changes: 76 additions & 0 deletions CODE_OF_CONDUCT.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# Contributor Covenant Code of Conduct

## Our Pledge

In the interest of fostering an open and welcoming environment, we as
contributors and maintainers pledge to making participation in our project and
our community a harassment-free experience for everyone, regardless of age, body
size, disability, ethnicity, sex characteristics, gender identity and expression,
level of experience, education, socio-economic status, nationality, personal
appearance, race, religion, or sexual identity and orientation.

## Our Standards

Examples of behavior that contributes to creating a positive environment
include:

* Using welcoming and inclusive language
* Being respectful of differing viewpoints and experiences
* Gracefully accepting constructive criticism
* Focusing on what is best for the community
* Showing empathy towards other community members

Examples of unacceptable behavior by participants include:

* The use of sexualized language or imagery and unwelcome sexual attention or
advances
* Trolling, insulting/derogatory comments, and personal or political attacks
* Public or private harassment
* Publishing others' private information, such as a physical or electronic
address, without explicit permission
* Other conduct which could reasonably be considered inappropriate in a
professional setting

## Our Responsibilities

Project maintainers are responsible for clarifying the standards of acceptable
behavior and are expected to take appropriate and fair corrective action in
response to any instances of unacceptable behavior.

Project maintainers have the right and responsibility to remove, edit, or
reject comments, commits, code, wiki edits, issues, and other contributions
that are not aligned to this Code of Conduct, or to ban temporarily or
permanently any contributor for other behaviors that they deem inappropriate,
threatening, offensive, or harmful.

## Scope

This Code of Conduct applies both within project spaces and in public spaces
when an individual is representing the project or its community. Examples of
representing a project or community include using an official project e-mail
address, posting via an official social media account, or acting as an appointed
representative at an online or offline event. Representation of a project may be
further defined and clarified by project maintainers.

## Enforcement

Instances of abusive, harassing, or otherwise unacceptable behavior may be
reported by contacting the project team at [email protected]. All
complaints will be reviewed and investigated and will result in a response that
is deemed necessary and appropriate to the circumstances. The project team is
obligated to maintain confidentiality with regard to the reporter of an incident.
Further details of specific enforcement policies may be posted separately.

Project maintainers who do not follow or enforce the Code of Conduct in good
faith may face temporary or permanent repercussions as determined by other
members of the project's leadership.

## Attribution

This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
available at https://www.contributor-covenant.org/version/1/4/code-of-conduct.html

[homepage]: https://www.contributor-covenant.org

For answers to common questions about this code of conduct, see
https://www.contributor-covenant.org/faq
7 changes: 7 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Contribution Guideline

Hello 👋!

Just send a pull request, I'd be happy to merge.

Happy contributing 🎉!
9 changes: 9 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
The MIT License (MIT)

Copyright (c) 2017 Farawe iLyas <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
119 changes: 119 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
# sshbunny
PHP library that provides an object-oriented wrapper to connect to SSH and run shell commands with the php ssh2 extension.

# Requirements
------------

- PHP version 5.3+
- [SSH2 extension](http://www.php.net/manual/en/book.ssh2.php).
- [composer](http://getcomposer.org).

# Install with composer
------------

The best way to add the library to your project is using [composer](http://getcomposer.org).
```bash
composer require faraweilyas/sshbunny
```
or

# Clone this repo

```bash
git clone https://github.com/faraweilyas/sshbunny.git
```

# Configuration
-------------

`SSHBunny` constructor takes four parameters and they all have default values `$method='local'`, `$authType=NULL`, `$host=NULL`, `$port=22`, `$username=NULL`

- `$method` can be set to `local` or `remote`, `local` will execute commands on your own shell without internet connection while `remote` executes commands on the remote server that you connect to based on your configuration.
- `$authType` can be set to `KEY`, `PASSWORD` or `KEY_PASSWORD`, `KEY` and `KEY_PASSWORD` uses [ssh2_auth_pubkey_file](http://php.net/manual/en/function.ssh2-auth-pubkey-file.php) the difference is when you set `$authType='KEY_PASSWORD'` ssh2_auth_pubkey_file takes the last parameter of password which will now be required and `PASSWORD` uses [ssh2_auth_password](http://php.net/manual/en/function.ssh2-auth-password.php).
- `$port` should be set to your server port if your are connecting to a remote server.
- `$username` should be set to your server username.

if your are setting connection method to `$method='remote'` and `$authType = KEY || KEY_PASSWORD` that means you will need to set your public & private key file which you can do with the setters `SSHBunny` has `$sshBunny->setKeys('public_key.pub', 'private_key')` before initialization.

# Basic usage
-------------
This is just going to run locally since connection method is set to `local`
```php
<?php

use SSHBunny\SSHBunny;

require_once 'vendor/autoload.php';

// ->getData() will return output of command executed while ->getData(TRUE) will dispay the output
$sshBunny = (new SSHBunny('local'))
->initialize()
->exec("echo 'Hello World'")
->getData(TRUE);
```

This is going connect to a remote server since connection method is set to `remote` and authentication type is set to `KEY`
```php
<?php

use SSHBunny\SSHBunny;

require_once 'vendor/autoload.php';

defined('TEST_HOST') ? NULL : define('TEST_HOST', "138.222.15.1");
defined('PORT') ? NULL : define('PORT', "22");
defined('USERNAME') ? NULL : define('USERNAME', "ubuntu");
defined('PUBLIC_KEY') ? NULL : define('PUBLIC_KEY', 'id_ssl.pub');
defined('PRIVATE_KEY') ? NULL : define('PRIVATE_KEY', 'id_ssl');

$sshBunny = (new SSHBunny('remote', 'KEY', HOST, PORT, USERNAME))
->setKeys(PUBLIC_KEY, PRIVATE_KEY)
->initialize()
->exec("echo 'Hello World'")
->getData(TRUE);
```

Command execution can take multiple commands or you can chain on the `exec` method with another `exec` method
```php
$sshBunny = (new SSHBunny('remote', 'KEY', HOST, PORT, USERNAME))
->setKeys(PUBLIC_KEY, PRIVATE_KEY)
->initialize()
// Multiple commands
->exec("echo 'Hello World'", "cd /var/www/html")
// Method chaining
->exec("ls -la")
->getData(TRUE);
```

## Available methods

- Executed command output
```php
// Will return the result of executed command output
$sshBunny
->exec("ls -la")
->getData();
// Will display the result of executed command output
$sshBunny
->exec("ls -la")
->getData(TRUE);
```

- Clear stored executed command output
```php
// Will clear the first executed command output and return the next executed command output
$sshBunny
->exec("ls -la")
->clearData()
->exec("whoami")
->getData(TRUE);
```

- Disconnect server connection
```php
// Will run the commands provided and display the result then disconnect from the server
$sshBunny
->exec("ls -la", "whoami")
->getData(TRUE)
->disconnect();
```
37 changes: 37 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"name": "faraweilyas/mangoapi",
"type": "library",
"description": "PHP library that provides an object-oriented wrapper to manage API requests.",
"keywords": ["mango", "api", "mangoapi", "mangoapi requests"],
"homepage": "https://github.com/faraweilyas/mangoapi",
"license": "MIT",
"authors": [
{
"name": "Farawe iLyas",
"email": "[email protected]"
}
],
"support": {
"email": "[email protected]",
"issues": "https://github.com/faraweilyas/mangoapi/issues"
},
"autoload": {
"psr-4": {
"MangoAPI\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"Tests\\": "tests"
}
},
"require-dev": {
"phpunit/phpunit": "^6.4"
},
"scripts": {
"tests": "phpunit"
},
"config": {
"optimize-autoloader": true
}
}
22 changes: 22 additions & 0 deletions mangoapi
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/usr/bin/env php
<?php

use MangoAPI\Request;
use MangoAPI\Response;

require_once 'vendor/autoload.php';

$request = new Request();
$request->addHeader("Access-Control-Allow-Origin", "*")
->addHeader("Content-Type", "application/json; charset=UTF-8")
->setAllowMethods(["GET", "POST", "PUT", "HEAD", "DELETE", "PATCH", "OPTIONS"])
->addHeader("Access-Control-Max-Age", "3600")
->addHeader("Access-Control-Allow-Credentials", "true")
->addHeader("Access-Control-Allow-Headers", "Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With")
->setHeaders();

$response = new Response;
$data = $request->getAllowMethods();
$response->isOk("Successfully processed", $data)
->send()
->kill();
16 changes: 16 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false">
<testsuites>
<testsuite name="unit">
<directory suffix="Test.php">./tests/Unit</directory>
</testsuite>
</testsuites>
</phpunit>
Loading

0 comments on commit 46f18ac

Please sign in to comment.