shlex와 함께 subprocess.popen을 사용하여 ssh를 사용하여 원격 bash 스크립트를 호출합니다. 이 명령은 bash 자체에서 아주 잘 작동합니다. 하지만 최대한 빨리 그것을 python 및 shlex subprocess.popen 변환하려고하면 오류가 발생합니다.Python을 사용하여 ssh를 통한 파일을 원격 bash 스크립트에 cat
원격 bash는 스크립트를
#!/bin/bash
tmp="";
while read -r line;
do
tmp="$tmp $line\n";
done;
echo $tmp;
BASH CMD의 결과 (명령 행에서 원격 bash는 스크립트를 호출)
$> ssh x.x.x.x cat < /tmp/bef69a1d-e580-5780-8963-6a9b950e529f.txt " | /path/to/bash/script.sh;"
Bar\n
$>
파이썬 코드
import shlex
import subprocess
fn = '/tmp/bef69a1d-e580-5780-8963-6a9b950e529f.txt'
s = """
ssh x.x.x.x cat < {localfile} '| /path/to/bash/script.sh;'
""".format(localfile=fn)
print s
lexer = shlex.shlex(s)
lexer.quotes = "'"
lexer.whitespace_split = True
sbash = list(lexer)
print sbash
# print buildCmd
proc=subprocess.Popen(sbash,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
out,err=proc.communicate()
print "Out: " + out
print "Err: " + err
PYTHON SCRIPT 결과 :
$> python rt.py
ssh x.x.x.x cat < /tmp/bef69a1d-e580-5780-8963-6a9b950e529f.txt '| /path/to/bash/script.sh'
['ssh', 'x.x.x.x', 'cat', '<', '/tmp/bef69a1d-e580-5780-8963-6a9b950e529f.txt', "'| /path/to/bash/script.sh'"]
Out:
Err: bash: /tmp/bef69a1d-e580-5780-8963-6a9b950e529f.txt: No such file or directory
$>
무엇이 누락 되었습니까?
왜 파이썬 스크립트가 bash 스크립트에없는 것으로 보이는 인수 목록에 여분의 ''< "가 있습니까? (또는 나는 이것을 잘못 읽고 있냐?) – mgilson
나는 그것을 놓쳤다, 오타. 나는 bash 명령 행에 <를 넣음으로써 그것을 바로 잡았다. – Chris
'/ tmp/bef69a1d-e580-5780-8963-6a9b950e529f.txt' 파일은 로컬 또는 원격 시스템에 있습니까? 지역 일 경우 커맨드를 다르게 실행한다고 생각합니다.'cat /tmp/bef69a1d-e580-5780-8963-6a9b950e529f.txt | ssh x.x.x.x/path/to/bash/script.sh' 원격 끝에'cat'을 실행할 필요가 없습니다. (이것이 파이썬 문제를 해결할 지 확신 할 수 없다.) – Blckknght