다음은 서버에서 정보를 얻기 위해 매일 실행하는 스크립트의 예입니다. 지난 며칠 동안 로컬 파일에 캡처된 출력에서 일부 서버 데이터가 누락되었습니다 VS-HV-Report_2017.txt
.
스크립트를 실행할 때 오류 상태가 표시되지만 서버에 연결할 수 없나요? 그러면 출력 대신 검은색 선이나 오류 상태가 표시됩니까?
#!/usr/bin/expect
set timeout 5
#find /path/to/files -type f -mtime +10 -delete
set date [exec date "+%d-%B-%Y"]
spawn sh -c "yes | cp -ifr .ssh/VS-HV-config .ssh/config"
spawn sh -c "> VS-HV-Report_2017.txt"
#cat ./info.py
spawn sh -c "ssh L1n \"./info.py | sed 's/total.*//g'\" >> VS-HV-Report_2017.txt"
expect "Enter passphrase for key '/root/.ssh/id_rsa':"
send "passwd\r"
spawn sh -c "ssh L2n \"./info.py | sed 's/total.*//g'\" >> VS-HV-Report_2017.txt"
expect "Enter passphrase for key '/root/.ssh/id_rsa':"
send "passwd\r"
spawn sh -c "ssh L3n \"./info.py | sed 's/total.*//g'\" >> VS-HV-Report_2017.txt"
expect "Enter passphrase for key '/root/.ssh/id_rsa':"
send "passwd\r"
set timeout 5
#spawn sh -c "./format-VS-HV.sh > format-allinement-output.csv"
#spawn sh -c \"./format-VS-HV.sh\" > format-allinement-output.csv
exec ./format-VS-HV.sh > /root/format-allinement-output/format-allinement-output-$date.csv
답변1
귀하의 코드는 TCL과 셸의 이상한 조합이므로 둘 중 하나만 사용하여 작성해야 할 것입니다. TCL의 경우 다음과 같을 수 있습니다.
#!/usr/bin/expect
set timeout 5
# TCL has time functions, no need to call out to `date`...
set epoch [clock seconds]
set outfile "VS-HV-Report_[clock format $epoch -format "%Y"].txt"
set date [clock format $epoch -format "%d-%B-%Y"]
# "yes | cp -i ..." is very strange; why the interactive flag and then
# in turn ignoring that with the "yes |" pipe from non-interactive code?
#spawn sh -c "yes | cp -ifr .ssh/VS-HV-config .ssh/config"
exec cp -f .ssh/VS-HV-config .ssh/config
# TCL can do I/O, let's use that instead of forking sh processes...
#spawn sh -c "> VS-HV-Report_2017.txt"
set outfh [open $outfile w+]
# and a TCL loop instead of the duplicated spawn-to-host code...
set hostlist {L1n L2n L3n}
foreach host $hostlist {
set done_auth 0
# Spawn sh to spawn ssh is mighty complicated; let's instead just
# call ssh directly (and catch any errors...)
#spawn sh -c "ssh L1n \"./info.py | sed 's/total.*//g'\" >> VS-HV-Report_2017.txt"
if { [catch {spawn ssh $host {./info.py | sed 's/total.*//g'}} msg] } {
puts $outfh "SSHFAIL $host $msg"
continue
}
while 1 {
expect {
-re {Enter passphrase for key '[^']+': } {
# since in loop (because EOF or timeout could happen
# anywhere) must only auth once in the event the remote
# side is up to no good
if {$done_auth == 0} {
send "hasapass\r"
set done_auth 1
}
}
# remember that set timeout thing? there's a hook for it.
timeout {
puts $outfh "TIMEOUT $host $timeout"
break
}
# dump whatever "server data" returned into the output
# file (match_max might be relevant reading if there's a
# lot of data) when the connection closes
eof {
puts $outfh $expect_out(buffer)
break
}
}
}
}
close $outfh
exec ./format-VS-HV.sh > /root/format-allinement-output/format-allinement-output-$date.csv