Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SSH connectivity using Swift

Tags:

ssh

swift

Recently, I've been trying to make a (very) simple program with Swift that lets you connect to a server through SSH and execute some files. Unfortunately, I could not figure out how to start an SSH session completely within the Swift app. Here is some code that I have written:

var sshConnectCommand = ["-c", "spawn ssh "+sshUsername+"@"+sshHost+"; expect assword:; send "+sshPassword+"\r"]
func sshIn() {
    //starting ssh session
    let sshConnect = NSTask()
    sshConnect.arguments = [testCmd]

    //rerouting output through a pipe
    sshConnect.standardOutput = logAppend

    //launch!
    sshConnect.launch();
}

As you can see, I have used NSTask to try and run the 'expect' command to enter the password and everything. I would like to try and avoid using SSH-keygen as much as possible as this is intended to be used a server that the user does not have any access to.

So, to sum up: How would you connect to SSH without SSH-keygen while remaining completely within the application code?

edit: I should also add, when trying to compile, I get this error: [Swift._SwiftDeferredNSArray fileSystemRepresentation]: unrecognized selector sent to instance 0x600000032500. I'm not sure what this means.

like image 673
perhapsmaybeharry Avatar asked Jul 14 '15 13:07

perhapsmaybeharry


1 Answers

I've been using something similar to this to ssh into my Raspberry Pi:

func sshIn() {
    let task = CommandLine()
    task.launchPath = "/usr/bin/ssh"
    task.arguments = ["USERNAME@IPADDRESS", "-t", "sudo systemctl stop mediacenter; /opt/vc/bin/tvservice -o"]
    task.launch()
}

-t closes the connection when the commands are finished running, and you can pass in all your commands like so command1; command 2 like where I've got sudo systemctl stop mediacenter; /opt/vc/bin/tvservice -o As for the keygen thing, I don't think you have much of a choice. You can look into locking it down a bit though. Here's a good place to start looking.

like image 113
The Beanstalk Avatar answered Nov 08 '22 01:11

The Beanstalk