SSH connectivity using Swift

10,427

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.

Share:
10,427

Related videos on Youtube

perhapsmaybeharry
Author by

perhapsmaybeharry

Hello there, I'm Harry. I'm interested in software development of the coding kind (particularly for Unix/Linux systems), and I (try to) write code/scripts in C++, Swift3, Bash and occasionally Java in my spare time. Presently, I'm trying to find time to learn the languages I know in greater detail. I'm active on Ask Different, Unix & Linux, Lifehacks and occasionally Stack Overflow where I try to render my insight on their relevant topics. I love tinkering around with code, my computer (so far it's been through twelve full disk wipes and so many OS resets I've lost count) and photography. Social recluse much I am. If you need to contact me for various reasons, feel free to approach me in chat. Thanks for stopping by! Here's my FlairTM: (If anyone wonders, my avatar is from /System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/ConnectToIcon.icns)

Updated on October 12, 2022

Comments

  • perhapsmaybeharry
    perhapsmaybeharry over 1 year

    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.

  • perhapsmaybeharry
    perhapsmaybeharry almost 9 years
    Thank you for your response. Trying it with your method I still get the nasty 0x600000... error. I'm not sure where that comes from, but according to my logs the program never got past the command. Any idea why this is so?
  • The Beanstalk
    The Beanstalk almost 9 years
    I just took out a line that might have been causing problems (forgot to delete it earlier). In which line does the error occur?
  • perhapsmaybeharry
    perhapsmaybeharry almost 9 years
    The error occurs at sshConnect.launch() or task.launch() in your example. It's telling me that there was an "unrecognised selector sent to instance 0x6000...".
  • The Beanstalk
    The Beanstalk almost 9 years
    It could be something with your arguments array, what's that look like?
  • Mark Gerrior
    Mark Gerrior almost 7 years
    After the great renaming: NSTask in now Process in Swift.
  • CousinCocaine
    CousinCocaine about 3 years
    After the great great renaming: NSTask that became Process is now CommandLine. So use: let task = CommandLine() git apple-swift
  • Radu Ursache
    Radu Ursache almost 2 years
    @CousinCocaine actually by using let task = CommandLine() you'll get 'CommandLine' cannot be constructed because it has no accessible initializers. So keep using let task = Process() which works in Swift 5.5