Linux 通过cURL远程执行Bash脚本

Linux 通过cURL远程执行Bash脚本,linux,bash,shell,curl,Linux,Bash,Shell,Curl,我有一个简单的Bash脚本,它接收输入并用输入输出几行 fortinetest.sh read -p "Enter SSC IP: $ip " ip && ip=${ip:-1.1.1.1} printf "\n" #check IP validation if [[ $ip =~ ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$ ]]; then echo "SSC IP: $ip" printf "\n" else echo "Enter a v

我有一个简单的Bash脚本,它接收输入并用输入输出几行

fortinetest.sh

read -p "Enter SSC IP: $ip " ip && ip=${ip:-1.1.1.1}
printf "\n"

#check IP validation
if [[ $ip =~ ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
  echo "SSC IP: $ip"
  printf "\n"
else
  echo "Enter a valid SSC IP address. Ex. 1.1.1.1"
  exit
fi

我尝试将它们上传到我的服务器,然后尝试通过
curl

我不知道为什么在我使用cURL/wget时输入提示从未出现


我遗漏了什么吗?

curl…|bash
form,bash的stdin正在读取脚本,因此stdin不可用于
read
命令

尝试使用调用远程脚本,就像调用本地文件一样:

bash <( curl -s ... )

bash只需运行下面的脚本即可复制您的问题

$ cat test.sh | bash
Enter a valid SSC IP address. Ex. 1.1.1.1
这是因为您使用
管道启动的bash没有得到
TTY
,当您执行
read-p
时,它是从
stdin
读取的,在本例中,stdin是
test.sh
的内容。所以问题不在于旋度。问题不是从tty读取

因此,解决方法是确保您已从tty准备好

read < /dev/tty -p "Enter SSC IP: $ip " ip && ip=${ip:-1.1.1.1}
printf "\n"

#check IP validation
if [[ $ip =~ ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
  echo "SSC IP: $ip"
  printf "\n"
else
  echo "Enter a valid SSC IP address. Ex. 1.1.1.1"
  exit
fi

我个人更喜欢
source它必须是提示吗?似乎url参数会更简单。通过wget使用的curl不是一个允许提示/响应的终端仿真器,它是一个数据获取器(单向)。您需要通过curl命令添加SSC IP输入(即,
curl..sh<1234 | bash
)@PrestonM:我不介意使用您的建议,听起来比我现在尝试做的要好,但我不确定如何调整我的代码以使用从URL读取的参数。想让我从几句话开始吗?@RandyCasburn,你介意回答吗?您的建议可能对我正在尝试做的工作有效。首先,我不建议直接从
curl
运行脚本。良好的安全性要求您首先下载脚本,检查它以确保它是您期望的脚本,然后执行它<代码>卷曲…>tmp.sh;bash tmp.sh
。非常好!这绝对解决了我提到的问题!如果在执行过程中需要远程资源,会发生什么?那么下载脚本显然是错误的解决方案。您需要
ssh-tuser@host“bash script.sh”
@glennjackman-你能用脚本或证据举个例子吗?我试过了。似乎不起作用。我不确定我错过了什么。“不工作”是一个无用的问题描述。提供详细信息。外包脚本而不是在子流程中运行脚本的一个问题是,脚本可能会改变调用(交互式)shell的环境。(例如,弄乱您的
路径
、更改提示、设置意外选项、修改shell变量、设置信号处理程序等)@GertvandenBerg,您是否遇到过这种情况,并且能够重现这种情况?我没有遇到过这样的行为,这就是为什么我认为情况并非如此。我将用示例更新我的答案。快速示例将是
echo PS1=test>test.sh;bash test.sh#不更改调用脚本的shell中的提示符
source test.sh#更改调用shell的提示符
。(
source
是针对这种类型的东西的…)
vagrant@vagrant:/var/www/html$ curl -s localhost/test.sh | bash
Enter SSC IP:  2.2.2.2

SSC IP: 2.2.2.2
### Open Two Terminals
# In the first terminal run:
echo "sleep 5" > ./myTest.sh
bash ./myTest.sh

# Switch to the second terminal and run:
ps -efjh

## Repeat the same with _source_ command
# In the first terminal run:
source ./myTest.sh

# Switch to the second terminal and run:
ps -efjh
## Test for variables declared by the script:
echo "test_var3='Some Other Value'" > ./myTest3.sh
bash ./myTest3.sh
echo $test_var3
source ./myTest3.sh
echo $test_var3
## Test for usability of current environment variables:
test_var="Some Value" # Setting a variable
echo "echo $test_var" > myTest2.sh # Creating a test script
chmod +x ./myTest2.sh # Adding execute permission
## Executing:
. myTest2.sh
bash ./myTest2.sh
source ./myTest2.sh
./myTest2.sh
## All of the above results should print the variable.