2016-12-02 8 views
0

두 개의 서버 a와 b 사이에 암호없는 ssh가 활성화되어 있지 않습니다. 그래서 sshpass를 사용하여 a에서 서버 b에 연결합니다.쉘 스크립트를 사용하여/etc/hosts 파일을 수정하십시오.

a에서 서버 b의/etc/hosts에 호스트 항목을 추가해야한다는 요구 사항이 있습니다. 하지만 서버 b에 로그인하는 사용자는 root가 아닌 사용자이지만 root가 소유 한 파일을 편집 할 수있는 sudo 권한이 있습니다.

sshpass를 사용하는 동안 쉘 스크립트를 통해 서버 a의 서버 b의/etc/hosts에 호스트 항목을 추가하려면 어떻게해야합니까? 여기

시도 된 스크립트입니다

#!/bin/bash 

export SSHPASS="password" 
SSHUSER=ciuser 
WPC_IP=10.8.150.28 

sshpass -e ssh -o UserKnownHostsFile=/dev/null -o 'StrictHostKeyChecking no' [email protected]$WPC_IP "echo test >> /etc/hosts" 

출력 :

bash test.sh 
Warning: Permanently added '10.8.150.28' (RSA) to the list of known hosts. 
bash: /etc/hosts: Permission denied 

감사합니다.

+0

시도한 명령과 실패한 명령을 게시 할 수 있습니까? –

+0

@thatotherguy : 질문을 업데이트했습니다. 감사 – user2714227

답변

1

sudodoesn't work with redirects 직접, 그래서 당신이 파일에 추가 할 sudo tee -a를 사용할 수 있습니다 명령에

echo '1.2.3.4 test' | sudo tee -a /etc/hosts 

,이 것이가없는 암호없이 sudo를 액세스를 필요로

sshpass -e ssh -o UserKnownHostsFile=/dev/null -o 'StrictHostKeyChecking no' "[email protected]$WPC_IP" "echo test | sudo tee -a /etc/hosts" 

주 tty는 sudo 권한과 반드시 ​​같을 필요는 없습니다.