-1
if __name__ == "__main__":
    # When running on the server locally use the following commented values
    # iLO_https_url = "blobstore://."
    # iLO_account = "None"
    # iLO_password = "None"

    # When running remotely connect using the iLO secured (https://) address, 
    # iLO account name, and password to send https requests
    # iLO_https_url acceptable examples:
    # "https://10.0.0.100"
    # "https://f250asha.americas.hpqcorp.net"
    iLO_https_url = "https://10.0.0.100"
    iLO_account = "admin"
    iLO_password = "password"

    # Create a REDFISH object
    try:
        REDFISH_OBJ = RedfishObject(iLO_https_url, iLO_account, iLO_password)
    except ServerDownOrUnreachableError as excp:
        sys.stderr.write("ERROR: server not reachable or doesn't support " \
                                                                "RedFish.\n")
        sys.exit()
    except Exception as excp:
        raise excp

    ex4_reset_server(REDFISH_OBJ)
    REDFISH_OBJ.redfish_client.logout()

Above is the "login" part of the script im writing. In this part, REDFISH_OBJ = RedfishObject(iLO_https_url, iLO_account, iLO_password), I would like to replace iLO_https_url with a variable whose value would be pulled from a simple CSV file. The CSV would have 3 columns...ip, username,pwd.

I'm trying to execute the other part of the script, not shown here, on every IP in the CSV file. I need to do this in python.

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
  • You seem to know what you want to do. What prevents you from doing it? What is your question? – mkrieger1 Oct 25 '18 at 19:43
  • this one line has a hard coded URL in it.... (iLO_https_url = "https://10.0.0.100") i want to use a variable instead of the hard coded URL. A CSV file would be holding the IPs that the varible would use. Im certainly no pro. – Joshua Bittner Oct 26 '18 at 13:40

1 Answers1

0

The easiest way is to use the open() function with the split() function.

Try something like this:

with open("data.csv", encoding="utf-8") as csv_file: iLO_account, iLO_password, iLO_https_url = csv_file.readline().split(",")

If you are separating your entries with new line breaks, simply replace the ",", with a "\n"

  • 1
    bad idea: use `csv` module instead (note that the last field will have a linefeed at the end...) – Jean-François Fabre Oct 25 '18 at 19:56
  • iLO_account, iLO_password, iLO_https_url = [x.strip() for x in csv_file.readline().split(",")] if you want to remove whitespace around the csv fields. – user3148225 Oct 25 '18 at 19:58
  • @Jean-FrançoisFabre depending on the use case is the `csv` module simply overkill. If he only wants to store and safe this data for internal use this method is fine. However if those are unknown files the `csv` module is better suited. Then you also have to deal with the inconsistencies of csv files, since there is no standard for csv. – Lightmoll Oct 25 '18 at 20:07
  • I will try all these suggestions and let you all know which worked best for me... You guys are great and so timely!! Thanks so much! – Joshua Bittner Oct 26 '18 at 14:00