원격 ssh 명령에서 변수 전달
ssh를 사용하여 내 컴퓨터에서 명령을 실행하고 환경 변수를 전달하고 싶습니다. $BUILD_NUMBER
내가 시도하는 것은 다음과 같습니다.
ssh pvt@192.168.1.133 '~/tools/myScript.pl $BUILD_NUMBER'
$BUILD_NUMBER
ssh를 호출하는 시스템에 설정되어 있고 변수가 원격 호스트에 존재하지 않기 때문에 선택되지 않습니다.
의 값을 $BUILD_NUMBER
어떻게 전달 합니까?
사용하는 경우
ssh pvt@192.168.1.133 "~/tools/run_pvt.pl $BUILD_NUMBER"
대신에
ssh pvt@192.168.1.133 '~/tools/run_pvt.pl $BUILD_NUMBER'
쉘은 $BUILD_NUMBER
명령 문자열을 원격 호스트에 보내기 전에 보간합니다 .
작은 따옴표로 묶인 변수는 평가되지 않습니다. 큰 따옴표 사용 :
ssh pvt@192.168.1.133 "~/tools/run_pvt.pl $BUILD_NUMBER"
쉘은 작은 따옴표가 아닌 큰 따옴표로 변수를 확장합니다. 이것은 ssh
명령 에 전달되기 전에 원하는 문자열로 변경됩니다 .
(이 대답은 불필요하게 복잡해 보일 수 있지만 내가 아는 한 공백 및 특수 문자와 관련하여 쉽게 확장 가능하고 강력합니다.)
ssh
명령 의 표준 입력을 통해 바로 데이터를 공급 read
하고 원격 위치에서 데이터를 공급할 수 있습니다 .
다음 예에서
- 인덱스 배열은 원격 측에서 값을 검색하려는 변수의 이름으로 (편의를 위해) 채워집니다.
- 이러한 각 변수에 대해 변수
ssh
의 이름과 값을 제공하는 null로 끝나는 줄에 제공합니다. - 에서
shh
명령 자체, 우리는이 라인을 통해 루프 필요한 변수를 초기화합니다.
# Initialize examples of variables.
# The first one even contains whitespace and a newline.
readonly FOO=$'apjlljs ailsi \n ajlls\t éjij'
readonly BAR=ygnàgyààynygbjrbjrb
# Make a list of what you want to pass through SSH.
# (The “unset” is just in case someone exported
# an associative array with this name.)
unset -v VAR_NAMES
readonly VAR_NAMES=(
FOO
BAR
)
for name in "${VAR_NAMES[@]}"
do
printf '%s %s\0' "$name" "${!name}"
done | ssh user@somehost.com '
while read -rd '"''"' name value
do
export "$name"="$value"
done
# Check
printf "FOO = [%q]; BAR = [%q]\n" "$FOO" "$BAR"
'
산출:
FOO = [$'apjlljs ailsi \n ajlls\t éjij']; BAR = [ygnàgyààynygbjrbjrb]
필요하지 않은 export
경우 declare
대신 을 사용할 수 있습니다 export
.
A really simplified version (if you don’t need the extensibility, have a single variable to process, etc.) would look like:
$ ssh user@somehost.com 'read foo' <<< "$foo"
As answered previously, you do not need to set the environment variable on the remote host. Instead, you can simply do the meta-expansion on the local host, and pass the value to the remote host.
ssh pvt@192.168.1.133 '~/tools/run_pvt.pl $BUILD_NUMBER'
If you really want to set the environment variable on the remote host and use it, you can use the env
program
ssh pvt@192.168.1.133 "env BUILD_NUMBER=$BUILD_NUMBER ~/tools/run_pvt.pl \$BUILD_NUMBER"
In this case this is a bit of an overkill, and note
env BUILD_NUMBER=$BUILD_NUMBER
does the meta expansion on the local host- the remote
BUILD_NUMBER
environment variable will be used by
the remote shell
Escape the variable in order to access variables outside of the ssh session: ssh pvt@192.168.1.133 "~/tools/myScript.pl \$BUILD_NUMBER"
참고URL : https://stackoverflow.com/questions/3314660/passing-variables-in-remote-ssh-command
'programing tip' 카테고리의 다른 글
프로덕션에서 바벨 노드를 사용해도 되나요? (0) | 2020.09.24 |
---|---|
여러 조건으로 if 문을 형식화하는 가장 좋은 방법 (0) | 2020.09.24 |
SSRS 2008 R2-SSRS 2012-ReportViewer : Safari / Chrome 보고서는 있지만 Firefox / Internet Explorer 8에서는 제대로 작동합니다. 왜 그런가요? (0) | 2020.09.24 |
Chrome에서 인라인 자바 스크립트 리디렉션 전에 자바 스크립트 중단 (0) | 2020.09.24 |
javax.persistence.Table.indexes () [Ljavax / persistence / Index의 NoSuchMethodError (0) | 2020.09.24 |