Reputation: 23
I am a system tester and I have a particular set of DUTs which can run either on a pre-release version of the firmware or a release candidate. On the pre-release I can access the Linux core OS of the DUT by logging in with a particular user account. The release candidate does not allow this.
The whole basis of the script I'm writing is to be able to remotely execute a script which resides on the DUT. I would like to check if I have access to the Linux core OS with need of a password or not. If I do have access then continue else exit the script.
I have tried many things and each has failed when testing against the release candidate as a password is expected. Here is the latest attempt:
set status [catch {exec ssh $user@$host ls} result]
if { [regexp "Password:" $result]} then {
# A password was asked for. Fail
puts "This is a Release Candidate Version\nAccess to the Linux OS is denied"
exit
} else {
# no password needed. Success
puts "This is a Pre-Release version"
}
When executed against a pre-release version this code works. But when a password is required it does not as the SSH session prompts for a password and waits for input.
Would anyone have a workaround that would break out of the required password scenario?
Thank you
Upvotes: 0
Views: 223
Reputation: 137557
If you have a case where the connection to the remote system may ask for a password, but isn't always going to, you're best of doing the connection from within expect. This is because the expect
command can be told to wait for several different things at once.
set timeout 20; # 20 seconds; if things don't respond within that, we've got problems
# 'echo OK' because it is quick and produces known output
spawn ssh $user@$host echo OK
# Wait for the possibilities we know about; note the extra cases!
expect {
"Password:" {
# Password was asked for
puts "This is a Release Candidate Version\nAccess to the Linux OS is denied"
close
exit
}
"OK" {
# Password not asked for
puts "This is a Pre-Release version"
}
timeout {
puts "There was a network problem? Cannot continue test"
close
exit 1
}
eof {
puts "Inferior ssh exited early? Cannot continue test"
close
exit 1
}
}
close
# ... now you've checked the connection ...
Upvotes: 2