Skip to content

Instantly share code, notes, and snippets.

@ShiSHcat
Last active September 18, 2025 14:47
Show Gist options
  • Select an option

  • Save ShiSHcat/728ecb62d5f5d71a5bc086dfa988e413 to your computer and use it in GitHub Desktop.

Select an option

Save ShiSHcat/728ecb62d5f5d71a5bc086dfa988e413 to your computer and use it in GitHub Desktop.
#!/usr/bin/php
<?php
// 1) Your “IMSI => desired dongle ID” map:
$correctAssociations = [
'XXX' => '3639491481',
'XXX' => '3123064351',
'XXXX' => '3189423538',
'XXX' => '3773956301',
];
// Function to get live IMSI => ID map
function getLiveMap() {
$raw = shell_exec('asterisk -rx "dongle show devices"');
if ($raw === null) {
fwrite(STDERR, "ERROR: cannot run 'dongle show devices'.\n");
exit(1);
}
$liveMap = [];
foreach (explode("\n", $raw) as $line) {
if (preg_match('/^(\d{10})\s+\S+.+\s+(\d{15})\s+\S+$/', trim($line), $m)) {
list(, $id, $imsi) = $m;
$liveMap[$imsi] = $id;
}
}
return $liveMap;
}
// Step 1: check if everything is already correct
$liveMap = getLiveMap();
$needsFix = [];
foreach ($correctAssociations as $imsi => $desiredId) {
if (!isset($liveMap[$imsi])) {
echo ">> IMSI $imsi not found in live devices. Skipping.\n";
} elseif ($liveMap[$imsi] !== $desiredId) {
$needsFix[$imsi] = $liveMap[$imsi];
}
}
if (empty($needsFix)) {
echo ">> All dongle ID assignments are already correct. No reload or changes needed.\n";
exit(0);
}
// Step 2: Read original config
$confFile = '/etc/asterisk/dongle.conf';
$conf = file_get_contents($confFile);
if ($conf === false) {
fwrite(STDERR, "ERROR: cannot read $confFile\n");
exit(1);
}
// Step 3: Replace only incorrect IDs with temp ones
function generateRandomId($existing) {
do {
$id = strval(random_int(1000000000, 9999999999));
} while (in_array($id, $existing));
return $id;
}
preg_match_all('/\[(\d{10})\](.*?)(?=\n\[|\z)/s', $conf, $blocks, PREG_SET_ORDER);
$tempMap = [];
$usedIds = [];
$modifiedConf = $conf;
foreach ($blocks as $block) {
$originalId = $block[1];
if (!in_array($originalId, $needsFix)) {
continue;
}
$content = $block[0];
$randomId = generateRandomId(array_merge($usedIds, array_values($correctAssociations)));
$usedIds[] = $randomId;
$tempMap[$originalId] = $randomId;
$updatedBlock = preg_replace(
['/^\[' . preg_quote($originalId, '/') . '\]/m', '/exten=' . preg_quote($originalId, '/') . '/'],
['[' . $randomId . ']', 'exten=' . $randomId],
$content
);
$modifiedConf = str_replace($content, $updatedBlock, $modifiedConf);
}
file_put_contents($confFile, $modifiedConf);
echo ">> Replaced only incorrect IDs with temporary values.\n";
shell_exec('asterisk -rx "dongle reload now"');
echo ">> Reloaded Asterisk with temporary IDs.\n";
// Step 4: Wait up to 10 seconds for IMSIs to reappear
$maxWait = 10;
$start = time();
$liveMap = [];
while (true) {
$liveMap = getLiveMap();
$allFound = true;
foreach (array_keys($needsFix) as $imsi) {
if (!isset($liveMap[$imsi])) {
$allFound = false;
break;
}
}
if ($allFound || (time() - $start) >= $maxWait) {
break;
}
echo ">> Waiting for IMSIs... " . implode(', ', array_diff(array_keys($needsFix), array_keys($liveMap))) . "\n";
sleep(1);
}
// Step 5: Replace temporary IDs with desired final ones
$finalConf = $modifiedConf;
$changed = false;
foreach ($needsFix as $imsi => $oldId) {
if (!isset($liveMap[$imsi])) {
echo ">> IMSI $imsi still not found after reload. Skipping.\n";
continue;
}
$tempId = $liveMap[$imsi];
$finalId = $correctAssociations[$imsi];
$pattern = '/\b' . preg_quote($tempId, '/') . '\b/';
$newConf = preg_replace($pattern, $finalId, $finalConf, -1, $count);
if ($count > 0) {
echo ">> Replaced $count occurrence(s): $tempId → $finalId\n";
$finalConf = $newConf;
$changed = true;
}
}
// Step 6: Final save and reload
if ($changed) {
file_put_contents($confFile, $finalConf);
echo ">> Written updated dongle.conf with final IDs.\n";
echo shell_exec('asterisk -rx "dongle reload now"');
echo ">> Final device list:\n";
echo shell_exec('asterisk -rx "dongle show devices"');
} else {
echo ">> No changes made to final IDs. Everything is now correct.\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment