SFTP Connection closed prematurely - phpseclib

Here's the result of a SFTP connection :
set_include_path(get_include_path() . PATH_SEPARATOR . 'phpseclib');
include('Net/SFTP.php');
define('NET_SFTP_LOGGING', NET_SFTP_LOG_COMPLEX);
$sftp = new Net_SFTP(
SERVER,
FTP_PORT
);
if (!$sftp->login(FTP_LOGIN, FTP_PASSWORD))
{
echo 'Login failed.'.'<br>';
echo $sftp->getLog().'<br>';
echo $sftp->getSFTPLog();
}
else
{
echo 'Login successful.'.'<br>';
echo $sftp->pwd();
}
Notice: Connection closed prematurely in /home/www/default/sftp/phpseclib/Net/SSH2.php on line 2992
Login failed.
-> NET_SFTP_INIT (0.048s) 00000000 00:00:00:03 ....
The connection is working with FileZilla
Any idea?

Related

Edit a remote file using SSH in Perl

Below is the code snippet I have written.
This tries to fetch the server name and file name through the arguments
I am stuck at editing the remote file after the server name and corresponding file name are fetched.
Command run: server.pl sox3d1 TEST50
File server.pl:
use strict;
use warnings;
use Data::Dumper;
use XML::Simple;
my $host=$ARGV[0];
my $db_host=$ARGV[1];
my %servers;
$servers{"ser14316.local.net"}=["sox3d1","sox3d2"] ;
$servers{"ser143hn.local.net"}=["sox4d1","sox4d2"] ;
$servers{"ser14441.local.net"}=["sox6d1","sox6d2"] ;
$servers{"ser18163.local.net"}=["soxuat61","soxuat62","soxuat63"] ;
$servers{"ser1444r.local.net"}=["soxuat51","soxuat52","soxuat53"] ;
my $files=
{
db=>"/appl/$host/db_info.ref",
};
my %groups_by_host;
for my $group (keys(%servers)) {
for my $host (#{ $servers{$group} }) {
push #{ $groups_by_host{$host} }, $group;
}
}
my $server = $groups_by_host{$host}; # we get the server name via variable $host
my ( $ssh_to_server )=#$server; # This is the server to ssh
my $dest_file=$files->{"db"} ; #This will fetch the file from the remote server
print "$ssh_to_server","->","$dest_file \n";
The file in this server has to be edited and below are the steps to do that via ssh.
Search for the string $host (i.e sox3d3) in the remote file $dest_file by doing ssh to server $ssh_to_server
Replace the string TEST30 with TEST50
The file is a semicolon-separated file
File content in remote server (db_info.ref):
sox3d3 ;/appl/sox3d3/current ;TEST30 ;TEST30 ;USER_10 ;USER_30
sox4d4 ;/appl/sox4d4/current ;TEST40 ;TEST40 ;USER_20 ;USER_40
So, the 3rd and 4th columns (TEST30;TEST30) should be replaced with TEST50 as per input.
Have tried the below code to get it working
as i said i was trying for a one line and had many variables in the command , the below worked actually
Update 1:
This basically ssh's the server and changes the file accordingly
system(
'ssh' => ('-q',$ser),
'sed' => ('-i -E', qq('/$host/s#([^;]+;[^;]+;)[^;]+;[^;]+#\\1$db_host
;$db_host #') , $dest_file),
Tried something like this?
my $destination = sprintf( '%s#%s', $user, $host );
my $file = '/path/to/some/file.txt';
system(
'ssh' => $destination,
'sed' => ( '-i', 's/TEST30/TEST50/g', $file ),
);
The ssh documentation shows you can pass a command after the ssh destination. When you do this, ssh will log into the server, run that command, then log out again, instead of dropping you into the shell on the server.
So you can use that to run sed to edit the file.

Amazon SES throwing error 500 when I try to send email

I've set up the Amazon SES on my ec2 instance as in this link
The test file is working fine and sending the email when I use the command php teste.php on the terminal on my ec2 instance, the problem is when I try to make a post request to access this file and send the email, it's throwing an error 500 and I don't know how to fix it.
here is the error message on chrome:
and on Insomnia:
here is my test file:
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
// location of your Composer autoload.php file.
require '../../../home/ec2-user/vendor/autoload.php';
$sender = 'email#email.com';
$senderName = 'Teste';
$recipient = 'email#email.com';
$usernameSmtp = '[removed for security]';
$passwordSmtp = '[removed for security]';
$host = 'email-smtp.sa-east-1.amazonaws.com';
$port = 587;
$subject = 'Amazon SES test (SMTP interface accessed using PHP)';
$bodyText = "Email Test\r\nThis email was sent through the
Amazon SES SMTP interface using the PHPMailer class.";
$bodyHtml = '<h1>Email Test</h1>
<p>This email was sent through the
Amazon SES SMTP
interface using the <a href="https://github.com/PHPMailer/PHPMailer">
PHPMailer</a> class.</p>';
$mail = new PHPMailer(true);
try {
$mail->isSMTP();
$mail->setFrom($sender, $senderName);
$mail->Username = $usernameSmtp;
$mail->Password = $passwordSmtp;
$mail->Host = $host;
$mail->Port = $port;
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'tls';
$mail->addCustomHeader('X-SES-CONFIGURATION-SET');
$mail->addAddress($recipient);
$mail->isHTML(true);
$mail->Subject = $subject;
$mail->Body = $bodyHtml;
$mail->AltBody = $bodyText;
$mail->Send();
echo "Email sent!", PHP_EOL;
} catch (phpmailerException $e) {
echo "An error occurred. {$e->errorMessage()}", PHP_EOL;
} catch (Exception $e) {
echo "Email not sent. {$mail->ErrorInfo}", PHP_EOL;
}
You need to open port 587 on your instance's security group to make it work.
A similar question asked on aws developer form
EDIT
using SSL
$mail->Host = 'ssl://email-smtp.sa-east-1.amazonaws.com';
$mail->SMTPSecure = 'tls';
$mail->Port = 443;

Unable to Connect Cloud SQL using PHP Google App Engine

I just deploy the following app. And i receive the error. Please help me to solve this issue.
$instance_name = "mysql:unix_socket=/cloudsql/My_Project_ID:google-cloud-instance";
$c = new mysqli(null, "root", "My_Password", 'My_DB_Name', 0, $instance_name);
if ($c->connect_error) {
echo $c->connect_error;
$c = null;
} else {
echo "Connected ";
}
And I receive the error in browser like
Unable to find the socket transport "unix" - did you forget to enable it when you configured PHP?
The socket argument doesn't say anything about a prefix:
$socket_path = "/cloudsql/My_Project_ID:google-cloud-instance";
$c = new mysqli(null, "my-user", "my-password", 'my-db', $socket_path);

Read a file untill a string in matched

My input file contains:
Startoffile
Host status
Server1
Server2
Server3
Pending Device
Device1
Device2
..
Devic100
Endoffile
How to write a simple PowerShell script which will read the files until the line "Pending Device"?
without knowing much about your desired action, try this:
$file = Get-Content "C:\Users\user\Downloads\file.txt"
foreach ($line in $file) {
if ($line -match "Pending Device") {
Write-Host "Reached 'Pending Device'" -ForegroundColor Red
}
else {
Write-Host "No 'Pending Device' yet"
}
}

[file-get-contents]: failed to open stream

I have the correct access_token, but when I call file_get_contents, I receive the following error:
[function.file-get-contents]: failed to open stream: HTTP request failed!
HTTP/1.1 400 Bad Request in..
Pasting the echo $fql_query_url in the browser gives the correct result?
Is it an encoding problem? I ask because other fql queries work fine. Here's the offending code:
<?php
require_once('facebook.php');
//GET access token
echo $uid = $facebook->getUser();
$access_token = $facebook->getAccessToken();
echo '<br>';
// Run fql query
$fql_query_url = 'https://graph.facebook.com/'
. '/fql?q=SELECT page_id from page_admin WHERE uid='.$uid.''
. '&access_token=' . $access_token;
$fql_query_result = file_get_contents($fql_query_url);
$fql_query_obj = json_decode($fql_query_result, true);
//display results of fql query
echo '<br><pre>';
print_r("query results:");
print_r($fql_query_obj);
echo '</pre>';