Use Putty’s PSCP Command Line Tool to Copy Files from Linux Cloud VM to Windows PC

I’m want to look at my cloud hosted Linux Apache web server log files on my local Windows PC and I’m using PuTTY SSH as my Windows SSH telnet client.

How do we transfer the Apache web server log files over to my local Windows PC so I can work with it?

Instead of trying to copy the file from inside the PuTTY SSH session on the remote server, we can use a built-In Windows command line tool (pscp.exe) included with PuTTY called “PSCP” which mimic much of what you get with Linux’s built-in scp file transfer utility.

PuTTY

PuTTY is a free and open-source terminal emulator, serial console, and network file transfer application. It supports various network protocols, including SCP, SSH, Telnet, rlogin, and raw socket connection. It can connect to a remote server or computer over these protocols to securely execute commands at the terminal over the network, manage files, or tunnel other applications. PuTTY is most commonly used on Windows, but versions for Linux and macOS are also available.

PuTTY was originally written for Microsoft Windows but now has versions for other operating systems. It is widely used by network administrators and other users who need to remotely manage systems and devices or transfer files securely.

PSCP

PSCP stands for PuTTY Secure Copy Client, a command-line tool that allows you to securely transfer files between computers using the SCP protocol. PSCP is part of the PuTTY suite of tools and is used for non-interactive file transfers from the command line. It is compatible with both Windows and Unix-like operating systems.

With PSCP, you can upload files from your local machine to a remote server or download files from a remote server to your local machine using a secure connection. It’s particularly useful for automating file transfers in scripts or for environments where a graphical user interface is not available.

Here are some basic examples of how PSCP is used:

  • To copy a file from your local machine to a remote server:rubyCopy codepscp localfile.txt user@remotehost:/remote/directory
  • To copy a file from a remote server to your local machine:rubyCopy codepscp user@remotehost:/remote/file.txt localdirectory

In these commands, user represents the username on the remote host, remotehost is the address of the remote server, and localdirectory is the directory on your local machine where you want to copy the files.

Remember, when using PSCP, you’ll be prompted for passwords or you can use SSH keys for authentication, depending on your server’s configuration and your setup.

My Personal Experience with Putty & PSCP

Putty’s Command Line File Transfer Tool (PSCP)

Below is a screenshot of me figuring out how to properly transfer an Apache web server log file over to my Windows PC temp folder on the C drive.

Sample Command Line Code

Run from the Windows Command Line from your Putty Install folder.

I used // to escape the forward slashes on the Linux server and \\ to escape backslahes and it worked.

Below should be entered all on one line.
c:\Program Files\PuTTY>pscp user@remoteserver://var//log//apache2//access.log c:\\temp\\access.log

Summary

You can use Putty’s PSCP command line tool to copy files from your Linux host to your Windows PC with these basic command line commands.

I hope this article helped you solve your problem!

~Cyber Abyss

Sending Email with PHP

Configuring the mail() function in PHP to send emails involves setting up the underlying mail sending system on the server. The configuration depends on the operating system of your web server and the mail transfer agent (MTA) software that is being used (e.g., Sendmail, Postfix, Exim).

Configuring Email in PHP

Configuring the mail() function in PHP to send emails involves setting up the underlying mail sending system on the server. The configuration depends on the operating system of your web server and the mail transfer agent (MTA) software that is being used (e.g., Sendmail, Postfix, Exim).

For Linux/Unix Servers

On most Linux/Unix servers, mail() is configured to use Sendmail or a Sendmail-compatible MTA by default. Here’s a general approach to configure it:

  1. Install Sendmail or another MTA: Ensure that Sendmail, Postfix, or another MTA is installed on your server. For example, to install Sendmail on a Debian-based system, you can use:bashCopy codesudo apt-get install sendmail
  2. Configure PHP to Use Sendmail: The default configuration in php.ini usually works out of the box with Sendmail. However, you can check or modify the path to Sendmail in your php.ini file:iniCopy code[mail function] ; For Sendmail and Sendmail-like systems sendmail_path = "/usr/sbin/sendmail -t -i" Make sure the path to sendmail matches the location of the Sendmail executable on your system.
  3. Configure Sendmail or MTA Settings: Depending on your server setup, you might need to configure your MTA to handle emails correctly. This could involve setting up relay options, modifying the mail queue, or adjusting security settings. This is highly specific to your server and network setup.
  4. Restart Apache or PHP-FPM: After making changes to php.ini, make sure to restart your web server or the PHP-FPM service to apply the changes.bashCopy codesudo systemctl restart apache2 Or for PHP-FPM:bashCopy codesudo systemctl restart php7.x-fpm (Replace 7.x with your specific PHP version.)

For Windows Servers

On Windows servers, mail() can be configured to use an SMTP server directly through the php.ini file:

  1. Configure SMTP Settings in php.ini: Locate your php.ini file and specify the SMTP server and related settings:iniCopy code[mail function] ; Setup for Windows systems SMTP = smtp.example.com smtp_port = 25 sendmail_from = user@example.com Replace smtp.example.com with your SMTP server’s hostname, 25 with your SMTP server’s port (often 587 for TLS or 465 for SSL), and user@example.com with the email address you want to send emails from.
  2. Restart Your Web Server: Apply the changes by restarting your web server software.

Note

  • Security: Sending emails from web applications exposes them to spam and abuse risks. Ensure you have mechanisms in place to prevent abuse (e.g., CAPTCHAs, rate limiting).
  • SPF, DKIM, and DMARC: To improve email deliverability, consider setting up SPF, DKIM, and DMARC records in your domain’s DNS. These help verify that your emails are legitimately coming from your domain and are not being spoofed.
  • Using External SMTP: For better reliability and deliverability, consider using an external SMTP service like Amazon SES, SendGrid, or Mailgun. These services often require authentication, and you’ll likely need to use a library like PHPMailer or SwiftMailer to integrate them into your PHP applications.

Configuring mail() requires access to server settings and might involve complex configurations depending on your hosting environment. If you’re unsure, consult with your hosting provider or a system administrator.

Sending an Email in PHP

Sending an email in PHP can be achieved using the mail() function. This function allows you to send emails directly from a PHP script. Below is a simple example that demonstrates how to use the mail() function to send an email:

phpCopy code<?php
// The recipient email address
$to = 'recipient@example.com';

// Subject of the email
$subject = 'Test Email from PHP';

// Message content for the email
$message = 'Hello, this is a test email sent from a PHP script.';

// Headers for the email
$headers = 'From: YourName <yourname@example.com>' . "\r\n" . // Sender's email address
           'Reply-To: yourname@example.com' . "\r\n" . // Reply-to address
           'X-Mailer: PHP/' . phpversion();

// Sending the email
if(mail($to, $subject, $message, $headers)) {
    echo "Email sent successfully!";
} else {
    echo "Email sending failed.";
}
?>

Here’s what each part does:

  1. $to: Specifies the recipient’s email address.
  2. $subject: The subject line of the email.
  3. $message: The body content of the email.
  4. $headers: Email headers to specify additional information like the sender’s email address, reply-to address, and the X-Mailer (which indicates the software used to send the email, in this case, PHP).

To use this script:

  • Replace 'recipient@example.com' with the actual recipient’s email address.
  • Change 'YourName <yourname@example.com>' in the $headers variable to your name and email address. This will be used as the “From” address.
  • You might need to adjust settings or configurations depending on your server or hosting provider to properly send emails.

Keep in mind, for bulk or more complex email sending tasks (like HTML emails, attachments, or using SMTP authentication), it’s recommended to use a library such as PHPMailer or SwiftMailer, as they offer more flexibility and functionality than the basic mail() function.

Sending Email with Attachments in PHP

Sending an email with an attachment in PHP requires a bit more work than a simple text email because you have to format the email to conform to MIME standards. This involves encoding the attachment in base64 and splitting the email body from the attachment using a unique boundary string.

Below is a basic example of how to send an email with an attachment using PHP:

phpCopy code<?php
// Recipient email address
$to = 'recipient@example.com';

// Subject of the email
$subject = 'Email with Attachment from PHP';

// File to attach
$file_path = '/path/to/your/attachment.pdf';
$file_name = basename($file_path);
$file_size = filesize($file_path);
$file_content = chunk_split(base64_encode(file_get_contents($file_path))); // encode the attachment

// Boundary 
$boundary = md5(time());

// Headers
$headers = "From: YourName <yourname@example.com>\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: multipart/mixed; boundary=\"{$boundary}\"";

// Message Body
$body = "--{$boundary}\r\n";
$body .= "Content-Type: text/plain; charset=ISO-8859-1\r\n";
$body .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$body .= "This is a MIME encoded message.\r\n\r\n"; // Message before attachment

// Add attachment
$body .= "--{$boundary}\r\n";
$body .= "Content-Type: application/octet-stream; name=\"{$file_name}\"\r\n";
$body .= "Content-Disposition: attachment; filename=\"{$file_name}\"\r\n";
$body .= "Content-Transfer-Encoding: base64\r\n\r\n";
$body .= $file_content . "\r\n\r\n";
$body .= "--{$boundary}--";

// Send email
if(mail($to, $subject, $body, $headers)) {
    echo "Email sent with attachment successfully.";
} else {
    echo "Email sending failed.";
}
?>

Steps Explained:

  1. Specify the Recipient, Subject, and File: Set the $to, $subject, and the path to the attachment with $file_path.
  2. Read and Encode the Attachment: The file’s contents are read, encoded in base64, and then chunk split to ensure it adheres to email standards.
  3. Create MIME Headers and Body: The email is composed using MIME standards with a unique boundary to separate different parts of the email. The headers include Content-Type set to multipart/mixed to accommodate both the text and the attachment.
  4. Constructing the Email: The email body contains both the text message and the attachment, separated by the boundary. Each part has appropriate headers to describe its content type, encoding, and disposition.
  5. Sending the Email: The mail() function is used with the recipient, subject, composed body, and headers.

Notes:

  • Make sure to replace /path/to/your/attachment.pdf with the actual path to your attachment file.
  • Update the From email address in the headers to your email.
  • This is a basic example. For more complex scenarios, like multiple attachments or different content types, it’s advisable to use a library like PHPMailer, as it greatly simplifies the process of sending emails with attachments, HTML content, and more.

I hope this article helps you figure out how to send email using PHP.

~Cyber Abyss