what about interactivity, ie executing shell comma...
# scripting
e
what about interactivity, ie executing shell commands with
sudo
where I have to type the pwd? Shall I directly execute the script itself with
sudo
?
m
Interactivity is relatively easy as you can read from stdin with kotlin with something as easy as https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.io/read-line.html
e
awesome, I'll try
m
Now becoming root in the middle of a
.kts
is another story, you'll certainly need to fiddle with setuid, whatever sudo is doing under the hood
But if you just want to run a sudo command, you should be able to readirect stdin to sudo to type the password
Copy code
ProcessBuilder().command("sudo", "whoami").apply {
  inheritIO()
}.start().waitFor()
e
I'm trying with
Copy code
println(System.`in`.readAllBytes().contentToString())
but the execution gets stuck there
m
stdin is never closed as long as the script is running so
readAllBytes
will never finish. You'll have to stop reading on a special char like
'\n'
for an example (which
readLine
is doing)
e
sorry, I didn't understood.
readLine
works flawless indeed
👍 1
m
But if you want to pipe that to
sudo
,
inheritIO
like above is easier than first reading the password and then feeding it to sudo
e
no, I was already using that construct, I just missed the
inheritIO
and
wairFor()
thanks Martin
👍 1
e
note, sudo really wants to interact with a real terminal, not a redirection, and it will open the controlling terminal for logging and password interaction even if its stdin stdout stderr have been redirected. if you really need to pass a password into sudo programmatically, there is
-S
, or using JNI to work with pty, but I wouldn't recommend either of those
❤️ 1
e
No, I need it interactively