Auto login based on client IP Address

I am wanting to setup an auto login based on the client IP address. Using if statements I can auto login the user and open the correct window for the client. It will become very cumbersome to keep up with as more clients are added. I have created a MySQL table setup with each clients IP address the username, login and startup window. How do I pull the info from the table to check the IP against the client IP then use the logon username, password and window?

Thanks!!
Rhett Motley

Hi Rhett,

You do a database query in your Client Startup Script. Like this:

ipAddress = system.tag.read("[System]Client/Network/IPAddress").value
query = """SELECT username, password, window 
	FROM client_users
	WHERE ipaddress=?"""
userInfo = system.db.runPrepQuery(query,[ipAddress])[0]
if system.security.switchUser(userInfo["username"],userInfo["password"]):
	system.nav.openWindow(userInfo["window"])

Best,

1 Like

Holy Scripting Bat Man! Its works.

Thanks!

1 Like

Of course it does. That is just the tip of the iceberg.

The number of things that you can do with scripting in Ignition is unlimited.

Best,

1 Like

Hi,
How are you doing the autologin for the client?
We have a multi-monitor project (7.) that utilises the AutoLogin function
(https://support.inductiveautomation.com/index.php?/Knowledgebase/Article/View/89/2/launch-project-to-multiple-monitors)
But want it to be based on IP Address.

I have tried overriding username/password properties etc, but nothing has worked so far…

Thanks
Scott

Not sure if reviving old threads in frowned upon here. Currently everybody gettings automatically logged in as "operator" but I used the code from this page to auto login technicians with matching hostnames to a higher access level. People get an index out of range error if they launch Ignition and aren't in the list.

If there a way to get around having to enter everyone's computer host names and just elevate the ones in the list? I mean it works for people that are listed in the database now but people not in the list get that "index out of range" error on the third line of this code

hostname = system.tag.read("[System]Client/Network/Hostname").value
query = """SELECT username, userpass, userwindow FROM client_list WHERE hostname=?"""
userInfo = system.db.runPrepQuery(query,[hostname])[0]

if system.security.switchUser(userInfo["username"],userInfo["userpass"]):
	print("Login Successful")

system.db.runPrepQuery(query,[hostname]) will return a PyDataset.
If there are no matches then the PyDataset will be empty.

userInfo = system.db.runPrepQuery(query,[hostname])[0] gets the first element of the PyDataset and assigns it to userInfo.

You can break it into two statements for clarity:

	queryResult = system.db.runPrepQuery(query,[hostname])
	userInfo = queryResult[0]

If the queryResult is empty then you will get an "index out of range" error.
You can check the length of the queryResult and test it to determine if there is data before attempting to use it.

	queryResult = system.db.runPrepQuery(query,[hostname])
	if len(queryResult) == 0:
		#figure out what you want to do if the hostname is not in the database and do it here
		return

	userInfo = queryResult[0]
	[...]

1 Like

Since PyDatasets returned by runPrepQuery pretend to be Python lists, you can even rely on Python's default empty-list-is-falsey behavior:

ipAddress = system.tag.read("[System]Client/Network/IPAddress").value
query = """SELECT username, password, window 
    FROM client_users
    WHERE ipaddress=?"""
queryResult = system.db.runPrepQuery(query,[ipAddress])
if queryResult:
    userInfo = queryResult[0]
    if system.security.switchUser(userInfo["username"],userInfo["password"]):
        system.nav.openWindow(userInfo["window"])
2 Likes

I used this code and it resolved my issue. Thank you both for the great suggestions.