sourcetip

URL에서 bash 스크립트 실행

fileupload 2023. 4. 28. 21:14
반응형

URL에서 bash 스크립트 실행

URL에 파일이 있다고 말합니다.http://mywebsite.example/myscript.txt스크립트가 포함되어 있습니다.

#!/bin/bash
echo "Hello, world!"
read -p "What is your name? " name
echo "Hello, ${name}!"

그리고 이 스크립트를 파일에 저장하지 않고 실행하려고 합니다.이거 어떻게 하는 거지?

이제 구문을 보았습니다.

bash < <(curl -s http://mywebsite.example/myscript.txt)

하지만 파일에 저장한 다음 실행하는 것처럼 작동하지는 않습니다.예를 들어, 읽기 줄이 작동하지 않고 출력은 다음과 같습니다.

$ bash < <(curl -s http://mywebsite.example/myscript.txt)
Hello, world!

마찬가지로 시도해 본 적이 있습니다.

curl -s http://mywebsite.example/myscript.txt | bash -s --

같은 결과로.

원래 다음과 같은 솔루션이 있었습니다.

timestamp=`date +%Y%m%d%H%M%S`
curl -s http://mywebsite.example/myscript.txt -o /tmp/.myscript.${timestamp}.tmp
bash /tmp/.myscript.${timestamp}.tmp
rm -f /tmp/.myscript.${timestamp}.tmp

하지만 이것은 엉성해 보이고, 저는 좀 더 우아한 해결책을 원합니다.

URL에서 셸 스크립트를 실행하는 것과 관련된 보안 문제는 알고 있지만, 지금은 이 모든 것을 무시해 보겠습니다.

source <(curl -s http://mywebsite.example/myscript.txt)

그것을 해야 합니다.또는 표준 입력을 리디렉션하는 초기 리디렉션을 해제합니다.bash리디렉션 없이 정상적으로 실행되는 파일 이름을 사용합니다.<(command)구문은 경로를 제공합니다.

bash <(curl -s http://mywebsite.example/myscript.txt)

의 출력을 보면 더 명확할 수 있습니다.echo <(cat /dev/null)

다음은 인수(arg1 arg2)를 전달하여 원격 스크립트를 실행하는 방법입니다.

curl -s http://server/path/script.sh | bash /dev/stdin arg1 arg2

Bash, Bourne 조개와 물고기:

curl -s http://server/path/script.sh | bash -s arg1 arg2

플래그 "-s"는 stdin에서 셸을 읽게 합니다.

사용:

curl -s -L URL_TO_SCRIPT_HERE | bash

예:

curl -s -L http://bitly/10hA8iC | bash

사용.wget이는 일반적으로 기본 시스템 설치의 일부입니다.

bash <(wget -qO- http://mywebsite.example/myscript.txt)

다음과 같은 작업도 수행할 수 있습니다.

wget -O - https://raw.github.com/luismartingil/commands/master/101_remote2local_wireshark.sh | bash

가장 좋은 방법은

curl http://domain/path/to/script.sh | bash -s arg1 arg2

이것은 @user77115에 의한 약간의 답변 변경입니다.

사용할 수 있습니다.curl그리고 그것을 지정합니다.bash다음과 같이:

bash <(curl -s http://mywebsite.example/myscript.txt)

나는 종종 다음을 사용하면 충분합니다.

curl -s http://mywebsite.example/myscript.txt | sh

하지만 오래된 시스템(커널 2.4)에서는 문제가 발생하고 다음을 수행하면 해결할 수 있습니다. 다른 많은 시도를 해보았지만 다음과 같은 작업만 수행했습니다.

curl -s http://mywebsite.example/myscript.txt -o a.sh && sh a.sh && rm -f a.sh

$ curl -s someurl | sh
Starting to insert crontab
sh: _name}.sh: command not found
sh: line 208: syntax error near unexpected token `then'
sh: line 208: ` -eq 0 ]]; then'
$

이 문제는 네트워크 속도가 느리거나 네트워크 속도를 정상적으로 처리할 수 없는 너무 오래된 버전으로 인해 발생할 수 있습니다.

하지만, 다음은 문제를 해결합니다.

$ curl -s someurl -o a.sh && sh a.sh && rm -f a.sh
Starting to insert crontab
Insert crontab entry is ok.
Insert crontab is done.
okay
$

또한:

curl -sL https://.... | sudo bash -

amra와 user77115의 답변을 결합하는 것만으로도:

wget -qO- https://raw.githubusercontent.com/lingtalfi/TheScientist/master/_bb_autoload/bbstart.sh | bash -s -- -v -v

-v-v 옵션을 전달하는 bbstart.sh 원격 스크립트를 실행합니다.

다음 명령을 사용하는 일부 무인 스크립트입니다.

sh -c "$(curl -fsSL <URL>)"

URL에서 직접 스크립트를 실행하지 않는 것이 좋습니다.실행하기 전에 URL이 안전한지 확인하고 스크립트 내용을 확인해야 합니다. 실행하기 전에 SHA256 체크섬을 사용하여 파일의 유효성을 검사할 수 있습니다.

스크립트를 직접 실행하는 대신 먼저 스크립트를 다운로드한 다음 실행합니다.

SOURCE='https://gist.githubusercontent.com/cci-emciftci/123123/raw/123123/sample.sh'

curl $SOURCE -o ./my_sample.sh
chmod +x my_sample.sh
./my_sample.sh

이 방법은 좋고 관습적입니다.

17:04:59@itqx|~
qx>source <(curl -Ls http://192.168.80.154/cent74/just4Test) Lord Jesus Loves YOU
Remote script test...
Param size: 4

---------
17:19:31@node7|/var/www/html/cent74
arch>cat just4Test
echo Remote script test...
echo Param size: $#

스크립트가 무엇인지에 관계없이 현재 셸을 사용하여 스크립트를 실행하려면 다음을 사용합니다.

${SHELL:-sh} -c "$(wget -qO - http://mywebsite.example/myscript.txt)"

에 ,wget또는:

${SHELL:-sh} -c "$(curl -Ls http://mywebsite.example/myscript.txt)"

에 ,curl.

스크립트가 대화형인 경우, 즉 사용자에게 입력을 요청하는 경우에도 이 명령은 계속 작동합니다.

에는 "OpenWRT"가 .wget복제하지만 복제하지 않음curl결석으로

bash | curl http://your.url.here/script.txt

실제 예:

juan@juan-MS-7808:~$ bash | curl https://raw.githubusercontent.com/JPHACKER2k18/markwe/master/testapp.sh


Oh, wow im alive


juan@juan-MS-7808:~$ 

언급URL : https://stackoverflow.com/questions/5735666/execute-bash-script-from-url

반응형