unable to connect using SSH2 - php

I am using phpseclib in my project. I have installed it and am testing it using the following piece of code which is similar to the example here.
Updated Code:
1 require("Net/SSH2.php");
2
3 if (!class_exists('Net_SSH2')) die("Class Net_SSH2 doesn't exist!");
4
5 $ssh = new Net_SSH2('***'); // the host IP address
6
7 if (!method_exists($ssh, 'Net_SSH2')) die("Net_SSH2 class doesn't have Net_SSH2 method!");
8
9 if (!$ssh->login('***', '***')) { // (verified) username and password
10 echo "LOG: " . $ssh->getLog() . "<br>";
11 exit('Login failed!');
12 }
Code Update 2:
1 require("Net/SSH2.php");
2
3 define('NET_SSH2_LOGGING', NET_SSH2_LOG_COMPLEX);
4
5 if (!class_exists('Net_SSH2')) die("Class Net_SSH2 doesn't exist!");
6
7 $ssh = new Net_SSH2('***'); // the host IP address
8
9 if (!method_exists($ssh, 'Net_SSH2')) die("Net_SSH2 class doesn't have Net_SSH2 method!");
10
11 if (!$ssh->login('***', '***')) { // (verified) username and password
12 echo "LOG: " . $ssh->getLog() . "<br>";
13 echo "ALL ERRORS: ";
14 print_r($ssh->getErrors());
15 echo "<br>LAST ERROR: " . $ssh->getLastError() . "<br>";
16 exit('Login failed!');
17 }
It exits at line # 16. getLog() gives nothing so it is hard to know where the problem is. I had the login credentials tested using PuTTY. It is not able to login to other servers either. I am not able to understand what is causing the problem.
Any help would be highly appreciated!

Net_SSH2 is not a function, this is a class and constructor within this class.
So 3rd line should be
if (!class_exists('Net_SSH2')) die("Class Net_SSH2 doesn't exist!");
If you wanna check further if Net_SSH2 method exists in class Net_SSH2 after line 5 you can add this:
if (!method_exists($ssh, 'Net_SSH2')) die("Net_SSH2 class doesn't have Net_SSH2 method");

If you hit the "Enable Logging" button on the link you provided you'll see that the following is done right after Net/SSH2.php is included:
define('NET_SSH2_LOGGING', NET_SSH2_LOG_COMPLEX);
Try that.

Try doing this at the top of your PHP script:
error_reporting(E_USER_NOTICE);
Setting error_reporting to 0 suppresses user_error so it's quite possible your PHP setup is just suppressing the error. eg.
error_reporting(E_USER_NOTICE);
user_error('zzzz', E_USER_NOTICE);
vs.
error_reporting(0);
user_error('zzzz', E_USER_NOTICE);

