Skip to content

Instantly share code, notes, and snippets.

@jandahl
Last active November 10, 2016 08:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jandahl/87fe3df0543982b09852e641f3fb33a1 to your computer and use it in GitHub Desktop.
Save jandahl/87fe3df0543982b09852e641f3fb33a1 to your computer and use it in GitHub Desktop.
Search for rpis based on name, ssh to first hit; assumes 'eth' is the var that controls which interface to check arp of
#!/usr/bin/env python3
# imports
import netifaces
import os
import subprocess
def main():
eth = 'en9'
validSearchResults="pi\|air"
useUser="pi"
print('Looking for Raspberries...')
# cmd line args are in sys.argv[1], sys.argv[2] ...
# sys.argv[0] is the script name itself and can be ignored
bcaddr = netifaces.ifaddresses(eth)[2][0]['broadcast']
cmd = "/usr/sbin/arp -i " + eth + " -a | grep -e '" + validSearchResults + "' | grep -v 'incomplete'"
os.system("/sbin/ping -c 1 " + bcaddr + "> /dev/null")
try:
cmdoutput = subprocess.check_output(cmd, shell=True)
except:
print("Oops! None caught!")
exit
else:
arpline = cmdoutput.decode("utf-8")
targetraspberry = arpline[arpline.find("(") + 1:arpline.find(")")]
print("Trying " + targetraspberry)
os.system("/usr/bin/ssh " + useUser + "@" + targetraspberry)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment