Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
B
backupCoordinator
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Operations
Operations
Incidents
Environments
Packages & Registries
Packages & Registries
Package Registry
Container Registry
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
kowis-projects
backupCoordinator
Commits
7a614c9c
Commit
7a614c9c
authored
Jul 30, 2018
by
David Kowis
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Doin some Experiments
Learning how to use the SSH library, and do stuff with it.
parent
1b5f03a4
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
101 additions
and
34 deletions
+101
-34
src/main/scala/is/kow/backupcoordinator/actors/SSHCommandActor.scala
...ala/is/kow/backupcoordinator/actors/SSHCommandActor.scala
+3
-2
src/main/scala/is/kow/backupcoordinator/experimentation/EUtils.scala
...ala/is/kow/backupcoordinator/experimentation/EUtils.scala
+48
-0
src/main/scala/is/kow/backupcoordinator/experimentation/SSHActorExperiment.scala
...ackupcoordinator/experimentation/SSHActorExperiment.scala
+40
-0
src/main/scala/is/kow/backupcoordinator/experimentation/SSHTest.scala
...la/is/kow/backupcoordinator/experimentation/SSHTest.scala
+10
-32
No files found.
src/main/scala/is/kow/backupcoordinator/actors/SSHCommandActor.scala
View file @
7a614c9c
...
...
@@ -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
...
...
src/main/scala/is/kow/backupcoordinator/experimentation/EUtils.scala
0 → 100644
View file @
7a614c9c
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
()
}
}
}
src/main/scala/is/kow/backupcoordinator/experimentation/SSHActorExperiment.scala
0 → 100644
View file @
7a614c9c
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"
)
}
}
}
}
src/main/scala/is/kow/backupcoordinator/experimentation/SSHTest.scala
View file @
7a614c9c
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}"
)
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment