You are on page 1of 3

Tata

Motors
Financ
e Perl script that uses the Net::FTP module to upload a file to an FTP server with
Official credentials:

use strict;

use warnings;

use Net::FTP;

my $ftp_host = 'ftp.example.com';

my $ftp_user = 'username';

my $ftp_pass = 'password';

my $file_path = '/path/to/local/file.txt';

my $remote_dir = '/path/to/remote/directory';

my $remote_file = 'uploaded_file.txt';

# Connect to the FTP server

my $ftp = Net::FTP->new($ftp_host, Passive => 1) or die "Could not connect to FTP server: $!";

$ftp->login($ftp_user, $ftp_pass) or die "FTP login failed: ", $ftp->message;

# Change to the desired remote directory

$ftp->cwd($remote_dir) or die "Failed to change remote directory: ", $ftp->message;

# Upload the file

$ftp->put($file_path, $remote_file) or die "Failed to upload file: ", $ftp->message;

# Close the FTP connection

$ftp->quit;

Make sure you have the Net::FTP module installed before running the script. You can install
it using the following command:

cpan Net::FTP

Tata
Motors
Financ
e
Tata
Motors
Financ
e Replace 'ftp.example.com' with the hostname or IP address of your FTP server. Set
Official $ftp_user and $ftp_pass to your FTP credentials. Adjust $file_path to the path of the local
file you want to upload. Set $remote_dir to the path of the remote directory where you
want to upload the file, and $remote_file to the desired name of the uploaded file on
the remote server.

When you run the script, it will connect to the FTP server, authenticate using the
provided credentials, change to the specified remote directory, and upload the local
file to the server with the desired filename. If any errors occur during the process, it
will display an appropriate error message.

Shell script to upload file in FTP with credentials

#!/bin/bash

FTP_HOST="ftp.example.com"

FTP_USER="username"

FTP_PASS="password"

LOCAL_FILE="/path/to/local/file.txt"

REMOTE_DIR="/path/to/remote/directory"

REMOTE_FILE="uploaded_file.txt"

# Connect to the FTP server and upload the file

ftp -n $FTP_HOST <<EOF

user $FTP_USER $FTP_PASS

binary

cd $REMOTE_DIR

put $LOCAL_FILE $REMOTE_FILE

quit

EOF

Tata
Motors
Financ
e
Tata
Motors
Financ
e Replace 'ftp.example.com' with the hostname or IP address of your FTP server. Set
Official $FTP_USER and $FTP_PASS to your FTP credentials. Adjust $LOCAL_FILE to the path
of the local file you want to upload. Set $REMOTE_DIR to the path of the remote
directory where you want to upload the file, and $REMOTE_FILE to the desired name
of the uploaded file on the remote server.

Save the script in a file, e.g., upload_ftp.sh , and make it executable using the following
command:

chmod +x upload_ftp.sh

Then, run the script using the following command:

./upload_ftp.sh

The script will connect to the FTP server, authenticate using the provided credentials,
change to the specified remote directory, and upload the local file to the server with
the desired filename. The binary command ensures that the file transfer mode is set
to binary to handle non-text files correctly.

If any errors occur during the process, the ftp command will display error messages.

Tata
Motors
Financ
e

You might also like