Last active
December 5, 2015 11:59
-
-
Save DenBeke/d3ce7c2d5f0bf50da46a to your computer and use it in GitHub Desktop.
Executing external command with Swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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