-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmd_ex1_3.php
56 lines (54 loc) · 2.08 KB
/
cmd_ex1_3.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<?php
echo "No of rows? Type integer to continue: ";
$handle = fopen("php://stdin", "r");
$num = trim(fgets($handle));
if (!is_numeric($num)) {
echo "No Integer Provided!\n";
exit;
}
$servername = "localhost";
$username = "root";
$password = "";
$db = 'teksystem';
$conn = mysqli_connect($servername, $username, $password, $db);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
for ($i = 0; $i < $num; $i++) {
$randIP = mt_rand(0, 255) . "." . mt_rand(0, 255) . "." . mt_rand(0, 255) . "." . mt_rand(0, 255);
$chars1 = '0123456789abcdefghijklmnopqrstuvwxyz';
$randSapId = substr(str_shuffle($chars1), 0, 18);
$chars2 = 'abcdefghijklmnopqrstuvwxyz';
$randHostName = substr(str_shuffle($chars2), 0, 10) . '.com';
$randMac = implode(':', str_split(str_pad(base_convert(mt_rand(0, 0xffffff), 10, 16) . base_convert(mt_rand(0, 0xffffff), 10, 16), 12), 2));
$types = ['AG1', 'CSS'];
$randType = $types[rand(0, count($types) - 1)];
$ipExists = mysqli_query($conn, "select loopback from tbl_router where loopback = '$randIP'");
if (mysqli_num_rows($ipExists) > 0) {
$i--;
return;
}
$sapIdExists = mysqli_query($conn, "select sapid from tbl_router where sapid = '$randSapId'");
if (mysqli_num_rows($sapIdExists) > 0) {
$i--;
return;
}
$hostNameExists = mysqli_query($conn, "select hostname from tbl_router where hostname = '$randHostName'");
if (mysqli_num_rows($hostNameExists) > 0) {
$i--;
return;
}
$macAddExists = mysqli_query($conn, "select mac_address from tbl_router where mac_address = '$randMac'");
if (mysqli_num_rows($macAddExists) > 0) {
$i--;
return;
}
$q = "insert into tbl_router (sapid, loopback, hostname, mac_address, type, added_at, updated_at)
values ('$randSapId', '$randIP', '$randHostName', '$randMac', '$randType', '" . date("Y-m-d H:i:s") . "' ,'" . date("Y-m-d H:i:s") . "')";
if ($conn->query($q) !== TRUE) {
echo "Error: " . $sql . "<br>" . $conn->error;
}
}
echo "COMPLETED\n";
exit;