Of course you could change your line 3 to:
if( !class_exists('Net_SSH2')...
But I would suggest you throw in some Exception handling. When using the PHPSecLib, I had nothing but issues, so I created this SSH2 Class which does require the PHP SSH2 Extension to be installed.
You should try calling: getLog() to find out why it's failing.
echo $ssh->getLog();

I ended up hosting my code on the server (server B) that I was trying to connect. The same code worked in the opposite direction (I could connect to the server A where my code previously was)! I ended up concluding that the server A has environmental issues.
I appreciate those were helping me here!

Given the latest comments, I assume your code exists at line 8 (where the Net_SSH2() object is instantiated)...
I just had a very similar issue where, once deployed, include() would work but check_exists() and instantiating a new Net_SSH2 object would fail... But parsing my php file (before deployment) with the php command line tool would work fine...
So the problem was on the server (Apache in my case): the directory where phpseclib was installed had permissions set so that I could not even cd to it.
As it happened, my hoem made deploying script was simply copying the files on the server and setting odd permissions so that the SSH2.php was not accessible.
Perhaps you have a similar problem here?

Related

NET_SFTP not working when doing consecutive logins

the second call doesn't work:
$sftp = new Net_SFTP('domain1', 22);
var_dump($sftp->login('username1', 'password1'));
unset ($sftp);
$sftp2 = new Net_SFTP('domain2', 22);
var_dump($sftp2->login('username2', 'password2'));
when i do the second call without the first one, it works perfectly.
this is the error the second call produce:
Notice: Invalid HMAC in C:\WT-NMP\WWW\lib\phpseclib\Net\SSH2.php on line 3048
...
Notice: Connection closed by server in C:\WT-NMP\WWW\lib\phpseclib\Net\SSH2.php on line 2015
The library in the question https://github.com/phpseclib/phpseclib/tree/1.0
I could not reproduce the error with v1.0.x-dev "e6a2ce1". It outputs
bool(true)
bool(true)
PHP 5.5.37
Please confirm your domains are accessible, passwords are correct, the output remains the same when you change order of domains. Update the question with exact version of the library and php.

how to install Crawler-Detect given the instructions?

Sorry in advance if it the questions seems too basic. But I want to know how to install "Crawler-Detect" in my xampp and online server?
The instructions are given in the git link at: https://github.com/JayBizzle/Crawler-Detect
I just don't know how to install them. no one else seem to be having problem with this.
So far I have tried to download the package, place it where my project is, required the class like any regular class in php and run it. this error occured:
Fatal error: Class 'CrawlerDetect' not found in H:\xampp\htdocs\viralagain\theme\crawl.php on line 5
Then installed the composer package and run the command:
composer require jaybizzle/crawler-detect 1.*
This Error occured:
Fatal error: Class 'Jaybizzle\CrawlerDetect\CrawlerDetect' not found in H:\xampp\htdocs\viralagain\theme\crawl.php on line 5
Then in my composer.js (In The project directory) I added the
"jaybizzle/crawler-detect" :"1.*"
Same error.
Crawl.php:
<?php
require "Jaybizzle/src/CrawlerDetect.php";
use Jaybizzle\CrawlerDetect\CrawlerDetect;
$CrawlerDetect = new CrawlerDetect;
// Check the user agent of the current 'visitor'
if($CrawlerDetect->isCrawler()) {
// true if crawler user agent detected
echo "</br> [BOT DETECTED]";
}else{
echo "not a bot";
}
echo $botname = $CrawlerDetect->getMatches();
?>
I have changed the CrawlerDetect's source and test it.
download it from here:
http://www.parsebazar.com/#files/CrawlerDetect.zip
sample:
require "CrawlerDetect.php";
$CrawlerDetect = new CrawlerDetect;
// Check the user agent of the current 'visitor'
if($CrawlerDetect->isCrawler()) {
echo 'robot'; // true if crawler user agent detected
} else {
echo 'visitor';
}

com token don't work in php

i work with token (COM Object) , in java Script all things is okay . my problem is creating a object from it in php.
this is the code :
try {
// flag 1
$verifier = new COM("pkiactivex.Verifier") or die("Couldn't create the COM Component");
// flag 2
}
catch(com_exception $e)
{
//flag 3
}
first i should say this code work in another local system in wampp and i have xampp and i don't think it's because of xampp or wampp but i think maybe a module is missing. about code in my system code run until flag 1 and even its don't go to exception and flag 3 it's go no where , even i see a blank page with no error !!
i know it's not a php error and i put the code just for more detail!

Cannot set process title in a PHP command line script using cli_set_process_title() OS X?

I am trying to build a simple daemon script with PHP and I would like to set the process title to see it in ps and top output. This code is:
#!/usr/bin/php
<?php
// Daemonize
cli_set_process_title('daemonized');
$pid = pcntl_fork(); // parent gets the child PID, child gets 0
if($pid){ // 0 is false in PHP
// Only the parent will know the PID. Kids aren't self-aware
// Parent says goodbye!
print "Parent : " . getmypid() . " exiting\n";
exit();
}
print "Child : " . getmypid() . "\n";
while (true) {
// daemon stuff...
sleep(2);
}
But I cannot set the title, when I launch the script I get the following warning:
$ ./daemonize.php
PHP Warning: cli_set_process_title(): cli_set_process_title had an error: Not initialized correctly in /Applications/MAMP/htdocs/daemonize.php on line 3
Warning: cli_set_process_title(): cli_set_process_title had an error: Not initialized correctly in /Applications/MAMP/htdocs/daemonize.php on line 3
Where is the issue, what should I do in order to make it work?
Thanks for the attention!
This is not really an answer, per se, but I am not sure that you can still do this with cli-php in OS X 10.10.x without running the process as the superuser or other not-so-secure hacks.
I have tried chmoding the process (or more exactly the file that is being executed via php file.php), chowning the GID to another group and also running the script using sudo. All of these options failed for me even though the php process was being run by an authorized user who belonged to the admin group.
My guess: Apple must be blocking this functionality for security reasons... and I suppose I can imagine why.
In the end, because I was targeting a Linux runtime, I refactored my code so that cli_set_process_title() was not mission critical and ran the command with the error suppression operator, #cli_set_proc_title() and just logged the fact that the command failed.
I would welcome more complete answers or other secure work arounds but after much wasted time this was the best I could come up with and I was unable to find any other answers.
Hope this helps.

Connect to SQLite using PHP - Cannot Connect

I'm trying to use PHP to connect to SQLite. I created a database by importing a CSV file into the tables for three tables. However, I'm unable to connect using the following code:
$dbhandle = sqlite_open('db/pokedex.db', 0666, $error);
if(!$dbhandle) die ($error);
This returns the following error:
Warning: sqlite_open() [function.sqlite-open]: file is encrypted or is not a database in /pokedex/configpokedexdb-sqlite.php on line 12
file is encrypted or is not a database
Googling told me I might have a version mismatch. Despite finding some SQLite3 mentions in my phpinfo(), I decided it might still be a problem so I tried the following suggested code:
try
{
//connect to SQLite database
$dbhandle = new PDO("sqlite:db/pokedex.db"); //sqlite:VPN0.sqlite
// echo "Handle has been created ...... <br><br>";
}
catch(PDOException $e)
{
echo $e->getMessage();
echo "<br><br>Database -- NOT -- loaded successfully .. ";
die( "<br><br>Query Closed !!! $error");
}
After which I received the following error:
Warning: sqlite_exec() expects parameter 1 to be resource, object given in /home/rawdco81/public_html/pokedex/index-sqlite.php on line 53
Before this, I tried running new PDO("sqlite:VPN0.sqlite"); which was what the site provided, but that was obviously wrong because it didn't point to my .db file at all. You'll see this piece of code in the comments beside the function call.
I'm having a hard time just connecting to the database...What is the proper way to do this?
Also, I'm running PHP Version 5.2.13.
EDITED: I pasted the wrong error message in the wrong place.
Now that I look more closely.., I think you are connecting successfully in the second code segment! You shouldn't be using sqlite_exec in tandem w/ PDO though; those are two different PHP interfaces into SQLite.
The reason the first bit of code bombed is because the legacy library doesn't support PDO v3. The second bit of code is bombing once you try to run sqlite_exec against the PDO object I presume.
I bet if you put a var_dump($dbhandle); after the try/catch you'll see you've got an initialized PDO object.
Just read up on using PDO to run queries and you should be golden!

Categories