shell指令碼實現檔案遠端拷貝scp

2021-09-28 16:04:56 字數 860 閱讀 9972

場景:將a伺服器上面的cc.jar包,複製到b伺服器上面:

實現方法1:

1、寫指令碼cp.sh:

內容為:

#!/bin/bash

ip=$1  (注釋:這裡引數化了目標伺服器的ip,執行指令碼時候將引數加在後面)

./scp.exp  ./cc.jar  root@$1:/root/tools

寫完後給許可權:chmod 777 cp.sh

執行命令為:./cp.sh  10.179.23.43 (目標位址是以引數傳進去的)

這個時候發現執行出錯,做步驟2:

2、保證伺服器已經安裝了expect,安裝命令為:

yum install expect

新建檔案scp.exp檔案,內容為:

#!/usr/bin/expect 

set timeout 20

if

set local_file [lindex $ar** 0]

set remote_path [lindex $ar** 1]

set passwd 目標伺服器的密碼

set passwderror 0

spawn scp $local_file $remote_path

expect

set timeout 1000

set passwderror 1

send "$passwd\r"

exp_continue

} "*es/no)?*"

timeout

}

修改許可權:chmod 777  scp.exp

3、再次執行指令碼,發現copy成功。

scp遠端拷貝expect實現SHELL

在linux上,我們可以很方便的使用scp命令來遠端拷貝檔案,但是由於scp命令本身無儲存密碼選項,所以在shell裡面無法像samba那樣子儲存密碼,比較麻煩,下面介紹下兩種實現方法。方法一 scp 遠端拷檔案,建議用搭配 ssh 方法 1 在客戶機上生成 ssh 需要的 rsa 金鑰 ssh k...

Linux使用shell指令碼批量拷貝檔案

在日常工作中我們經常會碰到在linux下匯出檔案的工作,如果挨個拷貝就很麻煩也很不方便。usr bin env bash file name.txt for state in cat file doecho visit beautiful state done usr bin env bash ec...

遠端呼叫shell指令碼檔案和遠端複製檔案

1 安裝sshpass yum install sshpass sshpass p sa ssh root 192.168.56.105 c bin bash test.sh 3 從本地複製檔案到遠端伺服器 sshpass p sa scp egova pub ex.jar root 192.168...