Commit 7a614c9c authored by David Kowis's avatar David Kowis

Doin some Experiments

Learning how to use the SSH library, and do stuff with it.
parent 1b5f03a4
......@@ -31,6 +31,7 @@ class SSHCommandActor(session: Session) extends Actor {
case e: ExecuteCommand =>
//This is really the only thing we're going to work on
val command = session.exec(e.command)
val replyTo = sender()
//This guy is fully synchronous and blocking so be sure to stick it into another thread
Future {
//Collect all the output
......@@ -43,8 +44,8 @@ class SSHCommandActor(session: Session) extends Actor {
ExecutionCompleted(e, output, exitStatus)
}.onComplete {
case Success(ec) =>
//Send the completed message back to the parent and stop myself
context.parent ! ec
//Send the completed message back to the sender and stop myself
replyTo ! ec
context.stop(self)
case Failure(throwable) =>
//I think I just need to die, and throw the exception, which should allow my supervisor
......
package is.kow.backupcoordinator.experimentation
import java.nio.file.Paths
import com.typesafe.config.ConfigFactory
import net.schmizz.sshj.SSHClient
import net.schmizz.sshj.connection.channel.direct.Session
trait EUtils {
val home = System.getProperty("user.home")
val config = ConfigFactory.parseFile(Paths.get(home, ".ssh_config").toFile())
val password = config.getString("user.pass")
val username = config.getString("user.name")
val hostname = config.getString("host.name")
val hostKey = config.getString("host.fingerprint")
def establishConnection(context: SSHClient => Unit): Unit = {
val ssh = new SSHClient()
ssh.addHostKeyVerifier(hostKey)
ssh.connect(hostname)
ssh.authPassword(username, password)
//Sessions are a one-time-use thing with exec, have to create a new session each time
try {
context(ssh)
} finally {
ssh.disconnect()
}
}
def withSession(sshClient: SSHClient)(context: Session => Unit): Unit = {
val session = sshClient.startSession()
try {
context(session)
} finally {
session.close()
}
}
}
package is.kow.backupcoordinator.experimentation
import java.util.concurrent.TimeUnit
import akka.actor.ActorSystem
import is.kow.backupcoordinator.actors.SSHCommandActor
import is.kow.backupcoordinator.actors.SSHCommandActor.{ExecuteCommand, ExecutionCompleted}
import akka.pattern.ask
import akka.util.Timeout
import scala.concurrent.Await
import scala.concurrent.duration.Duration
object SSHActorExperiment extends App with EUtils {
val system = ActorSystem.create("SSHActorExperiment")
val commands = Seq(
"cat /proc/cpuinfo",
"ps -eaf",
"ls -lah /tmp",
"wharrgarbl"
)
implicit val timeout: Timeout = Timeout(10, TimeUnit.SECONDS)
establishConnection { ssh =>
commands.foreach { cmd =>
withSession(ssh) { session =>
val actor = system.actorOf(SSHCommandActor.props(session))
val resultFuture = (actor ? ExecuteCommand(cmd)).mapTo[ExecutionCompleted]
val result = Await.result(resultFuture, Duration(10, TimeUnit.SECONDS))
println(s"COMMAND: ${cmd}\nOutput:\n${result.output}\nExitStatus: ${result.exitValue}\n\n")
}
}
}
}
package is.kow.backupcoordinator.experimentation
import java.nio.file.{Files, Paths}
import java.security.Security
import java.util.concurrent.TimeUnit
import com.typesafe.config.ConfigFactory
import net.schmizz.sshj.SSHClient
import org.bouncycastle.jce.provider.BouncyCastleProvider
import scala.io.Source
object SSHTest extends App {
object SSHTest extends App with EUtils {
Security.addProvider(new BouncyCastleProvider())
val home = System.getProperty("user.home")
val config = ConfigFactory.parseFile(Paths.get(home, ".ssh_config").toFile)
establishConnection { ssh =>
val password = config.getString("user.pass")
val username = config.getString("user.name")
val hostname = config.getString("host.name")
val hostKey = config.getString("host.fingerprint")
withSession(ssh) { session =>
val command = session.exec("cat /proc/cpuinfo")
val output = Source.fromInputStream(command.getInputStream).mkString
val ssh = new SSHClient()
ssh.addHostKeyVerifier(hostKey)
ssh.connect(hostname)
ssh.authPassword(username, password)
val session = ssh.startSession()
val command = session.exec("cat /proc/cpuinfo")
val output = Source.fromInputStream(command.getInputStream).mkString
command.join(5, TimeUnit.SECONDS)
val exitStatus = command.getExitStatus
println(s"output:\n${output}\nExit Status: ${exitStatus}")
session.close()
ssh.disconnect()
command.join(5, TimeUnit.SECONDS)
val exitStatus = command.getExitStatus
println(s"output:\n${output}\nExit Status: ${exitStatus}")
}
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment