hi, does any know how to fetch a file via scp from a remote
machine using expect inside php. I’ve tried a few things but no luck.
Look at the samples in this link and give it a try. If it doesn’t work, post your code here and
we will take a peek at it. (Please use the PHP button for any code you post.)
I suspect this isn’t working for me because I don’t have the PECL extension. I don’t have access to install it. That’s why I was trying expect.
So, show your code if it is not working and maybe we could help.
Or, tell us your error message. Not sure if the any extension causes it not to work…
so here is the expect script within php i am trying and am receiving the following error:
PHP Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING in /home/mdsadmin/public_html/clog.php on line 25[php][/php]
[php]<?php
// Copies file from remote host:
ini_set(“expect.timeout”, 30);
$stream = fopen(“expect://scp [email protected]:/eve.log ./messages.txt”, “r”);
$cases = array(
// array(pattern, value to return if pattern matched)
array(“password:”, “asked for password”),
array(“yes/no)?”, “asked for yes/no”)
);
while (true) {
switch (expect_expectl($stream, $cases)) {
case “asked for password”:
fwrite($stream, “my password\n”);
break;
case “asked for yes/no”:
fwrite($stream, “yes\n”);
break;
case EXP_TIMEOUT:
case EXP_EOF:
break 2; // break both the switch statement and the while loop
default:
die “Error has occurred!”;
}
}
?>[/php]
Good ! Now that we see your code, I think that the issue is that you are trying to use
a stream created from a standard or normal “FILE” open, not an “EXPECT” open. There are
differences in the stream layout.
Therefore, I think the issue with your errors is that you have to change from a file open to an
expect open. Here are two links that explain them form the PHP site. Try changing the open
code and let us know the results…
EXPECT command info: http://www.php.net/manual/en/function.expect-expectl.php
EXPECT OPEN info: http://www.php.net/manual/en/function.expect-popen.php
( NOTE: Your current “fopen” command should start with “scp” not the expect:// part! )
Curious if this fixes it for you… (I have never coded expect’s and find it interesting!)
I believe i dont have expect module for php as well. I was turned on to phpseclib and downloaded it and gave it a shot and it works. here’s what the sample looks like below, although now i’m trying to figure out how to have this executed when a user hits a submit button. Any thoughts ?
[php]<?php
include(‘Net/SFTP.php’);
define(‘NET_SFTP_LOGGING’, NET_SFTP_LOG_COMPLEX);
$sftp = new Net_SFTP(‘10.20.17.98’);
if (!$sftp->login(‘admin’, ‘passwd’)) {
exit(‘Login Failed’);
}
echo $sftp->pwd();
$sftp->get(’/usr/ft.log’, ‘./messages.log’);[/php]
Well, two ways jump out to me.
First, you can use a form on the page and the user presses the submit button and it goes to another page (or the same page) where the PHP code checks to see if they press that button and if so, it runs the routine.
Or, you could use a button that calls a JS routine that uses AJAX to load a small PHP file that contains that routine. These are fairly easy to find on the net. It’s just a small routine that loads a PHP file into a DIV.
What do you do with the file once you grab it? Is it displayed on the page? If so, the second version is good as it can allow the user to press the button as many times as they want to. That would allow them to “refresh” the screen whenever they wanted to.
If so, here is one way to load a PHP file into a DIV using the ONCLICK method:
HTML for the button and JQuery/AJAX to load the file…
[php]
Submit Query
function loadQueryResults() {
$(’#DisplayDiv’).load(‘test2.php’);
return false;
}
[/php]
Quite easy to do. Might work for you…
im kind of a newbie at this. I tried this but am seeing an empty html page:
[size=8pt][php]
TEST Submit QueryWell, you are missing all of the main standard HTML code and Javascript setup…
So, the Javascript (JQuery and AJAX) goes in it’s own script area not in the PHP.
The button is just a HTML tag that places a button on the screen. The extra code in the button’s
tag for ONCLICK makes the page do whatever is inside the LOADQUERYRESULTS() routine. That
routine is the JS code. It in turn, loads the second file which would be your PHP code you posted
before. That would be a separate file. So, here is the first HTML file which would be the one that
would be accessed. Basically just Javascript and a button and one DIV to hold the results…
I assumed you knew how to set up Java code, since that is a no, here is a sample for it.
(Note you have to tell your page to access the Jquery and AJAX functions.)
[php]
TEST Submit QueryNow, this routine will RELOAD the file named “test2.php” which would be where you would place all of
your PHP code. That would be the routine to grab the file and display the data in it. That is what would
be loaded into the “DisplayDiv” above… Note that I placed some text inside the current DIV. That is a
nice way to show the area so you can see if it gets replaced from the PHP file’s output.
test2.php:
[php]
[/php]
So, use this code for the testing page. Put your previous PHP code into a file named test2.php and try
it out. Let us know if it fails… Good Luck !
ok so here is my html:
[size=8pt][code]
TEST Submit Queryand here is my php file:
[size=8pt][php]<?php
include(‘Net/SFTP.php’);
define(‘NET_SFTP_LOGGING’, NET_SFTP_LOG_COMPLEX);
$sftp = new Net_SFTP('10.3.14.14');
if (!$sftp->login('support', '123ne')) {
exit('Login Failed');
}
$sftp->get(’/usr/nt.log’, ‘./messages.log’);
?>[/php][/size]
[left]i downloaded the jquery.min.js and jquery-1.9.1.min.js and saved them in the current directory. I dont see anything come back when i click the submit button. If i add an echo statement in the php it does come back
when i click the submit. So basically it’s not executing my sftp command, Although if i run the php manually
on the server it does go and sftp the file i need. Any suggestions please ?
thanks again as this is very instructional.[/left]
ok so ive changed my approach. what i have at the moment is the following. Two drop down menus and a submit button. Depending on what is chosen and submitted a certain text file will be displayed on the same page. i know im missing the variable checking on what gets passed from the dropdowns.
[size=8pt][php]
New York Toronto London Hong Kong |
Well, switching techniques in the middle of a thread doesn’t help getting it working.
So, these are the two EXACT files that I used for testing. They work as-is. If you alter them
such as using local libraries of JQuery, you might have to change them. So, I suggest you create
two files. One named fetchlog.html and one called fetchLog.php . Enter the data I am showing
below into each. Move these two files to the server. (I had to get your library and put on my server.)
Then, test it by going to the fetchlog.html file and tell me if it works. Then after that we can look at
your other code using drop-downs and see what goes there…
fetchlog.html:
[php]
fetchLog.php:
[php]
$sftp = new Net_SFTP('10.3.14.14');
if (!$sftp->login('support', '123ne')) {
exit('Login Failed');
}
$sftp->get('/usr/nt.log', './messages.log');
?>
[/php]Interestingly enough, this works on my local machine (localhost:) but not on my GoDaddy server.
Seems there is an option you have to change in their servers to allow for the site to access an
outside SFTP connection. Try these two files on your server and let us know if it works or fails.
One further comment on this. Seems that your SFTP is really like SSH. They both sit on top of the
TCP/IP connection in the IP stack. Therefore, on your server you need to have the correct options
for the SSH set up or you can not use that on your site.
hi Ernie,
so i tried this out but no luck. i click the submit button and it brings be to the same page. I also tried removing the path from the .js files since i downloaded them and put them in the same directory. Also if i run the fetchlog.php from the server it does work. What do you think? Thanks again. Also, i am running fiddler web debugger to see if when i click the submit button if anything happens but there is no activity.
Ok one more thing if i load a text file in the (’#DisplayDiv’).load(‘sometext.txt’) it comes back to the web page which is great.
Bishop, first, I answered your other post on dropdowns. I think it will make sense to you.
Later we can add that part in to select different servers as needed…
So, the two last files I posted work flawless on my localhost system. It grabs the data and it does
refresh it every time I press the button.
So a couple questions. Did you test it locally AND on the server with the exact code I last posted?
Did it work locally for you? What error(s) do you get on the server version? If it does not work, you
will receive an error message from the SFTP library.
When you say 'no luck" it does not tell me what is happening. So test the exact two files I posted
and let me know exactly what you get back for errors. I know you are close to getting it to work.