Skip to content

Instantly share code, notes, and snippets.

@DenBeke
Last active December 5, 2015 11:59
Show Gist options
  • Save DenBeke/d3ce7c2d5f0bf50da46a to your computer and use it in GitHub Desktop.
Save DenBeke/d3ce7c2d5f0bf50da46a to your computer and use it in GitHub Desktop.
Executing external command with Swift
import Foundation
func executeCommand(command: String, args: [String]) -> String {
let task = NSTask()
task.launchPath = command
task.arguments = args
let pipe = NSPipe()
task.standardOutput = pipe
task.launch()
let data = pipe.fileHandleForReading.readDataToEndOfFile()
let output: String = String(data: data, encoding: NSUTF8StringEncoding)!
return output
}
let commandOutput = executeCommand("/usr/sbin/arp", args: ["-a"])
print("Command output: \(commandOutput)")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment