분명히 명령을 루트로 처리하는 스크립트를 실행하는 것은 큰 보안 문제이며 일반적으로 허용되지 않습니다.
그러나, Expect은 가상 터미널을 실행할 수 있으므로 사람이 키보드로 타이핑 할 때처럼 행동 할 수 있습니다.
이 질문은 다소 질문했지만 테스트되지 않았습니다. 그것은 나를라면
#!/bin/sh
# if run from shell, restart with wish \
exec wish "$0" "[email protected]"
package require Expect
proc fix_a_host {host usrname password root_password} {
# open session to host, wait for a username prompt
spawn ssh $host
expect "username:"
# send username, wait for a password prompt
send "$usrname\r"
expect "password:"
# send password, wait for shell prompt
send "$password\r"
expect "%"
# become root, wait for prompt
send "su\r"
expect "#"
# change TEXT to NEWTEXT in password file
send "sed 's/TEXT/NEWTEXT'" /etc/passwd
expect "#"
# exit root, exit host connection
send "exit\r"
expect "%"
send "exit\r"
expect eof
}
fix_a_host {"host1" "someuser" "user_password" "root_password"}
fix_a_host {"host2" "someuser" "user_password" "root_password"}
, 나는 그것이 잘 작동 확신 할 때까지 훨씬 덜 파괴, grep TEXT /etc/passwd
같은에 나오지 명령을 변경할 것입니다. 보고 싶은 출력을 표시하는 원격 명령의 경우
set results $expect_out(buffer)
send "some_command\r"
expect "#" # use the command prompt to indicate output is complete
puts "command output is got `$buffer`"
감사합니다. – jaxxstorm