-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUnixHostFinder.py
71 lines (61 loc) · 1.94 KB
/
UnixHostFinder.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
from bs4 import BeautifulSoup
import urllib.request
from subprocess import call
import sys
import ssl
# Check that username was included in command line
if(len(sys.argv) < 2):
print(f"Usage: python3 {sys.argv[0]} <CS username>")
sys.exit(1)
# Use the username from the command line argument
username = str(sys.argv[1])
# URL for UTCS site with list of all Unix hosts available
url = 'https://apps.cs.utexas.edu/unixlabstatus/'
try:
# Create an SSL context to connect to UTCS's site
context = ssl._create_unverified_context()
# Read in website as a string
site = urllib.request.urlopen(url, context=context).read()
except:
print("Could not connect to UTCS Unix hosts site")
sys.exit(1)
#Create Web Scraper instance
soup = BeautifulSoup(site, 'html.parser')
#Create a list of all the hosts on the site
hosts = []
# Loop through all hosts on website
for host in soup.find_all('tr'):
#Get the text for this item
text = host.get_text()
#Split up text into an array of lines
lines = text.splitlines();
# Valid hosts take up 6 lines
if len(lines) == 6:
#Get the name of the host
name = lines[1]
# Can be either 'up' or 'down'
status = lines[2]
# Make sure this isn't the table header and that the host is 'up'
if name != 'Host' and status == 'up':
load = float(lines[5])
users = int(float(lines[4]))
hosts.append((name, users, load))
# See if we updated the default value and found a host
if len(hosts) > 0:
# Sort the hosts first by # of users, and then by load
hosts.sort(key=lambda tup: (tup[1], tup[2]))
# Try to connect this many times
NUM_ATTEMPTS = 10
for i in range(0, min(len(hosts), NUM_ATTEMPTS)):
# Current host to try
hostname = hosts[i][0]
try:
call(['ssh', username + '@' + hostname + '.cs.utexas.edu'])
sys.exit(0)
except Exception as e:
print(e)
print(f"Connection to {hostname} failed.")
print(f"Trying {hosts[i+1]} instead")
sys.exit(-1)
else:
print('No suitable host could be found.')