Skip to content

Instantly share code, notes, and snippets.

@heffebaycay
Created September 17, 2016 17:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save heffebaycay/5d01b9a41594fd82bb2e59c87bf5a4bd to your computer and use it in GitHub Desktop.
Save heffebaycay/5d01b9a41594fd82bb2e59c87bf5a4bd to your computer and use it in GitHub Desktop.
SFR ToIP Monitoring in Groovy via Jenkins
import javax.crypto.Mac
import javax.crypto.spec.SecretKeySpec
import java.security.MessageDigest
def username = ""
def password = ""
def response = new XmlSlurper().parse("http://192.168.1.1/api/1.0/?method=auth.getToken")
def token = response.auth.@token.toString();
def usernameHASH = computeSHA256(username)
def usernameHMAC = hmacDigestSHA256(usernameHASH, token)
def passwordHASH = computeSHA256(password)
def passwordHMAC = hmacDigestSHA256(passwordHASH, token)
def fullHash = usernameHMAC + passwordHMAC
def checkToken = new XmlSlurper().parse("http://192.168.1.1/api/1.0/?method=auth.checkToken&token=$token&hash=$fullHash")
if (checkToken.@stat != "ok") {
println "Failed to login to NeufBox router"
}
def voipGetInfo = new XmlSlurper().parse("http://192.168.1.1/api/1.0/?method=voip.getInfo&token=$token")
def voipStatus = voipGetInfo.voip.@status
println "VoIP status is: $voipStatus"
if (voipStatus == "up") {
build.setResult(hudson.model.Result.SUCCESS);
} else {
build.setResult(hudson.model.Result.UNSTABLE);
}
public static String computeSHA256(String input) {
String strDigest = null;
try {
MessageDigest md = MessageDigest.getInstance("SHA-256");
byte[] digest = md.digest(input.getBytes("UTF-8"));
strDigest = digest.encodeHex().toString();
} catch (Exception e) {
println "Failed to compute SHA256 Hash: " + e
} finally {
return strDigest;
}
}
public static String hmacDigestSHA256(String msg, String keyString) {
return hmacDigest(msg, keyString, "HmacSHA256");
}
public static String hmacDigest(String msg, String keyString, String algorithm) {
String digest = null;
try {
SecretKeySpec key = new SecretKeySpec(keyString.getBytes("UTF-8"), algorithm);
Mac mac = Mac.getInstance(algorithm);
mac.init(key);
byte[] bytes = mac.doFinal(msg.getBytes("ASCII"));
StringBuffer hash = new StringBuffer();
for (int i = 0; i < bytes.length; i++) {
String hex = Integer.toHexString(0xFF & bytes[i]);
if (hex.length() == 1) {
hash.append('0');
}
hash.append(hex);
}
digest = hash.toString();
} catch (Exception e) {
println "Failed compute HMAC digest: " + e
} finally {
return digest;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment