Skip to content

Instantly share code, notes, and snippets.

@mocyuto
Created December 27, 2014 00:25
Show Gist options
  • Save mocyuto/d1f98d9845aedacb7258 to your computer and use it in GitHub Desktop.
Save mocyuto/d1f98d9845aedacb7258 to your computer and use it in GitHub Desktop.
ssh-scalaで画像をscpするコード
import java.io.File
import com.decodified.scalassh._
import com.typesafe.config.ConfigFactory
import org.slf4j.LoggerFactory
import spray.http.StatusCodes
import scala.io.Source
/**
* Created by Yuto Suzuki on 2014/12/24.
*/
object SshDao {
val config = ConfigFactory.load
def transportImg(filePath: String, fileName: String) = {
// 転送するホスト名
val host = config.getString("img.server.host")
val logger = LoggerFactory.getLogger(getClass)
// user, password情報
val hostConfig = HostConfig(PasswordLogin(config.getString("img.server.user"), config.getString("img.server.password")))
// SSHをつなぐ
SSH(host, hostConfig) { client =>
client.exec(s"mkdir ${filePath}").right.map { result =>
logger.error(result.stdErrAsString())
}
// 画像のSCP送信
try client.upload(filePath + fileName, filePath + fileName).right.flatMap { _ =>
client.exec("ls " + filePath + fileName).right.map { result =>
logger.info(s"result: ${result.stdOutAsString()}")
}
} finally client.close()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment