Last active
November 10, 2016 08:57
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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