Created
September 17, 2022 10:17
-
-
Save fellipec/8618206d2d65f07d4523bfd59daf7b31 to your computer and use it in GitHub Desktop.
Connect via SSH to a host with multiple IPs, checking which is online first
This file contains hidden or 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
import os | |
# Try to connect to a socket | |
def isOpen(ip,port): | |
import socket | |
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
s.settimeout(2) | |
try: | |
s.connect((ip, int(port))) | |
s.shutdown(2) | |
return True | |
except: | |
return False | |
DeviceList = [ | |
("host.lan",22), | |
("host.vpn",22), | |
("host.com.br",2222), | |
] | |
isOnline = False | |
#Test every device, if any is online change the status to True | |
for (device,port) in DeviceList: | |
isOnline = isOnline or isOpen(device,port) | |
if isOnline: | |
print('fellipec@{0} -p {1}'.format(device,port)) | |
os.system('ssh fellipec@{0} -p {1}'.format(device,port)) | |
break | |
if not isOnline: | |
print("Host down") | |
os.system('pause') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment