• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Geek Diary

CONCEPTS | BASICS | HOWTO

  • OS
    • Linux
    • CentOS/RHEL
    • Solaris
    • Oracle Linux
    • Linux Services
    • VCS
  • Database
    • oracle
    • oracle 12c
    • ASM
    • mysql
    • MariaDB
    • Data Guard
  • DevOps
    • Docker
    • Shell Scripting
  • Interview Questions
  • Big Data
    • Hadoop
    • Cloudera
    • Hortonworks HDP

How to automate sftp file transfers using expect utility

By admin

You can always configure passwordless ssh in order to transfer files using sftp without user being asked for a password. But if its not allowed to configure a passswordless ssh, there is a way to automate sftp file transfers. It can be achieved by using ‘expect‘ command provided by the tcl shell.

For the expect utility to work we need to install the “expect” package. To do so use the below command :

# yum install expect

Below is an example script that automates remote access with a batch file.

#!/bin/bash

if (( $# < 3 )); then
    echo "Usage: $0 [remote addr] [password] [batch file]"
    echo
    echo -e "tremote addr : [loginid]:[host addr]n"
    exit
fi

REMOTE=$1
PASS=$2
BATCH=$3
expect -c "
spawn sftp -o "batchmode no" -b "$BATCH" $REMOTE
expect -nocase "password:" {send "$PASSr"; interact}
"

Here,

spawn - to initiate the sftp process
expect - it expects a particular string ( here it is "password:" prompt )
send - sends the password when the expect utility gets the required prompt.

Here's an example how to run the script which run the commands in testjob.bat on remote.example.com after log in as root with a password 'password'

# bash ./test.sh root@remote.example.com password testjob.bat

Filed Under: Linux

Some more articles you might also be interested in …

  1. How to Disable RSH Server in CentOS/RHEL
  2. How to Change Kernel Semaphore Limits in CentOS/RHEL
  3. How to Configure Network Teaming in CentOS/RHEL 7
  4. How to Limit some User Memory Resources on CentOS/RHEL using cgroup
  5. How to Audit for Modifications to files and Executions of Files in Linux
  6. How to audit all Commands run on OEL 5,6 using auditd
  7. CIFS Share Filesystem Is Not Mounted after Reboot on CentOS/RHEL 7
  8. How to Audit File Access on Linux
  9. How to configure CentOS/RHEL 6 system to not used last 3 passwords used
  10. Linux “shutdown”, “poweroff”, “halt”, “reboot” Commands

You May Also Like

Primary Sidebar

Recent Posts

  • CentOS/RHEL 8: “ACPI MEMORY OR I/O RESET_REG” Server Hung after reboot
  • How to Create a Physical Standby Database by Using SQL and RMAN Commands
  • Basics of client connectivity in Oracle Data Guard configuration
  • ORA-354 ORA-353 and ORA-312: Possible corruption in Online Redo Log File Members in a Redo Log Group
  • Archives
  • Contact Us
  • Copyright

© 2021 · The Geek Diary