Need to create a simple script for ftp login

1.8k views Asked by At

I am planning to make a simple script for logging on to a switch & take the configuration backup!

I used to login manually & want to do this in a single command or through a script!

Anyone could help me out?

root@xxx:/home# ftp xxx.19.254.61
Connected to xxx.19.254.61.
220 xx-xx-xx-xxx-SW-2 FTP version 1.0 ready at Sat Feb 14 23:28:25 1970
Name (xxx.19.254.61:root): admin
331 Enter PASS command
Password:
230 Logged in
Remote system type is UNIX.
Using binary mode to transfer files.
ftp> get config config.cfg
local: config.cfg remote: config
200 Port command okay
150 Opening data connection for RETR config
226 File sent OK
2824 bytes received in 1.05 secs (2.6227 kB/s)
ftp> bye
221 Goodbye!
2

There are 2 answers

11
auth private On

i wrote something similar here

ftp_server='localhost'
ftp_username='********'
ftp_password='***********'    
mkdir /home/user/linux/source_folder
source_folder="/home/user/linux/source_folder/"
cp -avr config* $source_folder
dest_folder="/home/user/linux/linux2/"
ftp -i -n $ftp_server <<END_SCRIPT
quote USER $ftp_username
quote PASS $ftp_password
cd $source_folder
!cd $dest_folder
mget -R *
quit
END_SCRIPT

(Note: you should mkdir because ftp not support multiple file copies from source to destination folder so you should use mget with -R option to multiple files which located in new created directory then copy your config files to this new created directory with cp -avr config* $source_folder).

this is another more enhanced script that no one can see your stored password, allowed to openssl encryption, decryption with pair keys .

#!/bin/bash
read -p "Enter Username: " Username
read -sp "Enter Username Password: " Password
echo
ftp_server="localhost"
ftp_username="$Username"
ftp_password="$Password"
passwrd=`echo $ftp_password | openssl aes-256-cbc -a -salt`
decrypt=`echo $passwrd | openssl aes-256-cbc -a -d -salt`
mkdir /home/username/linux/source_folder
source_folder="/home/username/source_folder/"
cp -avr config* $source_folder
dest_folder="/home/username/linux/dest_folder/"
ftp -in $ftp_server <<END_SCRIPT
quote USER $ftp_username
quote PASS $decrypt
cd $source_folder
!cd $dest_folder
mget -R *
quit
END_SCRIPT

instructions :

  • Enter Your Real Username
  • Enter Your Real Password
  • Encrypt Your private key
  • Verify Password private key
  • Last Login with the same private key

Example :

  • Sazzad (Username)
  • Sazzad (Real Password)
  • P@$$w0rd (password_to_encrypt (private key))

  • P@$$w0rd same password_to_encrypt (private key)

  • P@$$w0rd same password_to_decrypt (private key)

Note:

you can delete those lines if you don't want pair key encryption with openssl connection

passwrd=echo $ftp_password | openssl aes-256-cbc -a -salt

decrypt=echo $passwrd | openssl aes-256-cbc -a -d -salt

and change quote PASS $decrypt to quote PASS $ftp_password

Alternative: Just Put Your Username and Password and Server Ip Like 172.19.254.61

#!/bin/bash
read -p "Enter Username: " Username
read -sp "Enter Username Password: " Password
echo
read -p "FTP Server IP: " ftp_server
ftp_username=$Username
ftp_password=$Password
source_folder="/home/rr-de-nms-1/Sazzad/source_folder/"
cp -avr /home/rr-de-nms-1/Sazzad/config* $source_folder
dest_folder="/home/rr-de-nms-1/Sazzad/dest_folder/"
ftp -in $ftp_server <<END_SCRIPT
quote USER $ftp_username
quote PASS $decrypt
cd $source_folder
!cd $dest_folder
mget -R *
quit
END_SCRIPT

Now Your Config Files downloaded here in /home/rr-de-nms-1/Sazzad/dest_folder/

3
anishsane On

Is heredoc not good for you?

$ ftp xxx.19.254.61 <<< EOF
get config config.cfg
bye
EOF