bschultz Posted April 28, 2011 Share Posted April 28, 2011 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. Quote Link to comment Share on other sites More sharing options...
btherl Posted April 28, 2011 Share Posted April 28, 2011 What happens if you do a simple 1 line command like "id"? Quote Link to comment Share on other sites More sharing options...
bschultz Posted April 28, 2011 Author Share Posted April 28, 2011 I don't know what you mean... Quote Link to comment Share on other sites More sharing options...
btherl Posted April 28, 2011 Share Posted April 28, 2011 Like this: $url = "id"; $stream=ssh2_exec($connection,$url); stream_set_blocking( $stream, true ); $cmd=fread($stream,4096); fclose($stream); print "Output from server: $cmd\n"; Quote Link to comment Share on other sites More sharing options...
bschultz Posted April 28, 2011 Author Share Posted April 28, 2011 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? Quote Link to comment Share on other sites More sharing options...
btherl Posted April 28, 2011 Share Posted April 28, 2011 Do you have a fixed list of files to process, or do you need to process all files found in a specific location? Quote Link to comment Share on other sites More sharing options...
bschultz Posted April 28, 2011 Author Share Posted April 28, 2011 both...I tried to glob for all mp3's...but it didn't work. Before I figured it out, I figured I better ask to see if there's a better way. Quote Link to comment 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.