Solved

Php Question

Posted on 2010-09-11
9
319 Views
Last Modified: 2012-08-13
Hello,
I am trying to create a txt file (executable) from the php.. Its working fine but seems like some compile error... can anyone help?

The test file is the final output I am needing...

Line 30 to 63 in the php code is responsible to write this script output but having an error.
sftp-sendFiles.php
Test
0
Comment
Question by:Aanvik
  • 5
  • 4
9 Comments
 
LVL 82

Expert Comment

by:Dave Baldwin
ID: 33653893
I believe most of your slash-n are being converted to linefeeds by PHP.  You have to escape the '\' you want to exist in your text like "\\" so it would be like "\\n" which you did on a couple of lines.  But not all.
0
 
LVL 5

Author Comment

by:Aanvik
ID: 33653904
can you pls correct it?
0
 
LVL 82

Expert Comment

by:Dave Baldwin
ID: 33653947
Try this.  I think I got them all.
<?php
require("/var/www/html/include/mailfunction.php");
include("/var/www/html/dbIncl.php");

$email = "aanvik@yahoo.com";
$email_subject = "SFTP Log";

$samba_rdy = "/var/www/html/HTInc/samba/Results/rdy/";
$samba_rdy_arch = "/var/www/html/HTInc/samba/Results/rdy/Archive/";

$batch_dir = "/var/www/html/HTInc/batch/";

$sftp_user = 'fts';
$sftp_password = 'fts';
$sftp_dst = '/home/fts/';

// check folder on new files
$arr_host = array();
if ( is_dir($samba_rdy) && ( $dh = opendir($samba_rdy) ) ) {
	while ( false !== ( $filename = readdir($dh) ) ) {
		if ( $filename == "." || $filename == ".." || substr($filename, -3, 3) != 'csv' ) continue;

		$arr_host[substr($filename, 15, 6)][] = $filename;
	}

	closedir($dh);
}
if ( count($arr_host) == 0 ) exit;

// create expect-script
$fh_main = fopen($batch_dir.'main', 'w+');
fwrite($fh_main, "#!/usr/bin/expect\n");
fwrite($fh_main, "set timeout 50\n");
$arr_copy = array();

	fwrite($fh_main, "spawn sftp ".$sftp_user.'@'.$host."\\n");
	fwrite($fh_main, "expect \\n");
	fwrite($fh_main, "expect {");
	fwrite($fh_main, "expect \"".$sftp_user.'@'.$host."'s password: \"  {send \"fts\n\"}");
    	fwrite($fh_main, "\"sftp> \" {}");
    	fwrite($fh_main, "-re Connecting {}");
    	fwrite($fh_main, "\n send \\n");
    	fwrite($fh_main, "}");

foreach ( $arr_host as $host => $files ) {

	foreach ( $files as $file ) {
		fwrite($fh_main, "expect \"sftp> \"\n");
		fwrite($fh_main, "send \"put ".$samba_rdy.$file." ".$sftp_dst."\\n\";\n");

		$arr_copy[] = $file;
	}

	fwrite($fh_main, "expect \"sftp> \"\n");
	fwrite($fh_main, "send \"ls -1 ".$sftp_dst."\\n\";\n");
	fwrite($fh_main, "expect \"sftp> \"\n");
	fwrite($fh_main, "send \"bye\\n\";\n");
	fwrite($fh_main, "expect eof");
}

fclose($fh_main);
chmod($batch_dir.'main', 0755);

// run sftp
$main_stdout = array();
exec($batch_dir.'main', $main_stdout);

var_dump($main_stdout);
// check files copied on remote host
$stdout_size = count($main_stdout);
for ( $i = 0; $i < $stdout_size; $i++ ) {
	if ( substr($main_stdout[$i], 0, 11) == 'sftp> ls -1' ) {
		unset($main_stdout[$i]);
		break;
	} else {
		unset($main_stdout[$i]);
	}
}

