Skip to content

Instantly share code, notes, and snippets.

@gubi
Last active December 27, 2015 08:28
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 gubi/7296053 to your computer and use it in GitHub Desktop.
Save gubi/7296053 to your computer and use it in GitHub Desktop.
Connect via SSH to remote Server, search and find a SAMBA conf file (smb.conf) and then parse it to php array
<?php
if(!isset($_POST["submit"])) {
?>
<h3>Importante</h3>
<p>
Compilando questo form uno script effettuer&agrave; una connessione SSH con la tua macchina, cercher&agrave; il file di configurazione SAMBA (smb.conf) e ne far&agrave; un parser.<br />
Lo script esegue operazioni in sola lettura, e ha valore esemplificativo della possibilit&agrave; di automatizzare un'installazione via remoto tramite interfaccia web.<br />
Il comando che verr&agrave; eseguito sulla macchina da te indicata &egrave;:<br /><tt>$ cat $(find / -type f -name "smb.conf" -print 2>/dev/null -quit)</tt>.<br />
<br />
In ogni caso, puoi consultare i files sorgenti qui: <a href="https://gist.github.com/gubi/7296053" target="_blank">https://gist.github.com/gubi/7296053</a>
</p>
<br />
<form method="post" action="">
<table cellpadding="5" cellspacing="5">
<tr>
<td>
<label>Indirizzo IP del Server ssh: <input type="text" name="ip_address" value="" placeholder="Server IP" /></label>
</td>
</tr>
<tr>
<td>
<label>Username: <input type="text" name="username" value="" placeholder="Username SSH" /></label>
</td>
</tr>
<tr>
<td>
<label>Password: <input type="password" name="password" value="" placeholder="Password SSH" /></label>
</td>
</tr>
<tr>
<td align="right">
<input type="submit" name="submit" value="Trova il file smb.conf &rsaquo;" />
</td>
</tr>
</table>
</form>
<?php
} else {
header("Content-type: text/plain");
require_once("../common/include/funcs/parse_conf_file.php");
$connection = ssh2_connect($_POST["ip_address"], 22);
ssh2_auth_password($connection, $_POST["username"], $_POST["password"]);
$stream = ssh2_exec($connection, 'cat $(find / -type f -name "smb.conf" -print 2>/dev/null -quit)');
stream_set_blocking($stream, true);
$output = stream_get_contents($stream);
print "~~~~~~~~ QUESTO E' IL TUO SMB.CONF INTERPRETATO ~~~~~~~~\n\n";
$samba_conf = parse_conf_file($output, false);
print_r($samba_conf);
print "\n\n~~~~~~~~ E QUESTO E' IL TUO SMB.CONF ORIGINALE ~~~~~~~~\n\n" . $output;
fclose($stream);
}
?>
<?php
function parse_conf_file($f, $read_file = true, $show_comment = false, $show_line_returns = false){
$null = "";
$r = array();
$first_char = "";
$sec = $null;
$comment_chars = ";#";
$num_comments = "0";
$num_newline = "0";
if($read_file) {
//Read to end of file with the newlines still attached into $f
$f = @file(trim($f));
if ($f === false) {
return -2;
}
} else {
$f = explode("\n", trim($f));
}
// Process all lines from 0 to count($f)
for ($i = 0; $i<@count($f); $i++) {
$w = @trim($f[$i]);
$first_char = @substr($w,0,1);
if ($w) {
if ((@substr($w,0,1) == "[") and (@substr($w,-1,1)) == "]") {
$sec = @substr($w,1,@strlen($w)-2);
$num_comments = 0;
$num_newline = 0;
} else if ((stristr($comment_chars, $first_char) == true)) {
if($show_comment){
$r[$sec]["Comment_" . $num_comments] = $w;
$num_comments = $num_comments +1;
}
} else {
// Look for the = char to allow us to split the section into key and value
$w = @explode(" = ",$w);
$k = @trim($w[0]);
unset($w[0]);
$v = @trim(@implode(" = ",$w));
// look for the new lines
if ((@substr($v,0,1) == "\"") and (@substr($v,-1,1) == "\"")) {
$v = @substr($v,1,@strlen($v)-2);
}
$r[$sec][$k] = $v;
}
} else {
if($show_line_returns){
$r[$sec]["lr_" . $num_newline] = $w;
$num_newline = $num_newline +1;
}
}
}
return $r;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment