How to connect remotely to multiple iMac's and check battery %?

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:

ssh username@remoteimac01.home

kbatt=ioreg -c AppleBluetoothHIDKeyboard | grep BatteryPercent | awk -F"=" {'print $2'} && if [ ${#kbatt} -gt 0 ]; then echo “Bluetooth Keyboard: $kbatt%”; fi && tbatt=ioreg -c BNBTrackpadDevice | grep BatteryPercent | tail -1 | awk -F"=" {'print $2'} && if [ ${#tbatt} -gt 0 ]; then echo “Magic Trackpad Battery: $tbatt%”; fi && mbatt=ioreg -c BNBMouseDevice | grep BatteryPercent | tail -1 | awk -F"=" {'print $2'} && if [ ${#mbatt} -gt 0 ]; then echo “Mouse Battery: $mbatt %”; fi;

The command above only connects to 1 iMac so I would been some way of connecting to the other 11.

Any help, guidance would be greatly appreciated as I’m fairly new to scripting (as you can no doubt see).

Many thanks

Since you already have the script via SSH you can use ssh and expect in a shell command like here and 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.

I enter the password and then cut/paste the battery script and it works, how can I do this multiple times in a script?

Many thanks

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?

I really do appreciate the help - thank you.

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\"
   \"yes/no?\" {
       send \"yes\\r\"
   \"password:\" { #to support older Mac OS X systems and other ssh servers
       send \"" & sshPasswd & "\\r\"
   \"y/n?\" { #to support other ssh servers
       send \"y\\r\"
	set theResult to do shell script "expect <<<" & quoted form of expectScript
end runTaskOnRemoteHost