S
S
Sergey Melnikov2016-08-01 19:53:13
linux
Sergey Melnikov, 2016-08-01 19:53:13

How to use bash to copy a file from server to server?

Greetings.
Tell me how to copy a file from server to server?
I read about scp, sftp - there you need to enter the password manually, but how to set the password so as not to enter it?
What rights to give the user on the second server to make it work?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
strelmax, 2016-08-01
@azazel_live

do key authentication
profhelp.com.ua/articles/kopirovanie-faylov-po-set...

D
Dmitry Malinin, 2016-08-11
@pdacity

Not exactly bash, but tcl, or rather a utility from it expect (as a rule, it is in the default installation on many systems) and scp
Allows you to pass a password to the session as an argument to the script.

#!/usr/bin/expect

# Do not show stdout of spawn process
log_user 0

# Show debug messages
exp_internal 0

# Timeout for executing command
set timeout 29

#
match_max 1024

set hostip [lindex $argv 0]
set user [lindex $argv 1]
set passwd [lindex $argv 2]
set srcfile [lindex $argv 3]
set dstfile [lindex $argv 4]
set port 22

if { [llength $argv] < 5 } {
    send_user "Usage ./scpput.tcl hostip user passwd srcfile dstfile \[port\]\n"
    exit 1
}

if { [llength $argv] == 6 } {
    set port [lindex $argv 5]
}

# Запись файла на удаленную хостовую машину
spawn scp -P $port $srcfile [email protected]$hostip:$dstfile
    expect {
        "?assword:" { 
            send "$passwd\r" 
        }
        "(yes/no)" {
                send "yes\r"
                expect "?assword:" 
                send "$passwd\r" 
        }
        timeout {
            send_user "Timeout to connect $hostip:$port\n"
            exit 2
        }
        eof {
            send_user "Can't connect to $hostip:$port\n"
            exit 2
        }
    }
    expect eof

Crutches, but it works. :)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question