Hello, I have 12 remote iMac’s that would like to proactively check on their bluetooth keyboard’s, mice and trackpad’s health. Ideally I’d like to create a script that run’s every morning and output’s somehow(?) but don’t know how. The script I have so far is here:
Thanks for this but I’m fairly new to scripting and still unsure what I need to input? I need to ssh to 12 iMacs and run the same battery check script on each but I can’t see how?
If I manually do the commands in my example to one iMac I get this:
“The authenticity of host ‘usernameimac04.home (192.168.1.XX)’ can’t be established.
RSA key fingerprint is 68:14:ce:18:ec:23:de:d0 etc, etc.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added ‘usernameimac04.home,192.168.1.XX’ (RSA) to the list of known hosts.
Password:”
I enter the password and then cut/paste the battery script and it works, how can I do this multiple times in a script?
That is exactly what the expect command does in both links. It will wait for the fingerprint confirmation and send yes. Then it will wait for the Password: (or password:) prompt and send the password. Just copy the script and do an simple command like hostname to see if you got it working. The script’s is more easy in the second link, it’s a more verbose version you only have to fill in the right variables.
Thanks again for this and please bear with me as I know very little about scripting.
In your script when I attempt to amend the sshServer my understanding is that I need to input how I manually connect i.e: “ssh username@nameimac01.home” but it did not work? I am using the id from the sharing settings on each iMac is that not correct?
I understand how you are using the expect command (I think) but would I simply repeat the script for each iMac ssh connection as this would make it a very long script or can I add them all in upfront?
For that you can create handlers (routines), which will create a block of code than can be called. So when you have more than one machine I would put the ssh command itself in a handler and call it for each individual machine. Here an example of how invoking the hostname command on 10 different machines.
runTaskOnRemoteHost("remoteimac01.home", "admin", "[passwd]", "hostname")
runTaskOnRemoteHost("remoteimac02.home", "admin", "[passwd]", "hostname")
runTaskOnRemoteHost("remoteimac03.home", "admin", "[passwd]", "hostname")
runTaskOnRemoteHost("remoteimac04.home", "admin", "[passwd]", "hostname")
runTaskOnRemoteHost("remoteimac05.home", "admin", "[passwd]", "hostname")
runTaskOnRemoteHost("remoteimac06.home", "admin", "[passwd]", "hostname")
runTaskOnRemoteHost("remoteimac07.home", "admin", "[passwd]", "hostname")
runTaskOnRemoteHost("remoteimac08.home", "admin", "[passwd]", "hostname")
runTaskOnRemoteHost("remoteimac09.home", "admin", "[passwd]", "hostname")
runTaskOnRemoteHost("remoteimac10.home", "admin", "[passwd]", "hostname")
on runTaskOnRemoteHost(sshServer, sshUser, sshPasswd, theTask)
set expectScript to "set timeout 30
spawn ssh " & sshUser & "@" & sshServer & space & theTask & "
expect {
\"Password:\" {
send \"" & sshPasswd & "\\r\"
exp_continue
}
\"yes/no?\" {
send \"yes\\r\"
exp_continue
}
\"password:\" { #to support older Mac OS X systems and other ssh servers
send \"" & sshPasswd & "\\r\"
exp_continue
}
\"y/n?\" { #to support other ssh servers
send \"y\\r\"
exp_continue
}
}"
set theResult to do shell script "expect <<<" & quoted form of expectScript
end runTaskOnRemoteHost