2024-05-29 16:42:53 +01:00
import csv
2024-06-12 15:33:53 +01:00
import argparse
2024-05-29 16:42:53 +01:00
import paramiko
import getpass
import logging
import re
2024-06-12 15:43:42 +01:00
#Configure and commence logging
2024-05-29 16:42:53 +01:00
logger = logging . getLogger ( __name__ )
logging . basicConfig ( filename = ' python.log ' , level = logging . DEBUG )
logger . debug ( ' logging start ' )
2024-06-12 15:43:42 +01:00
#Set up argument parsing
parser = argparse . ArgumentParser ( " get-serials.py " )
parser . add_argument ( help = " This script will access every host BMC in the hosts-in.csv list and retrieve the serial number for them, writing them out to a csv file, along with the system host name for identification. " )
2024-06-12 15:23:45 +01:00
with open ( ' hosts-in.csv ' , encoding = ' utf8 ' ) as csvfile , open ( ' serials.csv ' , ' w ' ) as csvfile_out :
2024-05-29 16:42:53 +01:00
csvreader = csv . reader ( csvfile , delimiter = ' , ' )
csvwriter = csv . writer ( csvfile_out )
next ( csvreader )
2024-06-12 15:33:53 +01:00
USER_NAME = input ( " Please enter BMC username: " )
2024-05-29 16:42:53 +01:00
PASSWORD = getpass . getpass ( )
i = 1
SN_PATTERN = re . compile ( ' number= \ w {10} ' )
for row in csvreader :
print ( i )
2024-06-12 15:33:53 +01:00
BMC_NAME = ( row [ 1 ] )
2024-06-12 15:23:45 +01:00
SYSTEM_NAME = ( row [ 0 ] )
print ( BMC_NAME )
2024-05-29 16:42:53 +01:00
if i > 0 :
client = paramiko . SSHClient ( )
client . set_missing_host_key_policy ( paramiko . AutoAddPolicy ( ) )
2024-06-12 15:23:45 +01:00
client . connect ( hostname = BMC_NAME , look_for_keys = False , port = 22 , username = USER_NAME , password = PASSWORD , disabled_algorithms = { ' pubkeys ' : [ ' rsa-sha2-256 ' , ' rsa-sha2-512 ' ] } )
2024-05-29 16:42:53 +01:00
#command = ("OemHPE_licenseinstall /map1/oemHPE_license1 "+LICENSE_KEY)
command = ( " show /system1 number " )
stdin , stdout , stderr = client . exec_command ( command )
SERIAL_NUMBER_OUT = stdout . read ( )
SERIAL_NUMBER = ( str ( SN_PATTERN . findall ( str ( SERIAL_NUMBER_OUT ) ) ) [ - 12 : - 2 ] )
2024-06-12 15:23:45 +01:00
csvwriter . writerow ( [ SYSTEM_NAME , SERIAL_NUMBER ] )
2024-05-29 16:42:53 +01:00
i + = 1