0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

scala-sshを使ってリモート先でsudoコマンドを実行する方法

0
Posted at

scala-sshを使ってリモートサーバにアクセスして、sudoコマンドを使おうと思ったら詰まったのでメモ。

sudo使ってlsコマンドを叩きたかったわけではないけど、手軽だったため今回はlsでサンプルを用意した。

val command = "ls -l"
SSH("<リモートサーバーのホスト名>") { client =>
  val li = client.exec(command).right.map { res => res.stdOutAsString()
  println(li)
}

---
Right(total 0
-rw-rw-r-- 1 fuppi fuppi 0 Jun 27 17:13 1.txt
-rw-rw-r-- 1 fuppi fuppi 0 Jun 27 17:14 2.txt
-rw-rw-r-- 1 fuppi fuppi 0 Jun 27 17:14 3.txt
-rw-rw-r-- 1 fuppi fuppi 0 Jun 27 17:14 4.txt
)

sudoで実行しようとしても

val command = "sudo ls -l"
SSH("<リモートサーバーのホスト名>") { client =>
  val li = client.exec(command).right.map { res => res.stdOutAsString()
  println(li)
}

---
Right()

何も帰ってこなくなる。エラーとか警告すらでない。

val command = "sudo ls -l"
SSH("<リモートサーバーのホスト名>") { client =>
  val li = client.execPTY(command).right.map { res => res.stdOutAsString()
  println(li)
}

---
Right(total 0
-rw-rw-r-- 1 fuppi fuppi 0 Jun 27 17:13 1.txt
-rw-rw-r-- 1 fuppi fuppi 0 Jun 27 17:14 2.txt
-rw-rw-r-- 1 fuppi fuppi 0 Jun 27 17:14 3.txt
-rw-rw-r-- 1 fuppi fuppi 0 Jun 27 17:14 4.txt
)

exec -> execPTYに変えたら実行できた。
エラーも警告もないというのが辛かった・・・

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?