Ich versuche, einige Operationen zu automatisieren, und eine solche Operation wechselt zu einem technischen Benutzer auf einer Remote-Linux-Maschine. Das Verfahren sieht wie folgt aus: melden Sie sich mit einem „normalen“ Benutzer und wechseln Sie dann mitJsch und sudo Befehl
sudo /bin/rootsh -i -u techUser
dem technischen Benutzer.
Hier ist mein Groovy Codebeispiel, das ich arbeite:
import com.jcraft.jsch.JSch
import com.jcraft.jsch.Session
import com.jcraft.jsch.Channel
import com.jcraft.jsch.ChannelExec
import com.jcraft.jsch.JSchException
class Main {
static void main(String[] args) {
int responseCode = 0
String responseText = ""
def targetHost = "targetHost"
def targetUser = "targetUser"
def technicalUser= "technicalUser"
def targetPass = "targetPass"
def targetPort = 22
Properties configConnection = new Properties()
configConnection.put("StrictHostKeyChecking", "no")
configConnection.put("PreferredAuthentications", "publickey,keyboard-interactive,password")
JSch jsch = new JSch()
try {
Session targetSession = jsch.getSession(targetUser, targetHost, targetPort)
targetSession.setPassword(targetPass)
targetSession.setConfig(configConnection)
targetSession.connect()
Channel channel = targetSession.openChannel("exec")
((ChannelExec) channel).setCommand("echo 'targetPass' | sudo -S -p /bin/rootsh -i -u technicalUser")
((ChannelExec) channel).setPty(true)
final ByteArrayOutputStream baos = new ByteArrayOutputStream()
((ChannelExec) channel).setErrStream(baos)
channel.setInputStream(null)
InputStream is = channel.getInputStream()
channel.connect()
byte[] tmp = new byte[1024]
while (true) {
while (is.available() > 0) {
int i = is.read(tmp, 0, 1024)
if (i < 0)
break
responseText = new String(tmp, 0, i)
}
if (channel.isClosed()) {
responseText = new String(baos.toByteArray())
responseCode = channel.getExitStatus()
break
}
try {
Thread.sleep(1000);
} catch (Exception ee) {
println("[ERROR] " + ee.getMessage())
}
}
channel.disconnect()
targetSession.disconnect()
println("RESULT: code: " + responseCode + ", text: \"" + responseText + "\"")
} catch (JSchException e) {
println("[ERROR] Exception, problem with connection: " + e.getMessage())
}
}
}
Das Ergebnis ist:
RESULT: code: 1, text: ""
Als ich
gesetzt((ChannelExec) channel).setPty(false)
das Ergebnis ist:
RESULT: code: 1, text: "/bin/rootshSorry, user targetUser is not allowed to execute '/bin/bash' as technicalUser on targetHost."
Wenn ich das Passwort aus der folgenden Zeile entfernen:
((ChannelExec) channel).setCommand("echo '' | sudo -S -p /bin/rootsh -i -u technichalUser")
Das Ergebnis ist:
RESULT: code: 1, text: "/bin/rootsh/bin/rootsh
Sorry, try again.
/bin/rootsh
/bin/rootsh
sudo: pam_authenticate: Authentication information cannot be recovered"
Und wenn ich den folgenden Befehl ein:
((ChannelExec) channel).setCommand("sudo -S -p /bin/rootsh -i -u technichalUser")
Es gibt keine Antwort überhaupt, während der Prozess die ganze Zeit läuft (der Prozess wartet vielleicht auf das Passwort)
Wenn jemand solch ein Problem oder ein ähnliches bereits gelöst hat, würde ich jede Hilfe wirklich schätzen.