Skip to content

Instantly share code, notes, and snippets.

@dacr
Last active May 25, 2024 10:18

Revisions

  1. dacr revised this gist May 25, 2024. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion ssh-client-server-sync.sc
    Original file line number Diff line number Diff line change
    @@ -9,7 +9,7 @@
    // run-with : scala-cli $file

    // ---------------------
    //> using scala "3.4.2"
    //> using scala "3.4.2"
    //> using dep "org.apache.sshd:sshd-core:2.8.0"
    //> using dep "org.apache.sshd:sshd-sftp:2.8.0"
    //> using dep "org.apache.sshd:sshd-scp:2.8.0"
  2. dacr revised this gist May 25, 2024. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion ssh-client-server-sync.sc
    Original file line number Diff line number Diff line change
    @@ -9,7 +9,7 @@
    // run-with : scala-cli $file

    // ---------------------
    //> using scala "3.1.1"
    //> using scala "3.4.2"
    //> using dep "org.apache.sshd:sshd-core:2.8.0"
    //> using dep "org.apache.sshd:sshd-sftp:2.8.0"
    //> using dep "org.apache.sshd:sshd-scp:2.8.0"
  3. dacr revised this gist Apr 2, 2023. 1 changed file with 5 additions and 5 deletions.
    10 changes: 5 additions & 5 deletions ssh-client-server-sync.sc
    Original file line number Diff line number Diff line change
    @@ -2,18 +2,18 @@
    // keywords : scala, ssh, client, server, apache-sshd, mina, @testable
    // publish : gist
    // authors : David Crosson
    // license : Apache2 BUT Machine Learning models training is not allowed by the author
    // license : Apache NON-AI License Version 2.0 (https://raw.githubusercontent.com/non-ai-licenses/non-ai-licenses/main/NON-AI-APACHE2)
    // id : a267b427-a378-445c-b91e-e26ddf889c72
    // created-on : 2018-07-02T19:35:49Z
    // managed-by : https://github.com/dacr/code-examples-manager
    // run-with : scala-cli $file

    // ---------------------
    //> using scala "3.1.1"
    //> using lib "org.apache.sshd:sshd-core:2.8.0"
    //> using lib "org.apache.sshd:sshd-sftp:2.8.0"
    //> using lib "org.apache.sshd:sshd-scp:2.8.0"
    //> using lib "org.slf4j:slf4j-simple:1.7.32"
    //> using dep "org.apache.sshd:sshd-core:2.8.0"
    //> using dep "org.apache.sshd:sshd-sftp:2.8.0"
    //> using dep "org.apache.sshd:sshd-scp:2.8.0"
    //> using dep "org.slf4j:slf4j-simple:1.7.32"
    // ---------------------

    import java.util.concurrent.TimeUnit
  4. dacr revised this gist Dec 11, 2022. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion ssh-client-server-sync.sc
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,7 @@
    // keywords : scala, ssh, client, server, apache-sshd, mina, @testable
    // publish : gist
    // authors : David Crosson
    // license : Apache2
    // license : Apache2 BUT Machine Learning models training is not allowed by the author
    // id : a267b427-a378-445c-b91e-e26ddf889c72
    // created-on : 2018-07-02T19:35:49Z
    // managed-by : https://github.com/dacr/code-examples-manager
  5. dacr revised this gist Jul 20, 2022. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion ssh-client-server-sync.sc
    Original file line number Diff line number Diff line change
    @@ -6,7 +6,7 @@
    // id : a267b427-a378-445c-b91e-e26ddf889c72
    // created-on : 2018-07-02T19:35:49Z
    // managed-by : https://github.com/dacr/code-examples-manager
    // run-with : scala-cli $scriptFile
    // run-with : scala-cli $file

    // ---------------------
    //> using scala "3.1.1"
  6. dacr revised this gist Jan 28, 2022. 1 changed file with 5 additions and 5 deletions.
    10 changes: 5 additions & 5 deletions ssh-client-server-sync.sc
    Original file line number Diff line number Diff line change
    @@ -9,11 +9,11 @@
    // run-with : scala-cli $scriptFile

    // ---------------------
    // using scala 3.1.1
    // using lib "org.apache.sshd:sshd-core:2.8.0"
    // using lib "org.apache.sshd:sshd-sftp:2.8.0"
    // using lib "org.apache.sshd:sshd-scp:2.8.0"
    // using lib "org.slf4j:slf4j-simple:1.7.32"
    //> using scala "3.1.1"
    //> using lib "org.apache.sshd:sshd-core:2.8.0"
    //> using lib "org.apache.sshd:sshd-sftp:2.8.0"
    //> using lib "org.apache.sshd:sshd-scp:2.8.0"
    //> using lib "org.slf4j:slf4j-simple:1.7.32"
    // ---------------------

    import java.util.concurrent.TimeUnit
  7. dacr revised this gist Jan 22, 2022. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions ssh-client-server-sync.sc
    Original file line number Diff line number Diff line change
    @@ -9,7 +9,7 @@
    // run-with : scala-cli $scriptFile

    // ---------------------
    // using scala 3.1.1-RC2
    // using scala 3.1.1
    // using lib "org.apache.sshd:sshd-core:2.8.0"
    // using lib "org.apache.sshd:sshd-sftp:2.8.0"
    // using lib "org.apache.sshd:sshd-scp:2.8.0"
    @@ -47,7 +47,7 @@ object Main {
    sshd.setHost(sshHost)
    sshd.setPort(sshPort)
    // first command is the "unparsed one"
    sshd.setShellFactory(new ProcessShellFactory("/usr/bin/cat", "/usr/bin/cat"))
    sshd.setShellFactory(new ProcessShellFactory("/bin/cat", "/bin/cat")) // TODO Take care not always installed at this place
    sshd.setKeyPairProvider(new SimpleGeneratorHostKeyProvider())
    sshd.setPasswordAuthenticator(new PasswordAuthenticator {
    override def authenticate(username: String, password: String, serverSession: ServerSession): Boolean = {
  8. dacr revised this gist Jan 4, 2022. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion ssh-client-server-sync.sc
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    // summary : SSH server and client operations both within a single script using apache sshd.
    // keywords : scala, ssh, client, server, apache-sshd, mina
    // keywords : scala, ssh, client, server, apache-sshd, mina, @testable
    // publish : gist
    // authors : David Crosson
    // license : Apache2
  9. dacr revised this gist Jan 4, 2022. 1 changed file with 16 additions and 9 deletions.
    25 changes: 16 additions & 9 deletions ssh-client-server-sync.sc
    Original file line number Diff line number Diff line change
    @@ -1,16 +1,20 @@
    // summary : SSH server and client operations both within a single script using apache sshd.
    // keywords : scala, ssh, client, server, apache-sshd
    // keywords : scala, ssh, client, server, apache-sshd, mina
    // publish : gist
    // authors : David Crosson
    // license : Apache2
    // id : a267b427-a378-445c-b91e-e26ddf889c72
    // created-on : 2018-07-02T19:35:49Z
    // managed-by : https://github.com/dacr/code-examples-manager
    // execution : scala ammonite script (http://ammonite.io/) - run as follow 'amm scriptname.sc'
    // run-with : scala-cli $scriptFile

    import $ivy.`org.apache.sshd:sshd-core:2.0.0`
    import $ivy.`org.apache.sshd:sshd-sftp:2.0.0`
    import $ivy.`org.apache.sshd:sshd-scp:2.0.0`
    // ---------------------
    // using scala 3.1.1-RC2
    // using lib "org.apache.sshd:sshd-core:2.8.0"
    // using lib "org.apache.sshd:sshd-sftp:2.8.0"
    // using lib "org.apache.sshd:sshd-scp:2.8.0"
    // using lib "org.slf4j:slf4j-simple:1.7.32"
    // ---------------------

    import java.util.concurrent.TimeUnit
    import org.apache.sshd.client.SshClient
    @@ -42,7 +46,8 @@ object Main {
    val sshd = SshServer.setUpDefaultServer
    sshd.setHost(sshHost)
    sshd.setPort(sshPort)
    sshd.setShellFactory(new ProcessShellFactory("/bin/cat"))
    // first command is the "unparsed one"
    sshd.setShellFactory(new ProcessShellFactory("/usr/bin/cat", "/usr/bin/cat"))
    sshd.setKeyPairProvider(new SimpleGeneratorHostKeyProvider())
    sshd.setPasswordAuthenticator(new PasswordAuthenticator {
    override def authenticate(username: String, password: String, serverSession: ServerSession): Boolean = {
    @@ -58,9 +63,11 @@ object Main {
    session.auth.verify(5, TimeUnit.SECONDS)
    val channel:ClientChannel = session.createChannel(Channel.CHANNEL_SHELL)


    val baos = new ByteArrayOutputStream
    val w = new OutputStreamWriter(baos)
    w.write("id")
    w.write("Hello world")
    //w.flush()
    w.close()

    channel.setIn(new ByteArrayInputStream(baos.toByteArray))
    @@ -73,8 +80,8 @@ object Main {

    val result: String = out.toString.trim

    println("-->'"+result+"'") // should print 'id' if the current os has an available "/bin/cat" command
    assert(result == "id")
    println(s"-->'$result'") // should print 'id' if the current os has an available "/bin/cat" command
    assert(result == "Hello world")

    sshd.stop()
    }
  10. dacr revised this gist Nov 30, 2021. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions ssh-client-server-sync.sc
    Original file line number Diff line number Diff line change
    @@ -5,6 +5,7 @@
    // license : Apache2
    // id : a267b427-a378-445c-b91e-e26ddf889c72
    // created-on : 2018-07-02T19:35:49Z
    // managed-by : https://github.com/dacr/code-examples-manager
    // execution : scala ammonite script (http://ammonite.io/) - run as follow 'amm scriptname.sc'

    import $ivy.`org.apache.sshd:sshd-core:2.0.0`
  11. dacr revised this gist Nov 29, 2021. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions ssh-client-server-sync.sc
    Original file line number Diff line number Diff line change
    @@ -4,6 +4,7 @@
    // authors : David Crosson
    // license : Apache2
    // id : a267b427-a378-445c-b91e-e26ddf889c72
    // created-on : 2018-07-02T19:35:49Z
    // execution : scala ammonite script (http://ammonite.io/) - run as follow 'amm scriptname.sc'

    import $ivy.`org.apache.sshd:sshd-core:2.0.0`
  12. dacr revised this gist May 1, 2021. No changes.
  13. dacr revised this gist Oct 9, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion ssh-client-server-sync.sc
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,7 @@
    // keywords : scala, ssh, client, server, apache-sshd
    // publish : gist
    // authors : David Crosson
    // license : GPL
    // license : Apache2
    // id : a267b427-a378-445c-b91e-e26ddf889c72
    // execution : scala ammonite script (http://ammonite.io/) - run as follow 'amm scriptname.sc'

  14. dacr revised this gist Oct 3, 2020. No changes.
  15. dacr revised this gist Jul 7, 2020. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion ssh-client-server-sync.sc
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,8 @@
    // summary : SSH server and client operations both within a single script using apache sshd.
    // keywords : scala, ssh, client, server, apache-sshd
    // publish : gist, snippet
    // publish : gist
    // authors : David Crosson
    // license : GPL
    // id : a267b427-a378-445c-b91e-e26ddf889c72
    // execution : scala ammonite script (http://ammonite.io/) - run as follow 'amm scriptname.sc'

  16. dacr revised this gist Sep 11, 2019. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions ssh-client-server-sync.sc
    Original file line number Diff line number Diff line change
    @@ -3,6 +3,7 @@
    // publish : gist, snippet
    // authors : David Crosson
    // id : a267b427-a378-445c-b91e-e26ddf889c72
    // execution : scala ammonite script (http://ammonite.io/) - run as follow 'amm scriptname.sc'

    import $ivy.`org.apache.sshd:sshd-core:2.0.0`
    import $ivy.`org.apache.sshd:sshd-sftp:2.0.0`
  17. dacr revised this gist Aug 29, 2019. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions ssh-client-server-sync.sc
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    // summary : SSH server and client operations both within a single script using apache sshd.
    // keywords : ssh, client, server, apache-sshd
    // keywords : scala, ssh, client, server, apache-sshd
    // publish : gist, snippet
    // authors : @crodav
    // authors : David Crosson
    // id : a267b427-a378-445c-b91e-e26ddf889c72

    import $ivy.`org.apache.sshd:sshd-core:2.0.0`
  18. dacr revised this gist Aug 29, 2019. 1 changed file with 0 additions and 79 deletions.
    79 changes: 0 additions & 79 deletions ssh-client-server-sync.scala
    Original file line number Diff line number Diff line change
    @@ -1,79 +0,0 @@
    // summary : SSH server and client operations both within a single script using apache sshd.
    // keywords : ssh, client, server, apache-sshd
    // publish : gist, snippet
    // authors : david
    // id : a267b427-a378-445c-b91e-e26ddf889c72

    import $ivy.`org.apache.sshd:sshd-core:2.0.0`
    import $ivy.`org.apache.sshd:sshd-sftp:2.0.0`
    import $ivy.`org.apache.sshd:sshd-scp:2.0.0`

    import java.util.concurrent.TimeUnit
    import org.apache.sshd.client.SshClient
    import org.apache.sshd.common.channel.Channel
    import org.apache.sshd.server.SshServer
    import org.apache.sshd.server.auth.password.PasswordAuthenticator
    import org.apache.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider
    import org.apache.sshd.server.session.ServerSession
    import java.io.OutputStreamWriter

    import org.apache.sshd.client.channel.{ClientChannel, ClientChannelEvent}
    import java.io.ByteArrayInputStream
    import java.io.ByteArrayOutputStream

    import org.apache.sshd.server.shell.ProcessShellFactory


    // Everything in a object as soon as we have to deal with futures, promises, asynchronous operations
    //
    object Main {
    val sshHost = "localhost"
    val sshPort = 2222
    val testUsername = "test"
    val testPassword = "test"

    def go:Unit = {
    println("Starting operations")

    val sshd = SshServer.setUpDefaultServer
    sshd.setHost(sshHost)
    sshd.setPort(sshPort)
    sshd.setShellFactory(new ProcessShellFactory("/bin/cat"))
    sshd.setKeyPairProvider(new SimpleGeneratorHostKeyProvider())
    sshd.setPasswordAuthenticator(new PasswordAuthenticator {
    override def authenticate(username: String, password: String, serverSession: ServerSession): Boolean = {
    username==testUsername && password == testPassword
    }
    })
    sshd.start()

    val ssh = SshClient.setUpDefaultClient()
    ssh.start()
    val session = ssh.connect(testUsername, sshHost, sshPort).verify(5, TimeUnit.SECONDS).getSession
    session.addPasswordIdentity(testPassword)
    session.auth.verify(5, TimeUnit.SECONDS)
    val channel:ClientChannel = session.createChannel(Channel.CHANNEL_SHELL)

    val baos = new ByteArrayOutputStream
    val w = new OutputStreamWriter(baos)
    w.write("id")
    w.close()

    channel.setIn(new ByteArrayInputStream(baos.toByteArray))
    val out: ByteArrayOutputStream = new ByteArrayOutputStream
    val err: ByteArrayOutputStream = new ByteArrayOutputStream
    channel.setOut(out)
    channel.setErr(err)
    channel.open
    channel.waitFor(java.util.Arrays.asList(ClientChannelEvent.CLOSED), 0)

    val result: String = out.toString.trim

    println("-->'"+result+"'") // should print 'id' if the current os has an available "/bin/cat" command
    assert(result == "id")

    sshd.stop()
    }
    }

    Main.go
  19. dacr revised this gist Aug 29, 2019. 1 changed file with 79 additions and 0 deletions.
    79 changes: 79 additions & 0 deletions ssh-client-server-sync.sc
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,79 @@
    // summary : SSH server and client operations both within a single script using apache sshd.
    // keywords : ssh, client, server, apache-sshd
    // publish : gist, snippet
    // authors : @crodav
    // id : a267b427-a378-445c-b91e-e26ddf889c72

    import $ivy.`org.apache.sshd:sshd-core:2.0.0`
    import $ivy.`org.apache.sshd:sshd-sftp:2.0.0`
    import $ivy.`org.apache.sshd:sshd-scp:2.0.0`

    import java.util.concurrent.TimeUnit
    import org.apache.sshd.client.SshClient
    import org.apache.sshd.common.channel.Channel
    import org.apache.sshd.server.SshServer
    import org.apache.sshd.server.auth.password.PasswordAuthenticator
    import org.apache.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider
    import org.apache.sshd.server.session.ServerSession
    import java.io.OutputStreamWriter

    import org.apache.sshd.client.channel.{ClientChannel, ClientChannelEvent}
    import java.io.ByteArrayInputStream
    import java.io.ByteArrayOutputStream

    import org.apache.sshd.server.shell.ProcessShellFactory


    // Everything in a object as soon as we have to deal with futures, promises, asynchronous operations
    //
    object Main {
    val sshHost = "localhost"
    val sshPort = 2222
    val testUsername = "test"
    val testPassword = "test"

    def go:Unit = {
    println("Starting operations")

    val sshd = SshServer.setUpDefaultServer
    sshd.setHost(sshHost)
    sshd.setPort(sshPort)
    sshd.setShellFactory(new ProcessShellFactory("/bin/cat"))
    sshd.setKeyPairProvider(new SimpleGeneratorHostKeyProvider())
    sshd.setPasswordAuthenticator(new PasswordAuthenticator {
    override def authenticate(username: String, password: String, serverSession: ServerSession): Boolean = {
    username==testUsername && password == testPassword
    }
    })
    sshd.start()

    val ssh = SshClient.setUpDefaultClient()
    ssh.start()
    val session = ssh.connect(testUsername, sshHost, sshPort).verify(5, TimeUnit.SECONDS).getSession
    session.addPasswordIdentity(testPassword)
    session.auth.verify(5, TimeUnit.SECONDS)
    val channel:ClientChannel = session.createChannel(Channel.CHANNEL_SHELL)

    val baos = new ByteArrayOutputStream
    val w = new OutputStreamWriter(baos)
    w.write("id")
    w.close()

    channel.setIn(new ByteArrayInputStream(baos.toByteArray))
    val out: ByteArrayOutputStream = new ByteArrayOutputStream
    val err: ByteArrayOutputStream = new ByteArrayOutputStream
    channel.setOut(out)
    channel.setErr(err)
    channel.open
    channel.waitFor(java.util.Arrays.asList(ClientChannelEvent.CLOSED), 0)

    val result: String = out.toString.trim

    println("-->'"+result+"'") // should print 'id' if the current os has an available "/bin/cat" command
    assert(result == "id")

    sshd.stop()
    }
    }

    Main.go
  20. dacr revised this gist Jun 7, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion ssh-client-server-sync.scala
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    // summary : ssh server and client operations both within a single script.
    // summary : SSH server and client operations both within a single script using apache sshd.
    // keywords : ssh, client, server, apache-sshd
    // publish : gist, snippet
    // authors : david
  21. dacr created this gist Jun 7, 2019.
    79 changes: 79 additions & 0 deletions ssh-client-server-sync.scala
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,79 @@
    // summary : ssh server and client operations both within a single script.
    // keywords : ssh, client, server, apache-sshd
    // publish : gist, snippet
    // authors : david
    // id : a267b427-a378-445c-b91e-e26ddf889c72

    import $ivy.`org.apache.sshd:sshd-core:2.0.0`
    import $ivy.`org.apache.sshd:sshd-sftp:2.0.0`
    import $ivy.`org.apache.sshd:sshd-scp:2.0.0`

    import java.util.concurrent.TimeUnit
    import org.apache.sshd.client.SshClient
    import org.apache.sshd.common.channel.Channel
    import org.apache.sshd.server.SshServer
    import org.apache.sshd.server.auth.password.PasswordAuthenticator
    import org.apache.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider
    import org.apache.sshd.server.session.ServerSession
    import java.io.OutputStreamWriter

    import org.apache.sshd.client.channel.{ClientChannel, ClientChannelEvent}
    import java.io.ByteArrayInputStream
    import java.io.ByteArrayOutputStream

    import org.apache.sshd.server.shell.ProcessShellFactory


    // Everything in a object as soon as we have to deal with futures, promises, asynchronous operations
    //
    object Main {
    val sshHost = "localhost"
    val sshPort = 2222
    val testUsername = "test"
    val testPassword = "test"

    def go:Unit = {
    println("Starting operations")

    val sshd = SshServer.setUpDefaultServer
    sshd.setHost(sshHost)
    sshd.setPort(sshPort)
    sshd.setShellFactory(new ProcessShellFactory("/bin/cat"))
    sshd.setKeyPairProvider(new SimpleGeneratorHostKeyProvider())
    sshd.setPasswordAuthenticator(new PasswordAuthenticator {
    override def authenticate(username: String, password: String, serverSession: ServerSession): Boolean = {
    username==testUsername && password == testPassword
    }
    })
    sshd.start()

    val ssh = SshClient.setUpDefaultClient()
    ssh.start()
    val session = ssh.connect(testUsername, sshHost, sshPort).verify(5, TimeUnit.SECONDS).getSession
    session.addPasswordIdentity(testPassword)
    session.auth.verify(5, TimeUnit.SECONDS)
    val channel:ClientChannel = session.createChannel(Channel.CHANNEL_SHELL)

    val baos = new ByteArrayOutputStream
    val w = new OutputStreamWriter(baos)
    w.write("id")
    w.close()

    channel.setIn(new ByteArrayInputStream(baos.toByteArray))
    val out: ByteArrayOutputStream = new ByteArrayOutputStream
    val err: ByteArrayOutputStream = new ByteArrayOutputStream
    channel.setOut(out)
    channel.setErr(err)
    channel.open
    channel.waitFor(java.util.Arrays.asList(ClientChannelEvent.CLOSED), 0)

    val result: String = out.toString.trim

    println("-->'"+result+"'") // should print 'id' if the current os has an available "/bin/cat" command
    assert(result == "id")

    sshd.stop()
    }
    }

    Main.go