๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ’ปTech/๐ŸงLinux

[๋ฆฌ๋ˆ…์Šค] ์‰˜ ์Šคํฌ๋ฆฝํŠธ์—์„œ scp, sftp ์‚ฌ์šฉ ๋ฐฉ๋ฒ•

by _viper_ 2019. 5. 23.
๋ฐ˜์‘ํ˜•

๋ฆฌ๋ˆ…์Šค ์„œ๋ฒ„์—์„œ ์™ธ๋ถ€์„œ๋ฒ„๋กœ ํŒŒ์ผ ์ „์†กํ•  ๋•Œ scp ๋ช…๋ น์–ด๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

์ฃผ๊ธฐ์ ์œผ๋กœ ํŒŒ์ผ์„ ์ „์†กํ•ด์•ผ ํ•  ๊ฒฝ์šฐ ์•„๋ž˜ ๋‚ด์šฉ ์Šคํฌ๋ฆฝํŠธ ์ž‘์„ฑ์— ์ฐธ๊ณ ํ•ด ์ฃผ์„ธ์š”.

 

โ—พ scp ์‚ฌ์šฉ๋ฒ•

expect << EOF
spawn scp /๊ฒฝ๋กœ/ํŒŒ์ผ๋ช… ๊ณ„์ •@์•„์ดํ”ผ:/๊ฒฝ๋กœ
expect "password:"
        send "ํŒจ์Šค์›Œ๋“œ\r"
expect eof
EOF

 

  • ์šฉ์–ด ์ •๋ฆฌ
    • expect : scp ์‚ฌ์šฉ ์‹œ ํŒจ์Šค์›Œ๋“œ๋ฅผ ์ž…๋ ฅํ•ด์•ผ ํ•˜๋Š”๋ฐ ์ธ์ฆ ๋ถ€๋ถ„์„ ์ฒดํฌํ•ด ์ค„ ๋ช…๋ น์–ด
      ex) "password:" ๋ž€ ๋ฌธ์ž๊ฐ€ ๋‚˜ํƒ€๋‚˜๋ฉด send๋กœ ํŒจ์Šค์›Œ๋“œ๋ฅผ ์ž…๋ ฅํ•ด ์ค๋‹ˆ๋‹ค.
    • spawn : expect ์•ˆ์—์„œ scp ๋ช…๋ น์–ด๋ฅผ ์‹คํ–‰์‹œํ‚ฌ ๋•Œ ์‚ฌ์šฉ (์‰˜์—์„œ ๋ฆฌ๋ˆ…์Šค ๋ช…๋ น์–ด ์‹คํ–‰์‹œํ‚ค๋Š” ``์™€ ๊ฐ™์€ ์˜๋ฏธ)
    • \r์€ ํŒจ์Šค์›Œ๋“œ ์ž…๋ ฅ ํ›„ ์—”ํ„ฐ(๊ฐœํ–‰๋ฌธ์ž)๋ฅผ ์ž…๋ ฅํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์‚ฌ์šฉ

 

โ—พ sftp ์‚ฌ์šฉ๋ฒ•

  • sftp ์ ‘์†ํ•˜์—ฌ ํŒŒ์ผ put, sftp ํฌํŠธ ์ ‘์†
expect << EOF
set timeout 120

## ํฌํŠธ ์ž…๋ ฅํ•  ๊ฒฝ์šฐ
## spawn sftp -oPORT=10024 $GET_USER@$GET_SERVER_IP
spawn sftp $GET_USER@$GET_SERVER_IP

expect "password:" { send "${PASSWORD}\r"}
expect "sftp>" { send "cd ${DIR}\r"}
expect "sftp>" { send "put ${FILE}\r"}
expect "sftp>" { send "bye\r"}
expect eof
EOF