我在 Gnu/Linux 上创建了一个 Bash Shell 脚本,它可以从一台服务器到一组其他服务器进行 ssh,并为一组 IP 运行 netcat 命令。
这是通过 SSH 连接到其他服务器的脚本:
*read -p "Enter the instant stack host[s]: " hosts
read -p "Enter the list of destinations [IP]: " dest_list
read -p "Enter the port # associated with the destination: " port
for h in $hosts
do
scp ./connectivityTest.sh @$h:/var/tmp/
ssh user@$h . /var/tmp/connectivityTest.sh "$dest_list" "$port"
done*
这是运行 netcat 命令的脚本:
*echo "Working on `hostname` to check connectivity for following end points"
echo $1
echo $2
dest_list=`echo "$1"`
port=`echo "$2"`
echo $dest_list
for d in $dest_list
do
echo "Endpoint checked: $d"
nc -v -i 1 -w 5 $d $port
done
exit*
当我只提供单个 IP [端点] 来运行时,脚本运行良好:
*Enter the instant stack host[s]: host1.com
Enter the list of destinations [IP]: **test.ip1.com**
Enter the port # associated with the destination: 443
connectivityTest.sh 100% 520 1.8MB/s 00:00
##############################################################
Working on host1 to check connectivity for following end points
##############################################################
test.ip1.com
443
test.ip1.com
Endpoint checked: test.ip1.com
Ncat: Version 7.50 ( https://nmap.org/ncat )
Ncat: Connected to ip1:443.
Ncat: Idle timeout expired (1000 ms).*
但是,当我给出多个端点时,脚本将使用第二个 IP/端点作为端口号。
*Enter the instant stack host[s]: host1.com
Enter the list of destinations [IP]: **test.ip1.com test.ip2.com**
Enter the port # associated with the destination: 443
connectivityTest.sh 100% 520 1.8MB/s 00:00
##############################################################
Working on host1 to check connectivity for following end points
##############################################################
test.ip1.com
**test.ip2.com**
test.ip1.com
Endpoint checked: test.ip1.com
Ncat: Version 7.50 ( https://nmap.org/ncat )
**Ncat: Invalid port number "test.ip2.com". QUITTING.***
此外,只有当脚本通过 SSH 运行时才会发生这种情况。如果我直接在服务器上运行脚本,它可以在多个 IP 上正常工作。很明显,在这个级别上有些事情是不对的:
*ssh user@$h . /var/tmp/connectivityTest.sh "$dest_list" "$port"*
然后我找到了这个解决办法。我在单引号下添加了输入变量:
*ssh user@$h . /var/tmp/ "'$dest_list'" "'$port'"*
现在它可以与多个 IP 配合使用:
*Enter the instant stack host[s]: host1.com
Enter the list of destinations [IP]: **test.ip1.com test.ip2.com test.ip3.com test.ip4.com**
Enter the port # associated with the destination: 443
connectivityTest.sh 100% 525 1.5MB/s 00:00
##############################################################
Working on host1 to check connectivity for following end points
##############################################################
test.ip1.com test.ip2.com
443
test.ip1.com test.ip2.com
Endpoint checked: test.ip1.com
Ncat: Version 7.50 ( https://nmap.org/ncat )
Ncat: Connected to ip1:443.
Ncat: Idle timeout expired (1000 ms).
Endpoint checked: test.ip2.com
Ncat: Version 7.50 ( https://nmap.org/ncat )
Ncat: Connected to ip2:443.
Ncat: Idle timeout expired (1000 ms).*
虽然我解决了,但我不知道如何解决单引号跑步之间的区别本地脚本与通过 ssh 运行脚本。还有如何单引号跑步之间的区别具有多个输入的脚本与单个输入的脚本。
任何见解都会有所帮助。提前致谢 !!!
答案1
如果你会发生什么
ssh user@$h . /var/tmp/connectivityTest.sh '$dest_list' '$port'
?
如果它的工作原理与您的解决方法相同,则问题在于您的变量正在哪个系统上扩展(本地或远程)。