Jump to content

Remote File Size Over SSH


bschultz

Recommended Posts

I need to check a file's size with php and ssh.  Everything I've seen online for remote files uses curl and port 80.  I'll be checking a LAN computer that isn't running a webserver, but is running an SSH server.

 

How can I do this?

 

I've tried this:

 

<?php
$tomorrow = date ('n-j-y');
echo system(ssh root@192.168.2.169 ls -lah "/MacRadio X/WMIS Logs/WMIS $tomorrow Log" | awk {print $5});
?>

 

but it didn't work.  I got a T_VARIAABLE error.  This was the same command I used right from the command line, and it worked.

 

I'll also need to add a check to the file size, and if it's less than 175K, send me an email...but I'll work on that after I have the first part working.

 

Thanks!

 

 

Link to comment
Share on other sites

Thanks for the help...but the curl version won't work since a webserver or ftp server isn't involved...just a local filesystem that DOES have SSH.

 

The local machine I'm checking on is a Mac...stat functions a bit differently on a Mac...and --format=%s failed.

 

So, here's what I used...

 

<?php

$nextday = mktime(0, 0, 0, date("n"), date("j")+1, date("y"));

$tomorrow = date("n-j-y", $nextday);

echo system("ssh root@192.168.2.169 ls -lah '/MacRadio\ X/WMIS\ Logs/WMIS\ $tomorrow\ Log' | awk '{ print $5}'");

?>

 

This is working.  Thanks Abra for helping me with the location of the single and double quotes to make this work!

 

Now, how can I add the email functionality to this?  I'm running the command over ssh on the remote machine...but I need to store the filesize in a variable, and if it's below 175K, send an email from the LINUX machine that initiates the ssh session.

 

Something along these lines...which doesn't work!

 

<?php

$message = "The file size is too small...make sure every line of the log is included";

$nextday = mktime(0, 0, 0, date("n"), date("j")+1, date("y"));

$tomorrow = date("n-j-y", $nextday);

echo system("ssh root@192.168.2.169 ls -lah '/MacRadio\ X/WMIS\ Logs/WMIS\ $tomorrow\ Log' | awk '{ print $5}'");

if ($5 <= '175K') { mail ('address@example.com', 'My Subject', $message)}  //this line needs to be executed on the LINUX machine that inititated the ssh session...NOT on the Mac

?>

 

 

Thanks again!

Link to comment
Share on other sites

Thanks Adam,

 

The $5 variable is for awk...I was trying to use that to check.

 

This is putting the , in the name of the file...

 

<?php

$message = "The file size is too small...make sure every line of the log is included";

$nextday = mktime(0, 0, 0, date("n"), date("j"), date("y"));

$tomorrow = date("n-j-y", $nextday);

$today = date("n-j-y");

//echo system("ssh root@192.168.2.169 ls -lah '/MacRadio\ X/WMIS\ Logs/WMIS\ $tomorrow\ Log' | awk '{ print $5}'");

echo system("ssh root@192.168.2.169 ls -lah '/MacRadio\ X/WMIS\ Logs/WMIS\ $today\ Log', $retval");

echo $retval;

if ($retval <= '175K') { mail ('bschultz@kkbj.com', 'My Subject', $message);}  

?>

 

How can I get rid of comma from the name?

Link to comment
Share on other sites

You don't want the return value because it indicates success or failure.  You want the last line of the output:

 

$file_size = system("ssh root@192.168.2.169 ls -lah '/MacRadio\ X/WMIS\ Logs/WMIS\ $tomorrow\ Log' | awk '{ print $5}'");
echo $filesize;

But then you have to work out if it is 175 or 175K etc...  I would just look up stat() on OSX and use that.

Link to comment
Share on other sites

Thanks again for all the help!  We're getting close.

 

<?php

$message = "The file size is too small...make sure every line of the log is included";
$nextday = mktime(0, 0, 0, date("n"), date("j"), date("y"));
$tomorrow = date("n-j-y", $nextday);
$today = date("n-j-y");

//$file_size = system("ssh root@192.168.2.169 ls -lah '/MacRadio\ X/WMIS\ Logs/WMIS\ $tomorrow\ Log' | awk '{ print $5}'");
//echo $filesize;

$file_size = system("ssh root@192.168.2.169 stat -f %z '/MacRadio\ X/WMIS\ Logs/WMIS\ $tomorrow\ Log'");
   $size = $filesize;
     echo $size;  //this is echoing 309600, but is still sending the email

if ($size <= "175000") 
  { 
    mail ('address@domain.com', 'My Subject', $message); 
     echo "email sent"; 
  } 
   else 

  { 
   echo "file is large enough"; 
  }  

?>

 

The email part isn't working yet...as you can see, the file size is larger than the if statement, and it's still sending the email.  Any idea why?

 

thanks again for all the help!

Link to comment
Share on other sites

I figured it out...thanks again for all the help!

 

<?php

$message = "The file size is too small...make sure every line of the log is included";
$nextday = mktime(0, 0, 0, date("n"), date("j")+1, date("y"));
$tomorrow = date("n-j-y", $nextday);
$today = date("n-j-y");

$cmd = "ssh root@192.168.2.169 stat -f %z '/MacRadio\ X/WMIS\ Logs/WMIS\ $tomorrow\ Log'";

$file_size = exec("$cmd", $results);
$comma_separated = implode(",", $results);

if ($comma_separated <= "175000") 

  { 
    mail ('address@domain.com', 'My Subject', $message); 
     echo "email sent"; 
  } 
   else 

  { 
   echo "file is large enough"; 
  }  

?>

 

 

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.