Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/03 Report--

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

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report