bschultz Posted September 5, 2011 Share Posted September 5, 2011 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! Quote Link to comment https://forums.phpfreaks.com/topic/246498-remote-file-size-over-ssh/ Share on other sites More sharing options...
dreamwest Posted September 5, 2011 Share Posted September 5, 2011 curl -k https://site.com/file.swf Use curl_getinfo or just get the headers (Content-length) Quote Link to comment https://forums.phpfreaks.com/topic/246498-remote-file-size-over-ssh/#findComment-1265777 Share on other sites More sharing options...
AbraCadaver Posted September 5, 2011 Share Posted September 5, 2011 echo system("stat --format=%s '/MacRadio X/WMIS Logs/WMIS $tomorrow Log'"); Quote Link to comment https://forums.phpfreaks.com/topic/246498-remote-file-size-over-ssh/#findComment-1265778 Share on other sites More sharing options...
bschultz Posted September 6, 2011 Author Share Posted September 6, 2011 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! Quote Link to comment https://forums.phpfreaks.com/topic/246498-remote-file-size-over-ssh/#findComment-1265806 Share on other sites More sharing options...
Adam Posted September 6, 2011 Share Posted September 6, 2011 system accepts a second parameter to store the response from the server. Instead of echoing it, store the result and use that variable in your comparison. $5 is not a valid custom variable name by the way, as it starts with a number. Quote Link to comment https://forums.phpfreaks.com/topic/246498-remote-file-size-over-ssh/#findComment-1265890 Share on other sites More sharing options...
bschultz Posted September 6, 2011 Author Share Posted September 6, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/246498-remote-file-size-over-ssh/#findComment-1265961 Share on other sites More sharing options...
AbraCadaver Posted September 6, 2011 Share Posted September 6, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/246498-remote-file-size-over-ssh/#findComment-1266025 Share on other sites More sharing options...
AbraCadaver Posted September 6, 2011 Share Posted September 6, 2011 Might try: stat -f %z /path/to/file Quote Link to comment https://forums.phpfreaks.com/topic/246498-remote-file-size-over-ssh/#findComment-1266028 Share on other sites More sharing options...
bschultz Posted September 6, 2011 Author Share Posted September 6, 2011 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! Quote Link to comment https://forums.phpfreaks.com/topic/246498-remote-file-size-over-ssh/#findComment-1266037 Share on other sites More sharing options...
bschultz Posted September 7, 2011 Author Share Posted September 7, 2011 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"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/246498-remote-file-size-over-ssh/#findComment-1266279 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.