/    Sign up×
Community /Pin to ProfileBookmark

set_stream_blocking() and fread() question

I’ve gotten some awesome help here, and am having a hard time with another piece of a project. For part of it, I’m writing a small ssh library, to ssh out, run a command, capture the output, and return it to the user. I’m unable to get all of the output of the command. I can get most of it, but I’m missing the MOTD and am not getting the rest of the output. I’m thinking the problem is in my logic and unfamiliarity with the commands (even after looking at the php man).

Here’s what I have, and it does work connection wise, and the commands I pass in do run, but I’m only getting partial output back. Should I be getting more than 4096 bytes in the fread? That seems like an awful lot. Do I need to set stream blocking to true? Btw, I set the timeouts way up for this, expecting large outputs for some of my commands.

[CODE]
if(!($con = ssh2_connect(jumpServer, 22))){
echo “fail: unable to establish connection to $jumpServern”;
} else {
// try to authenticate w/ user and secret pass
if(!ssh2_auth_password($con, $username, $password)) {
echo “fail: unable to authenticate user $usernamen”;
} else {
// debug
//echo “okay: logged in…n”;
//echo $command.”n”;

// execute a command
if (!($stream = ssh2_exec($con, $command))) {
echo “fail: unable to execute command $commandn”;
} else {
// collect output from command
stream_set_blocking($stream, true);
$data = “”;
while ($buf = fread($stream,4096)) {
$data .= $buf.”n<br />”;
}
fclose($stream);
if ($data != “”){
echo $data;
}else{
echo “data: “. $data . ” but NO DATA”;
}
return $data;
}
}
}

[/CODE]

to post a comment
PHP

0Be the first to comment 😎

×

Success!

Help @mcru spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...