Jump to content

check if file exists on remote server...NOT using http


bschultz

Recommended Posts

I need to check (from a Debian server to a Mac CLIENT (not server) machine) to see if a file exists on the file system...not an http server.

 

Here's what I've tried (based on reading at http://www.php.net/manual/en/function.ssh2-exec.php)

 

<?php
function check_remote_files($username, $hostname, $remote_file)
{
$connection = ssh2_connect($hostname, 22, array('hostkey'=>'ssh-rsa'));

if (ssh2_auth_pubkey_file($connection, $username,
                          '/root/.ssh/id_rsa.pub',
                          '/root/.ssh/id_rsa', '')) 
{
echo "Public Key Authentication On Server #2 Successful\n";
}

$url = "if ssh $hostname stat $remote_file \> /dev/null 2\>\&1";
$stream=ssh2_exec($connection,$url);
    stream_set_blocking( $stream, true );
    $cmd=fread($stream,4096);
fclose($stream);

$stream=ssh2_exec($connection,"then");
    stream_set_blocking( $stream, true );
    $cmd=fread($stream,4096);
fclose($stream);

$stream=ssh2_exec($connection,"echo File exists");
    stream_set_blocking( $stream, true );
    $cmd=fread($stream,4096);
fclose($stream);

$stream=ssh2_exec($connection,"else");
    stream_set_blocking( $stream, true );
    $cmd=fread($stream,4096);
fclose($stream);

$stream=ssh2_exec($connection,"echo Not found");
    stream_set_blocking( $stream, true );
    $cmd=fread($stream,4096);
fclose($stream);

$stream=ssh2_exec($connection,"fi");
    stream_set_blocking( $stream, true );
    $cmd=fread($stream,4096);
fclose($stream);

}
?>

 

NOTHING happens after the echo of Public Key Authentication On Server #2 Successful.

 

I'm running this script from a bash shell.

 

Any ideas what might be wrong?

 

Thanks.

 

 

Link to comment
Share on other sites

Alright...I figured if I'm scp'ing the file over to the remote server...why not scp it back...

 

BUT...!!!! something tells me that this can't be the easiest way of doing things...

 

Here's what I need to it to do (either php or shell):

 

- delete all mp3's in a given directory

- use wget to download the "new" mp3's

- if wget failed (404 or any other reason) email an error report

- if wget downloaded the files successfully, chmod a remote directory (the remote server is a Mac, so the permissions change at will!), then scp them to the remote server (Mac, so wget doesn't work so well...or I'd run the script on that server)

- if the scp didn't work (permissions, internet connection or any other reason) email an error report

 

Here's what I have:

 


<?php
////delete all contents of a directory

function delete_directory_contents($local_path)
{
echo system('cp /backup.mp3 $local_path/backup.mp3');
$mydir1 = opendir($local_path);
while(false !== ($file1 = readdir($mydir1))) {
if($file1 != "." && $file1 != "..") {
chmod($dir1.$file1, 0777);
if(is_dir($dir1.$file1)) {
chdir('.');
destroy($dir1.$file1.'/');
rmdir($dir1.$file1) or DIE("couldn't delete $dir1$file1<br />");
}
else
unlink($dir1.$file1) or DIE("couldn't delete $dir1$file1<br />");
}
}
}

///get a single file from an ftp dirctory

function ftp_get_contents($ftp_server, $ftp_user_name, $ftp_user_pass, $local_file, $server_file){
$conn_id = ftp_connect($ftp_server);
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
if (ftp_get($conn_id, $local_file, $server_file, FTP_BINARY)) {
    echo "Successfully written to $local_file\n";
} else {
    echo "There was a problem\n";
}
ftp_close($conn_id);
}

//////email if something goes wrong

function mailto($show, $reason) 
{
$today = date('l, F j, Y');
$time = date('g:i:s a');
$message = "There was a problem with the download of the $show for $today at $time.\n\nThe $show $reason. \n\n Please check the server.";
$headers = 'From: me@domain.com' . "\r\n" . 'Reply-To: me@domain.com' . "\r\n" . 'X-Mailer: PHP/' . phpversion();
mail('user1@domain.com, user2@domain.com', 'Automatic Downloads Problem', $message, $headers);
}

/////check to see if local file exists...meaning wget was successful 

function fileExists($filename)
{
if (file_exists($filename)) 
{
echo "The file $filename exists\n";
} else {
//echo "The file $filename does not exist\n";
mailto(constant("SHOW"), constant("REASON"));
}
}



////send the recently downloaded file to the remote server

function scp_files($local_file, $username, $hostname, $remote_file)
{
$connection = ssh2_connect('192.168.2.245', 22, array('hostkey'=>'ssh-rsa'));

if (ssh2_auth_pubkey_file($connection, 'root',
                          '/root/.ssh/id_rsa.pub',
                          '/root/.ssh/id_rsa', '')) 
{
echo "Public Key Authentication Successful\n";
ssh2_scp_send($connection, '/var/www/weather/Current.mp3', '/directory/Current.mp3', 0777);

} else {
  die('Public Key Authentication Failed\n');
}
if ($ssh2_scp_send) { echo "remote_file transfer complete\n"; }
}



function check_remote_files($local_file, $username, $hostname, $remote_file)
{
$connection = ssh2_connect('192.168.2.245', 22, array('hostkey'=>'ssh-rsa'));

if (ssh2_auth_pubkey_file($connection, 'root',
                          '/root/.ssh/id_rsa.pub',
                          '/root/.ssh/id_rsa', '')) 
{
echo "Public Key Authentication For Server 2 Was Successful\n";
}
else 
{
die('Public Key Authentication For Server 2 Failed\n');
mailto(SHOW, SECONDREASON);
}

$find  = "\"";
$replace = "";


$newphrase = str_replace($find, $replace, $remote_file);
echo "$newphrase\n";

if(ssh2_scp_recv($connection, $newphrase, $local_file)){
echo "File ".$newphrase." was copied to $local_file\n"; 
}
else
{
echo "we had a problem here\n";
}			
}



define("SHOW","Weather Currents");
define("REASON","did not transfer properly");


chdir('/var/www/weather/');
delete_directory_contents('/var/www/weather/');
ftp_get_contents('xxx.ip.address','username','password','/var/www/weather/Current.mp3','Current.mp3');
fileExists('Current.mp3');
scp_files('/var/www/weather/Current.mp3', 'root', '192.168.2.245', '"/Volumes/Big Disk/directory/Current.mp3"');
check_remote_files('/Current.mp3', 'root', '192.168.2.245', '"/Volumes/Big Disk/directory/Current.mp3"');
?>

 

I don't have the chmod done...and the ftp download function is only for ONE file...not ALL mp3's in the directory...which I need to do.

 

Is there a better way to do this?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.