84 lines
4.7 KiB
Python
84 lines
4.7 KiB
Python
# External deps
|
|
import paramiko
|
|
import logging
|
|
from time import sleep
|
|
|
|
# Local deps
|
|
import listServers
|
|
|
|
# Debugging stuffs
|
|
logging.basicConfig()
|
|
logging.getLogger("paramiko").setLevel(logging.DEBUG)
|
|
|
|
# Creds
|
|
hostname = '172.23.224.40'
|
|
sshuser = 'Administrator'
|
|
sshkeylocation = '/home/jc/.ssh/id_rsa'
|
|
|
|
paramiko.transport.Transport._preferred_keys += ('ssh-dss',) # Allow the insecurities
|
|
ssh = paramiko.SSHClient() # Alias
|
|
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) # Ignore host key REMOVE ME (though it doesn't kind of matter anyways)
|
|
|
|
ssh.connect(hostname, username=sshuser, key_filename=sshkeylocation) # Attempt to connect
|
|
channel = ssh.invoke_shell() # Get a new shell(, and keep it open, since we need to exec multiple commands depending on the last command being executed successfully.)
|
|
|
|
def ssh_runcmd(command): # ssh_runcmd <command>
|
|
# Input validation
|
|
if not type(command) is str:
|
|
raise TypeError("ssh_runcmd: command must be a string, '" + command + "' is not.")
|
|
|
|
channel.send(command + '\n') # Execute command.
|
|
|
|
while not channel.recv_ready(): # Wait for output to be recieved.
|
|
sleep(0.5) # The command could still be writing to output buffer after running for 0.5s. Assuming during the period we read the buffer, cmd can keep up with us, or at least till the 8th KiB end.
|
|
|
|
cmdout = "" # Init
|
|
while channel.recv_ready() or not cmdout[-2:] == '> ': # Fetch new output, if there is any or look for '> ' (prompt ready). The latter is useful when connecting to blades.
|
|
sleep(0.5)
|
|
cmdout += channel.recv(65536).decode("ascii") # Get output, capped to 64KiB, format ascii. | Must get, otherwise next command will get this command's output.
|
|
breakpoint()
|
|
cmdout = cmdout.split('\n')[2:-3] # Split to a list, per newlines. Remove first 2, and last 3 lines.
|
|
#cmdout = '\n'.join(cmdout) # DO NOT UNCOMMENT THIS, HERE FOR ONLY REFERENCE TO JOIN THE LINES BACK.
|
|
return(cmdout) # Return list of output stuff.
|
|
|
|
### MAIN ###
|
|
|
|
logging.debug(ssh_runcmd('show date')) # Get rid of motd, init for next cmds. This is better than indefinitely reading buffer before any command as to counter this.
|
|
|
|
# Get a list of blades
|
|
# TESTING DATA IN USE
|
|
# serverName = ssh_runcmd('show server names')
|
|
serverName = ['Bay Server Name Serial Number Status Power UID Partner\r', '--- ------------------------------------------------- --------------- -------- ------- --- -------\r', ' 1 tty-lab-1 OK On Off \r', ' 2 tty-lab-2 CZ320263P9 OK On Off \r', ' 3 tty-lab-3 CZJ14410KP Failed On Off \r', ' 4 kspve1 CZJ14410KK OK On Off \r', ' 5 kspve2-2 OK On Off \r', ' 6 kspve3 OK On Off \r', ' 7 plaes-blade OK On Off \r', ' 8 Ringly-01 CZ3402Y48C OK On Off \r', ' 9 toomas-lepik CZ3217FNYE OK On Off \r', ' 10 toomas-lepik2 CZ3217FFSS OK On Off \r', ' 11 [Absent] \r', ' 12 erki-naumanis OK On Off \r', ' 13 [Absent] \r', ' 14 [Absent] \r', ' 15 [Absent] \r', ' 16 [Absent] \r']
|
|
servers = listServers.listServers(serverName)
|
|
|
|
baysInUse = [x[0] for x in servers] # List of blades in use.
|
|
baysInUseCount = len(baysInUse) # How many bays in use.
|
|
logging.info("There are " + str(baysInUseCount+1) + "servers presenet.") # Further optimizations could be made by not connecting to servers, what are turned off.
|
|
|
|
# TESTING DATA IN USE
|
|
baysInUseCount = 1
|
|
|
|
for b in range(1, baysInUseCount+1):
|
|
logging.info("Accessing server " + str(b))
|
|
logging.debug( ssh_runcmd('connect server ' + str(b)) ) # Use the enc as a jump host.
|
|
sleep(7)
|
|
powerInfoTmp = ssh_runcmd('show system1/oemhp_power1') # Get the data
|
|
print(powerInfoTmp)
|
|
|
|
logging.debug(ssh_runcmd('exit')) # Exit the blade's iLO, return to the enc's iLO SSH interface.
|
|
breakpoint()
|
|
|
|
breakpoint()
|
|
|
|
#print()
|
|
|
|
# ServerList = re.sub('(Banana)', r'\1Toothpaste', oldstring)
|
|
|
|
# connect server n
|
|
# show system1/oemhp_power1
|
|
# exit
|
|
|
|
# End sesion.
|
|
logging.debug(ssh_runcmd('exit'))
|
|
ssh.close()
|