1

The below is the sample of a script which we execute daily for getting info from the server. For the last couple of days, some server data is missing from the output which is captured on the local file VS-HV-Report_2017.txt.

Is it possible to get error status when the script is executed, but its unable to connect the server? So that we have a black line or and error status instead of output?

#!/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
4
  • What error status does ./info.py report? What does the output look like, and what are you interested in from that output?
    – thrig
    Jul 6, 2017 at 15:41
  • @thrig: Actually ./info.py does't show any error it gives output, we have edited the question. sometime it happens that the server is not connected or the script did not give any output. So the expected output is not recorded in the file in this case it is VS-HV-Report_2017.txt.
    – Shanker
    Jul 7, 2017 at 10:12
  • I am guessing you use either the python module pexpect or the command expect? If it's just for storing the SSH password, you could also use sshpass in a normal shell script and handle everything there. Or you tell us with what you really start your shown commands with and we can maybe help out.
    – Jaleks
    Jul 7, 2017 at 10:32
  • @Jaleks we have update the question in between there are many server which will be connected. i have given the start and the end
    – Shanker
    Jul 7, 2017 at 10:43

1 Answer 1

0

Your code is a curious mix of TCL and shell, and probably should be written in only one or the other. With TCL, it might look something like

#!/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
1
  • Its not working. there is no output not any error. currently there is no output from epoch.
    – Shanker
    Jul 10, 2017 at 3:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .