Get the App
SLTechnology News&Howtos  ›  Servers  › 

Scp & # 39 th port & # 39; 22 problem solving

Shulou Source: shulou.com Published: 2022-06-03 01:58:36 09月25日 Update

Problems encountered when writing a scp push file script

Script

# cat test.shemaking binbinapacheIP = `head-1 hosts.txt | tail-1 | awk-F ""'{print $1} '`PORT= `head-1 hosts.txt | tail-1 | awk-F ""' {print $2} '`scp-P ${PORT}-r $1 ${head}: / tmp

Execution result

# sh test.sh / tmp/yum.log'ad port '22lost connection

Hosts.txt file content

# cat hosts.txt 192.168.0.103 22

Cause analysis

Enable the shell debugging option, and the execution result is as follows

# sh-x test.sh / tmp/yum.log++ awk'- F'{print $1}'+ + tail-1} + head-1 hosts.txt+ IP=192.168.0.103++ awk'- F''{print $2}'+ + tail-1} + head-1 hosts.txt+ PORT=$'22\ rly + scp-P $'22\ r'- r / tmp/yum.log 192.168.0.103:/tmpyum.log'ad port '22lost connection

I wonder why there are so many problems with PORT=$'22\ r, Baidu $'\ r'.\ r is the newline character of windows. The problem may be that I wrote the hosts.tx file from windows and uploaded it to linux; because $2 is the last field, so $2 contains\ r

Solution.

Method 1: add a space after each line of the hosts.txt file

Method 2: install dos2unix

# yum install dos2unix-y. # dos2unix hosts.txt dos2unix: converting file hosts.txt to UNIX format...

Successful execution of the script again

# sh-x test.sh / tmp/yum.log + + awk'- F'{print $1}'+ + tail-1 hosts.txt+ PORT=22+ scp + head-1 hosts.txt+ IP=192.168.0.103++ awk'- F''{print $2}'+ + tail-1 hosts.txt+ PORT=22+ scp-P 22-r / tmp/yum.log 192.168.0.103:/tmpyum.log

Tags: File question script content method result success one line again method cause cause analysis field newline character time space analysis push Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi vpn MariaDB Shulou Information Apple