2017-03-19 1 views
1

호스트로 ssh로 서브 프로세스를 사용했습니다.내 로컬 컴퓨터에서 Linux 호스트로 SSH하고 루트 사용자에게 sudo

다음은 사용자로 ssh를 실행하고 명령을 실행하는 데 사용되는 코드 단편입니다. (이 같은)

sshProcess = subprocess.Popen(['ssh', hostname], stdin=subprocess.PIPE, stdout = subprocess.PIPE, universal_newlines=True, bufsize=0) 
sshProcess.stdin.write("hostname\n") 
sshProcess.stdin.write("ls -ldr %s \n"%path) 
sshProcess.stdin.write("pwd \n") 
sshProcess.stdin.close() 
for line in sshProcess.stdout: 
if line == "END\n": 
break 
print(line,end="") 

하지 터미널하지만 난

sshProcess.stdin.write("sudo su - serviceuser \n") 

답변

0

당신은 강제 할 수 명령 아래 실행할 수 아니에요 - 난 sudo는 명령을 사용하려고하면 내가 청각 장애와 관련된 오류 -t 옵션을 지정하여 pseudo-tty 할당. man ssh

-t  

Force pseudo-tty allocation. This can be used to execute arbitrary screen-based programs on a remote machine, which can be very useful, e.g. when implementing menu services. Multiple -t optionsforce tty allocation, even if ssh has no local tty.

+0

에서

subprocess.Popen(['ssh -t', hostname], ..... 

나는 이전에 그것을 시도,하지만 난 오류가 아래 역 추적 (마지막으로 가장 최근 통화)는 무엇입니까 작동하지 않았다 파일 "ssh.py"를 선 11, in sshProcess = subprocess.Popen ([ 'ssh -t', hostname], stdin = subprocess.PIPE, stdout = subprocess.PIPE, universal_newlines = True, bufsize = 0) 파일 "/ usr/lib/python2 .7/subprocess.py ", l 오프라인 (711)에 __init__ _execute_child 인상 child_exception에서, errwrite) 파일 "/usr/lib/python2.7/subprocess.py"라인 1343은, OSError errread : [errno를 2] 해당 파일이나 디렉토리 – Springhills

+0

내가 추가되지 shell = Popen에서 True이지만 다른 오류가 발생했습니다. sshProcess = subprocess.Popen ([ 'ssh -t', hostname], shell = True, stdin = subprocess.PIPE, stdout = subprocess.PIPE, universal_newlines = True, bufsize = 0) – Springhills

+0

사용법 : ssh [-1246AaCfGgKkMNnqsTtVvXxYy] [-b bind_address] [-c 암호 _spec] [-D [bind_address :] 포트] [-E log_file] [-e escape_char] [-F configfile] [-I pkcs11] [-i identity_file] [-J [사용자 @] 호스트 [: 포트]] [-L 주소] [-l login_name] [-m mac_spec] [-O ctl_cmd] [-o 옵션] [-p 포트] [-Q 쿼리 _ 옵션] [-R 주소] [-S ctl_path] [-W 호스트 : 포트] [-w local_tun [: remote_tun]] [ user @] hostname [command] – Springhills

관련 문제