$arr_arch = array();
$str_offset = strlen($sftp_dst);
foreach ( $main_stdout as $line ) {
	if ( substr($line, 0, 5) == 'sftp>' ) break;
	if ( ( $key = array_search(substr($line, $str_offset), $arr_copy) ) !== FALSE ) {
		$arr_arch[] = $arr_copy[$key];
		unset($arr_copy[$key]);
	}
}

// copy files in achive folder
foreach ( $arr_arch as $file ) {
	copy($samba_rdy.$file, $samba_rdy_arch.$file);
	unlink($samba_rdy.$file);

	//doquery($dblink, "DELETE FROM sftp_log WHERE filename='$file'");
}

if ( count($arr_copy) == 0 ) exit;

// write logs about not copied files
$arr_files = array();

$res = doquery($dblink, "SELECT filename, count(*) FROM sftp_log GROUP BY filename");
while ( ( $log_rec = dofetch($res) ) ) {
	if ( ( $key = array_search($log_rec[0], $arr_copy) ) !== FALSE && ( (int) $log_rec[1] ) == 4 ) {
		$arr_files[] = $log_rec[0];
		unset($arr_copy[$key]);
	}
}

//foreach ( $arr_copy as $file ) doquery($dblink, "INSERT INTO sftp_log (filename) VALUES ('$file')");

// send message on email
if ( $email != '' && count($arr_files) > 0 ) sendEmail($email_subject, 'Did not sent files: '.implode(", ", $arr_files), $email);
?>

Open in new window

0
 
LVL 5

Author Comment

by:Aanvik
ID: 33653977
Sorry.... That didn;t helped.
Error.JPG
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 82

Expert Comment

by:Dave Baldwin
ID: 33654025
Try changing line 42 to this.
fwrite($fh_main, "\\n send \\n");

Open in new window

0
 
LVL 5

Author Comment

by:Aanvik
ID: 33654044
same error
0
 
LVL 82

Accepted Solution

by:
Dave Baldwin earned 500 total points
ID: 33654196
Ok, this test file generates the same text as your 'Test' file above.  I can't test the whole file since I don't have all the rest of the resources.  But you should be able to First: test this file and see that it generates the right text and Second: use it to fix that part of the main file.
<?php
$samba_rdy = "/var/www/html/HTInc/samba/Results/rdy/";
$samba_rdy_arch = "/var/www/html/HTInc/samba/Results/rdy/Archive/";

$sftp_user = 'fts';
$sftp_password = 'fts';
$sftp_dst = '/home/fts/';
$host = 'jlc031';
$file = 'AQR01jlc_00031_jlc031_0138_DELETE-DELETE.csv';

// create expect-script
$fh_main = fopen('sftp-test2.txt', 'w+');
fwrite($fh_main, "#!/usr/bin/expect\n");
fwrite($fh_main, "set timeout 5\n");
fwrite($fh_main, "spawn sftp ".$sftp_user.'@'.$host."\n");
fwrite($fh_main, "expect \\n\n");
fwrite($fh_main, "expect {\n");
fwrite($fh_main, "\"".$sftp_user.'@'.$host."'s password: \" {send \"fts\\n\"}\n");
fwrite($fh_main, "\"sftp> \" {}\n");
fwrite($fh_main, "-re Connecting {}\n");
fwrite($fh_main, "\\n {send \"No idea\\n\"}\n");
fwrite($fh_main, "}\n\n");

fwrite($fh_main, "expect \"sftp> \"\n");
fwrite($fh_main, "send \"put ".$samba_rdy.$file." ".$sftp_dst."\\n\"\n");


	//fwrite($fh_main, "expect \"sftp> \"\n");
	//fwrite($fh_main, "send \"ls -1 ".$sftp_dst."\\n\";\n");
	//fwrite($fh_main, "expect \"sftp> \"\n");
	fwrite($fh_main, "send \"bye\\n\"\n");
	fwrite($fh_main, "expect eof\n");

fclose($fh_main);

?>

Open in new window

0
 
LVL 5

Author Closing Comment

by:Aanvik
ID: 33654474
Thank You
0
 
LVL 82

Expert Comment

by:Dave Baldwin
ID: 33654486
You're welcome.
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now