【SYS_CONTEXT】使用SYS_CONTEXT获取连接到数据库服务器的IP地址

使用SYS_CONTEXT可以获得很多数据库信息,官方文档参考链接如下:
http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/functions165.htm#SQLRF06117

这里给大家演示如何使用SYS_CONTEXT获取连接到数据库服务器的IP地址。

1.使用SYS_CONTEXT获取IP地址用法
以Windows操作系统的sqlplus客户端连接secdb数据库服务器为例。

1)使用ipconfig命令获取本地IP地址
C:\Users\secooler>ipconfig

Windows IP 配置


以太网适配器 本地连接 3:

   媒体状态  . . . . . . . . . . . . : 媒体已断开
   连接特定的 DNS 后缀 . . . . . . . :

以太网适配器 本地连接 2:

   连接特定的 DNS 后缀 . . . . . . . : iata.org
   本地链接 IPv6 地址. . . . . . . . : fe80::4012:497d:a9d9:bca%20
   IPv4 地址 . . . . . . . . . . . . : 10.142.8.173
   子网掩码  . . . . . . . . . . . . : 255.255.255.192
   默认网关. . . . . . . . . . . . . :

本地IP地址为10.142.8.173

2)使用sqlplus命令连接secdb服务器,获取客户端的IP地址。
C:\Users\secooler>sqlplus sec/sec@secdb

SQL*Plus: Release 10.2.0.3.0 - Production on 星期三 4月 6 21:32:00 2011

Copyright (c) 1982, 2006, Oracle.  All Rights Reserved.


连接到:
Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production
With the Partitioning, OLAP and Data Mining options

SQL> select sys_context('userenv','ip_address') from dual;

SYS_CONTEXT('USERENV','IP_ADDRESS')
------------------------------------------------------------
10.142.8.173

如上所示,返回的IP地址信息与本地IP地址信息一致。
查询成功。

2.使用SYS_CONTEXT获取IP地址为空的问题及原因
1)问题现象
我们在服务器端直接连接特定用户secooler,然后尝试获取IP地址。
ora10g@secdb /home/oracle$ sqlplus secooler/secooler

SQL*Plus: Release 10.2.0.1.0 - Production on Thu Apr 7 22:21:41 2011

Copyright (c) 1982, 2005, Oracle.  All rights reserved.


Connected to:
Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production
With the Partitioning, OLAP and Data Mining options

secooler@ora10g> select sys_context('userenv','ip_address') from dual;

SYS_CONTEXT('USERENV','IP_ADDRESS')
------------------------------------------------------------


注意,此时上面的SQL并没有返回任何内容,即内容为NULL。

2)问题原因
这是由于SYS_CONTEXT无法获取非TCP连接信息的缘故。

3)重新使用TCP方式(即使用“@连接串”方式连接)进行连接secooler用户,在此尝试获取IP地址
ora10g@secdb /home/oracle$ sqlplus secooler/secooler@ora10g

SQL*Plus: Release 10.2.0.1.0 - Production on Thu Apr 7 22:25:44 2011

Copyright (c) 1982, 2005, Oracle.  All rights reserved.


Connected to:
Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production
With the Partitioning, OLAP and Data Mining options

secooler@ora10g> select sys_context('userenv','ip_address') from dual;

SYS_CONTEXT('USERENV','IP_ADDRESS')
------------------------------------------------------------
144.194.192.183

OK,此时IP地址获取成功。

3.小结
使用SYS_CONTEXT可以获取很多有用的数据库信息。
在使用这个方法时对其本身的限制需要做到心中有数,避免不必要的疑惑。

Good luck.

secooler
11.04.06

-- The End --

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/519536/viewspace-691814/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/519536/viewspace-691814/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当然可以!以下是一个示例代码,展示了何使用Python语言构建一个类来实现通过SNMP采集设备信息并存储到MySQL数据库中: ```python import mysql.connector from pysnmp.hlapi import * class SNMPDeviceCollector: def __init__(self, host, community): self.host = host self.community = community self.db = mysql.connector.connect( host="localhost", user="yourusername", password="yourpassword", database="yourdatabase" ) def collect_device_info(self): # 创建SNMP请求 g = getCmd( SnmpEngine(), CommunityData(self.community), UdpTransportTarget((self.host, 161)), ContextData(), ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysName', 0)), ObjectType(ObjectIdentity('SNMPv2-MIB', 'ipAdEntAddr')) ) # 发送SNMP请求并处理响应 error_indication, error_status, error_index, var_binds = next(g) if error_indication: print(f"SNMP error: {error_indication}") return if error_status: print(f"SNMP error: {error_status.prettyPrint()} at {error_index and var_binds[int(error_index)-1][0] or '?'}") return # 解析SNMP响应并存储到数据库 sys_name = var_binds[0][1] ip_address = var_binds[1][1] config = self.get_device_config() cursor = self.db.cursor() sql = "INSERT INTO devices (name, ip_address, config) VALUES (%s, %s, %s)" val = (sys_name, ip_address, config) cursor.execute(sql, val) self.db.commit() def get_device_config(self): # 在此处实现获取设备配置的逻辑 # 可以使用pysnmp或其他工具来获取配置信息 # 返回设备配置字符串 pass # 使用示例 collector = SNMPDeviceCollector("192.168.1.1", "public") collector.collect_device_info() ``` 请注意,上述代码只是一个示例,需要根据实际情况进行适当修改。你需要安装并导入 `mysql.connector` 和 `pysnmp` 库,以便在Python中连接MySQL数据库和执行SNMP操作。 此示例代码中的 `collect_device_info` 方法使用SNMP协议从设备中获取名称和IP地址,并调用 `get_device_config` 方法来获取设备的配置信息(请根据实际情况自行实现该方法)。然后,它将这些信息存储到MySQL数据库中。 记得在代码中替换 `yourusername`、`yourpassword` 和 `yourdatabase` 为你的MySQL数据库的实际凭据和数据库名称。 希望这个示例对你有所帮助!如有任何问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值