Giter VIP home page Giter VIP logo

shout's Introduction

Shout

Build Status

SSH made easy in Swift

import Shout

let ssh = try SSH(host: "example.com")
try ssh.authenticate(username: "user", privateKey: "~/.ssh/id_rsa")
try ssh.execute("ls -a")
try ssh.execute("pwd")
...

Installation

> ice add jakeheis/Shout

Swift Package Manager

Add Shout as a dependency to your Package.swift:

dependencies: [
    .package(url: "https://github.com/jakeheis/Shout", from: "0.5.5")
]

Swift 5.2 note

Due to a bug in Swift 5.2, in order to build a project that depends on Shout you must explicitly tell SPM where to find the pkgconfig for libssh2. If you installed libssh2 using Homebrew, the instruction looks something like:

export PKG_CONFIG_PATH="/usr/local/opt/[email protected]/lib/pkgconfig"
swift build

Generating an Xcode project is done the same way:

export PKG_CONFIG_PATH="/usr/local/opt/[email protected]/lib/pkgconfig"
swift package generate-xcodeproj

See issue #34 for more details.

Usage

Creating a session

You create a session by passing a host and optionally a port (default 22):

let ssh = try SSH(host: "example.com")
// or
let ssh = try SSH(host: "example.com", port: 22)

Authenticating

You can authenticate with a private key, a password, or an agent.

Private key

To authenticate with a private key, you must pass the username and the path to the private key. You can also pass the path to the public key (defaults to the private key path + ".pub") and the passphrase encrypting the key (defaults to nil for no passphrase)

session.authenticate(username: "user", privateKey: "~/.ssh/id_rsa")
// or
session.authenticate(username: "user", privateKey: "~/.ssh/id_rsa", publicKey: "~/.ssh/id_rsa.pub", passphrase: "passphrase")

Password

Simply pass the username and password:

session.authenticate(username: "user", password: "password")

Agent

If you've already added the necessary private key to ssh-agent, you can authenticate using the agent:

session.authenticateByAgent(username: "user")

Executing commands

You can remotely execute a command one of two ways. session.execute will print the output of the command to stdout and return the status of the command, while session.capture will not print anything to stdout and will return both the status and the output of the command as a string.

let status = try session.execute("ls -a")
let (status, output) = try session.capture("pwd")

Send files

You can send a local file to a remote path, similar to the scp command line program, with sendFile.

let status = try session.sendFile(localURL: myLocalFile, remotePath: "~/cats.png")

SFTP

You can open an SFTP session with the remote server:

let sftp = try session.openSftp()
try sftp.download(remotePath: "/a/remote/file", localURL: myLocalFile)
try sftp.upload(localURL: myLocalFile, remotePath: "~/cats.png")

Configuration

You can instruct the session to request a pty (pseudo terminal) before executing commands:

session.ptyType = .vanilla

shout's People

Contributors

jakeheis avatar interstateone avatar cyrusingraham avatar brabanod avatar jnewc avatar kyleishie avatar rafiki270 avatar simplykyra avatar bigtows avatar matimax avatar beefon avatar pelopina6 avatar

Stargazers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.