# Auto login based on client IP Address

**URL:** <https://forum.inductiveautomation.com/t/auto-login-based-on-client-ip-address/11163>\
**Category:** Ignition\
**Created:** [February 3, 2016, 1:46pm UTC](https://forum.inductiveautomation.com/t/auto-login-based-on-client-ip-address/11163 "2016-02-03T13:46:55Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![rhettmotley](https://avatars.discourse-cdn.com/v4/letter/r/bbce88/32.png) [@rhettmotley](https://forum.inductiveautomation.com/u/rhettmotley)\
**Post date:** [February 3, 2016, 1:46pm UTC](https://forum.inductiveautomation.com/t/auto-login-based-on-client-ip-address/11163/1 "2016-02-03T13:46:55Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![nmudge](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nmudge/32/3394_2.png) [@nmudge](https://forum.inductiveautomation.com/u/nmudge)\
**Post date:** [February 3, 2016, 3:41pm UTC](https://forum.inductiveautomation.com/t/auto-login-based-on-client-ip-address/11163/2 "2016-02-03T15:41:22Z")

</div>

Hi Rhett,

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

```python
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,

---

<div class="post-metadata">

**Author:** ![rhettmotley](https://avatars.discourse-cdn.com/v4/letter/r/bbce88/32.png) [@rhettmotley](https://forum.inductiveautomation.com/u/rhettmotley)\
**Post date:** [February 7, 2016, 2:19pm UTC](https://forum.inductiveautomation.com/t/auto-login-based-on-client-ip-address/11163/3 "2016-02-07T14:19:05Z")

</div>

Holy Scripting Bat Man! Its works.

Thanks!

---

<div class="post-metadata">

**Author:** ![nmudge](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nmudge/32/3394_2.png) [@nmudge](https://forum.inductiveautomation.com/u/nmudge)\
**Post date:** [February 7, 2016, 4:26pm UTC](https://forum.inductiveautomation.com/t/auto-login-based-on-client-ip-address/11163/4 "2016-02-07T16:26:39Z")

</div>

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,

---

<div class="post-metadata">

**Author:** ![scott2](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@scott2](https://forum.inductiveautomation.com/u/scott2)\
**Post date:** [November 26, 2017, 6:28pm UTC](https://forum.inductiveautomation.com/t/auto-login-based-on-client-ip-address/11163/5 "2017-11-26T18:28:21Z")

</div>

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](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

---

<div class="post-metadata">

**Author:** ![Kyle\_Latzke](https://avatars.discourse-cdn.com/v4/letter/k/c68b51/32.png) [@Kyle\_Latzke](https://forum.inductiveautomation.com/u/Kyle_Latzke)\
**Post date:** [July 26, 2023, 4:02pm UTC](https://forum.inductiveautomation.com/t/auto-login-based-on-client-ip-address/11163/6 "2023-07-26T16:02:55Z")

</div>

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

```auto
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")

```

---

<div class="post-metadata">

**Author:** ![Tim\_Carlton](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/tim_carlton/32/52835_2.png) [@Tim\_Carlton](https://forum.inductiveautomation.com/u/Tim_Carlton)\
**Post date:** [July 26, 2023, 4:29pm UTC](https://forum.inductiveautomation.com/t/auto-login-based-on-client-ip-address/11163/7 "2023-07-26T16:29:16Z")

</div>

`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:

```auto
	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.

```auto
	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]
	[...]

```

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [July 26, 2023, 4:38pm UTC](https://forum.inductiveautomation.com/t/auto-login-based-on-client-ip-address/11163/8 "2023-07-26T16:38:29Z")

</div>

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

```auto
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"])

```

---

<div class="post-metadata">

**Author:** ![Kyle\_Latzke](https://avatars.discourse-cdn.com/v4/letter/k/c68b51/32.png) [@Kyle\_Latzke](https://forum.inductiveautomation.com/u/Kyle_Latzke)\
**Post date:** [July 26, 2023, 5:15pm UTC](https://forum.inductiveautomation.com/t/auto-login-based-on-client-ip-address/11163/10 "2023-07-26T17:15:12Z")

</div>

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