Commit 1b5f03a4 authored by David Kowis's avatar David Kowis

Doin some stuff

parent 64be7dde
......@@ -13,9 +13,19 @@ val sshj = "com.hierynomus" % "sshj" % "0.26.0"
val bouncycastle = "org.bouncycastle" % "bcprov-jdk15on" % "1.60"
val jzlib = "com.jcraft" % "jzlib" % "1.1.3"
val log4jV = "2.11.1"
val log4j = Seq(
"org.apache.logging.log4j" % "log4j-api" % log4jV,
"org.apache.logging.log4j" % "log4j-core" % log4jV,
"org.apache.logging.log4j" % "log4j-slf4j-impl" % "2.11.1"
)
lazy val backupCoordinator = (project in file("."))
.settings(
name := "Backup Coordinator",
libraryDependencies ++= Seq(akka, akkaTestkit, scalaTest, scalacticScalaTest, sshj, bouncycastle, jzlib)
libraryDependencies ++= Seq(
akka, akkaTestkit, scalaTest, scalacticScalaTest, sshj, bouncycastle, jzlib,
) ++ log4j
)
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</Console>
</Appenders>
<Loggers>
<Root level="info">
<AppenderRef ref="Console"/>
</Root>
</Loggers>
</Configuration>
\ No newline at end of file
package is.kow.backupcoordinator.actors
import java.util.concurrent.{Executors, TimeUnit}
import akka.actor.{Actor, Props}
import is.kow.backupcoordinator.actors.SSHCommandActor.{ExecuteCommand, ExecutionCompleted}
import net.schmizz.sshj.connection.channel.direct.Session
import scala.concurrent.{ExecutionContext, Future}
import scala.io.Source
import scala.util.{Failure, Success}
object SSHCommandActor {
def props(session: Session): Props = Props(new SSHCommandActor(session))
sealed trait SSHCommandActorMessages
case class ExecuteCommand(command: String) extends SSHCommandActorMessages
case class ExecutionCompleted(command: ExecuteCommand, output: String, exitValue: Integer) extends SSHCommandActorMessages
}
class SSHCommandActor(session: Session) extends Actor {
val singleThread = Executors.newSingleThreadExecutor()
implicit val executionContext = ExecutionContext.fromExecutor(singleThread)
override def receive: Receive = {
case e: ExecuteCommand =>
//This is really the only thing we're going to work on
val command = session.exec(e.command)
//This guy is fully synchronous and blocking so be sure to stick it into another thread
Future {
//Collect all the output
val output = Source.fromInputStream(command.getInputStream).mkString
command.join(5, TimeUnit.SECONDS)
val exitStatus = command.getExitStatus
//join on it, and then also get the exit status
ExecutionCompleted(e, output, exitStatus)
}.onComplete {
case Success(ec) =>
//Send the completed message back to the parent and stop myself
context.parent ! ec
context.stop(self)
case Failure(throwable) =>
//I think I just need to die, and throw the exception, which should allow my supervisor
// to do something about it
throw throwable
}
}
}
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 {
Security.addProvider(new BouncyCastleProvider())
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")
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()
}
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