I am scanning a subnet to gather WMI information on assets on the network. However, if a machine is stuck in POST or has some unknown issue, it will freeze the script and never move on to the next machine. Question is, is there a way to set a timeout on PHP COM WbemScripting.SWbemLocator?
$host = 10.1.1.5; //Host is online, but may be hung trying to shutdown
//Check if host is online
if(#$fp = fsockopen($host,135,$errCode,$errStr,0.1))
{
//Create new connection
$WbemLocator = new COM ("WbemScripting.SWbemLocator");
//Connect to remote workstation
$WbemServices = $WbemLocator->ConnectServer($host, 'root\\cimv2',$user,$password);
$WbemServices->Security_->ImpersonationLevel = 3;
//Basic WMI query
$system = $WbemServices->execQuery("Select * from Win32_ComputerSystem");
foreach($pcsystem AS $n){
$hostname = $n->Name; //Hostname
}
//Process all data ->Insert into DB
}
//Move on to next machine. In this case, the script will never move on
Short answer, no - because it's a COM object, not a PHP object, and there's no facility to control this directly.
Longer, speculative answer, you could try tweaking the TCP parameters via the registry notably TcpInitialRtt although there doesn't seem to be a lot of scope for changing the behaviour unless you are running on a very reliable and uncongested network. And you'll probably break other things running on the machine.
Related
Objective: Update prices of products between databases: Shop's server DB has the latest prices and website's DB need to be updated accordingly with any "each 24 hours" script (I'll look this up later).
I'm using Ionos as hosting for the website, and The server is shared, so I can't touch php.ini or add files for php.
I'm trying to connect to a SQL server DB, but since it requires dll libraries to be installed and to modify the php.ini, I can't do that.
I can't either make it from the other side, If I make it from an external server in order to update the prices of the website, they don't allow to make connections out of the context of the server.
So, I know that the solution is to upgrade the hosting's plan and pay more and so on, so I have a virtual server for my own. But before doing that, is there any other way to establish this connection without using php? Is there something else that allows me to create a DB connection?
The fatal errors appears as soon as sqlsrv_connect is read as there is no library to load this function.
$serverName = "x, 0000";
$connectionInfo = array( "Database"=>"x", "UID"=>"x", "PWD"=>"xxx");
$conn = sqlsrv_connect( $serverName, $connectionInfo);
Edit: Comes to my mind... Maybe a solution would be to tell this php file to load php.ini and so on from another server if that's possible?
You could possibly call a JSON endpoint on your DB server (secure the endpoint though (out of scope for my answer)) https://3v4l.org/Gpi28
<?php
// MSSQL server side
$data = [
1 => 'hello',
2 => 'world',
];
// Imagine $data above is the array of rows returned by the db query
header('Content-Type: application/json');
echo json_encode($data);
exit;
// IONOS Side
$json = file_get_contents('http://your-database-server/some/url/or/other');
$data = json_decode(true);
// Now do your updates
// NB This is an INSECURE example, people who know the URL can see this data!
Good day,
I have created an IFTTT receipe that if a "myfox" alarm system is armed, a php script is executed on my NAS (192.165.x.x). The php script is supposed to trigger a stored procedure in my mysql database.
The following PHP script has been tested by other means and I'm sure that it works :
<?php
/*
php_update_mode_armed.php
*****************************************************************************************
* This script updates the value of the components in the table tbl_eedomus_current_mode
* It calls the stored procedure 'sp_tbl_eedomus_current_mode_armed'
*****************************************************************************************
Version 1.00, 09.06.2017, Initial version of the script
*/
mainProcess();
function mainProcess()
{
$ServerIP = "192.165.x.x";
$sqlUser = "domoos";
$sqlDatabase = "domoos";
$pw = "myPass";
// Connect
$mysqli = new mysqli($ServerIP, $sqlUser, $pw, $sqlDatabase);
if(!$mysqli) {[![enter image description here][1]][1]
header('Location: error.php?error=DbConnectionFailure');
die();
}
// Call stored procedure sp_tbl_eedomus_current_mode_armed
if(!$mysqli->query("CALL sp_tbl_eedomus_current_mode_armed ()"))
{
echo "OK";
if($mysqli) $mysqli->close(); // Close DB connection
//header('Location: error.php?error=QueryFailure');
die();
}
if($mysqli) $mysqli->close(); // Close DB connection
}
?>
Below, also, a screen shot of my "then" part of the IFTTT receipe.
Am I doing something wrong here or is the use of IFTTT not fit for the purpose I'm trying to achieve here?
Many thanks for your help on this matter and have a great day.
Most likly IFTTT can't access your NAS from the internet due to home router firewall etc. Also the ip address used in IFTTT shouldn't be a local ip like 192.168.* but your public ip address. You can figure this out by googling for "whats my ip".
Best way to test your setup using your laptop is to disconnect from local wifi network, tether your phone to your laptop and try visiting the NAS ip url to see if it still works. You can use chrome postman app to send out POST requests.
If that's all fine, get PHP to log incoming connections by writing to a file. file_put_contents("log.txt", print_r($_REQUESTS, true));
I would suggest trying with a GET request first with default content type. Good luck!
currently, I am programming an Android app that connects to a php-server. On that server are some php-scripts, which send mysqli_queries and receive responses of remote mysql databases.
<?php
error_reporting(0);
$db_host = $_POST["dbhost"];
$db_uid = $_POST["dbusername"];
$db_pass = $_POST["dbpassword"];
$db_name = $_POST["dbname"];
if ($db_host == "" || $db_uid == "" || $db_pass == "" || $db_name =="" )
echo "Missing information!";
else
{
$con = mysqli_connect($db_host,$db_uid, $db_pass, $db_name);
// Check connection
if (mysqli_connect_error())
die("Couldn't connect to the database!");
else
echo "Connection Successfull";
}
?>
That works without any problems, but now, I need to connect to an intranet first, to get access on a local database.
I thought about a php-script that connects to the intranet (via vpn) and if the connection is established it runs the mysql queries.
After the php-script received the response it disconnects and sends the result back to the Android app. Sadly I've never done that before and Google seems to not know an answer.
I don't want that the whole server connects to the intranet itself, just the scripts are allowed to establish a connection, because other mysqli_queries are executed at the same time.
My question, is that possible?
And if it isn't, is there another solution how to solve my problem?
Thank you pretty much in advance.
The best solution is that you created a web service on your intranet and you consume it from your android device you can enviyer gives them the get method of utl web service.
For the augulenter securiter your web service you can add a variable that you patage between the web service and the android application.
To solve this you have to setup a specific port that will communicate outside or an api and secure it via a public key. More exactly you can just forward a port what can work on http from the server in your intranet. Something like http://address:8888 and pull data through an API . That's the most secure way. Please take this answer into account only if you feel that I understood correctly. If now add a comment and I can still give you some ideas.
Is it possible to connect to a database lying on different server through another server ?
For example :-
I want to access the users table of www.abc.com from www.xqz.com, both are on different server.
Can you give me any ideas ?
will really appreciate if provide me the php query to connect.
Thanks.
"Is it possible" is a bit over-reaching, I think. To give you the shortest answer, yes, it is possible. However, whether or not you could do it depends on a lot of variables, not least of which is whether your hosting providers allow it. For instance, I use Hostmonster for Linux hosting, and to remotely access my MySQL database I have to first whitelist the IP address of the machine that will be accessing it.
Most hosting services provide a F.A.Q. section that should do a decent job of answering this particular question for you. Some will even provide support technicians help you set it up (Hostmonster does, within reason).
HTH.
Try This :
<?php
// Server in the this format: <computer>\<instance name> or
// <server>,<port> when using a non default port number
// Suppose your "www.abc.com" has an IP = 192.168.1.1
// So to connect, do something like this :
$server = '192.168.1.1,PORT_NUMBER'; // FOR WINDOWS
$server = '192.168.1.1:PORT_NUMBER'; // FOR LINUX
// Connect to MSSQL
$link = mssql_connect($server, [username], [password]);
if (!$link) {
die('Something went wrong while connecting to MSSQL');
}
?>
I have a Master - Slave setup for a web application written in PHP. I have a pool of slaves I use for reading, and a Master that is used for writes (and reads if a write has been sent this request). I would like to incorporate an automated system for removed crashed servers from the read pool. Currently I am using:
foreach($readers as $reader)
{
$fp = #fsockopen($reader['host'],3306,$errno,$errstr,1);
if(!$fp)
{
//Remove from pool
}
unset($fp);
}
My primary question is there a more reliable method. I have had quite a few false positives, and vice versa because it is not actually checking for a MySQL server, but rather just a connection on port 3306. Is there a way to check for a MySQL server without raising an exception, which is the behaviour of the PDO and MySQLi extensions in PHP.
You could just use mysql_connect() and check the result for false, and close the connection right away on success. You can make a dummy account with no privileges for that if you like.
That's really the only reliable way, especially if you want to distinguish a running MySQL server from any other random process listening on port 3306.
You could use mysql_ping() to check if a current DB Connection you have open is still alive
Here is the example posted at http://www.php.net/manual/en/function.mysql-ping.php
<?php
set_time_limit(0);
$conn = mysql_connect('localhost', 'mysqluser', 'mypass');
$db = mysql_select_db('mydb');
/* Assuming this query will take a long time */
$result = mysql_query($sql);
if (!$result) {
echo 'Query #1 failed, exiting.';
exit;
}
/* Make sure the connection is still alive, if not, try to reconnect */
if (!mysql_ping($conn)) {
echo 'Lost connection, exiting after query #1';
exit;
}
mysql_free_result($result);
/* So the connection is still alive, let's run another query */
$result2 = mysql_query($sql2);
?>
The best way to check if any service is alive is to actually use it. So for MySQL try to connect and execute some fast query, for web server try to fetch some file, for PHP try to fetch some simple script...
For MySQL master/slave setup, one of the solutions is to actually check the state of replication. You can check how many transactions is the slave behind master and decide to stop using that slave when/while it has old data. (I don't do the replication myself, but I think you need to compare the variables Read_Master_Log_Pos and Relay_Log_Pos)