
expect_popen 함수는 SSH 명령어를 실행할 때 오류를 발생시키는 경우가 있습니다. 이 경우 원인과 해결 방법을 설명하겠습니다.
expect_popen 함수는 expect 명령어를 사용하여 SSH 명령어를 실행합니다. expect 명령어는 SSH 서버에 접속하고, 암호를 입력하는 과정을 자동화합니다. 하지만 expect 명령어의 timeout 파라미터를 설정하는 것이 중요합니다.
expect 명령어의 timeout 파라미터는 SSH 서버에 접속하는 데 걸리는 시간을 초과하는 경우 오류를 발생시키는 데 사용됩니다. 따라서 timeout 파라미터를 설정할 때는 SSH 서버에 접속하는 데 걸리는 시간을 고려해야 합니다.
예를 들어, SSH 서버에 접속하는 데 10초가 걸리는 경우 timeout 파라미터를 15초로 설정하면 됩니다. 이 경우 expect 명령어는 SSH 서버에 접속하는 데 걸리는 시간을 초과하는 경우 오류를 발생시키지 않습니다.
위 코드에서 expect_popen 함수의 timeout 파라미터를 설정하는 방법은 다음과 같습니다.
#hostingforum.kr
python
import subprocess
import time
# SSH 명령어를 실행할 때 expect_popen 함수 사용
ssh_command = "ssh user@host 'ls -l'"
process = subprocess.Popen(ssh_command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
# expect_popen 함수 사용
expect_popen = subprocess.Popen(["expect", "-c", "spawn " + ssh_command + "; expect "password: " ; send "password\r" ; interact"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
# timeout 파라미터 설정
timeout = 15 # 초 단위
# timeout 초과 시 오류 발생
if process.poll() is not None or expect_popen.poll() is not None:
print("Timeout 발생")
else:
print("성공적으로 SSH 명령어 실행")
위 코드에서 expect_popen 함수의 timeout 파라미터를 15초로 설정했습니다. 이 경우 expect 명령어는 SSH 서버에 접속하는 데 걸리는 시간을 초과하는 경우 오류를 발생시키지 않습니다.
2025-05-11 21:30