1 package org.argeo.slc.jsch; 2 3 import java.net.URL; 4 import java.util.ArrayList; 5 import java.util.List; 6 7 import javax.security.auth.Subject; 8 import javax.security.auth.login.LoginContext; 9 10 class GetMachineId { 11 12 public static void main(String[] args) { 13 URL url = GetMachineId.class.getResource("jaas.cfg"); 14 System.setProperty("java.security.auth.login.config", url.toExternalForm()); 15 16 String hostname = args[0]; 17 try { 18 LoginContext lc = new LoginContext("SINGLE_USER"); 19 lc.login(); 20 21 SshTarget sshTarget = new SshTarget(); 22 sshTarget.setHost(hostname); 23 RemoteExec remoteExec = new RemoteExec(); 24 remoteExec.setCommand("cat /etc/machine-id"); 25 remoteExec.setSshTarget(sshTarget); 26 List<String> lines = new ArrayList<>(); 27 remoteExec.setStdOutLines(lines); 28 29 Subject.doAs(lc.getSubject(), remoteExec.asPrivilegedAction()); 30 31 String machineId = lines.get(0); 32 System.out.println(hostname + ": " + machineId); 33 } catch (Exception e) { 34 e.printStackTrace(); 35 } 36 37 } 38 39 }