Python实现Kerberos用户的增删改查操作

唐可
唐可
发布时间:2026-10-14| 5人浏览过
原创

本文介绍如何使用Python的subprocess和paramiko库封装Kerberos命令行工具,实现本地及远程用户的增删改查。重点解决Kerberos认证下Hive连接的交互问题,特别是删除Principal时处理expect交互的逻辑,并提供完整的Python代码示例。

Python模拟Shell命令行接口

首先,需要模拟类似Shell命令行操作的接口,以便在Python中执行系统命令。

在Python环境中,安装subprocess用于本地命令执行,以及paramiko用于SSH远程连接。


#-*- coding: UTF-8 -*-
#!/usr/bin/python
import os, sys
import subprocess
import paramiko
import settings
 
class RunCmd(object):
 def __init__(self):
  self.cmd = 'ls'
 
 @staticmethod
 def local_run(cmd):
  print('start executing...')
  print('cmd is -------> %s' % str(cmd))
  s = subprocess.Popen(str(cmd), shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  out, err = s.communicate()
  print("outinfo is -------> %s" % out)
  print("errinfo is -------> %s" % err)
  print('finish executing...')
  print('result:------> %s' % s.returncode)
  return s.returncode
 
 @staticmethod
 def remote_run(host, username, password, port, cmd):
  client = paramiko.SSHClient()
  client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  client.connect(hostname=host, port=int(port), username=username, password=password, timeout=5)
  stdin, stdout, stderr = client.exec_command(cmd)
  result = stdout.read()
  client.close()
  return result
 
 @staticmethod
 def krb_run(cmd):
  print('krb_run start...')
  print('cmd is -------> %s' % str(cmd))
  result = RunCmd.remote_run(settings.KRB_HOST, settings.USERNAME, settings.PASSWORD, settings.PORT, cmd)
  print('result:------> %s' % result)
  print('krb_run finish...')
  return result

Kerberos命令封装与交互处理

接下来,将Kerberos常用的命令操作封装为接口,其他操作相对简单,但删除Principal需要处理交互式输入。


 def delete_user(self, username):
  cmd = r"""
   expect -c "
   set timeout 1;
   spawn kadmin.local -q \"delete_principal {principal}\" ;
   expect yes/no {{ send \"yes\r\" }} ;
   expect *\r
   expect \r
   expect eof
   "
  """.format(principal=username)
  RunCmd.krb_run(cmd)

Kerberos认证下的Hive连接

补充知识:Python操作有Kerberos认证的Hive库。

之前访问Hive都比较简单,直接用pyhive连接即可。

但是最近遇到了一个问题,Hive有了Kerberos认证。

最终经过各种尝试和灵感迸发,终于解决了这个问题,遂记录之。

完整代码实现

代码


from pyhive.hive import connect
con = connect(host='XXXX',port=10000,auth='KERBEROS',kerberos_service_name="hive")
cursor = con.cursor()
cursor.execute('select * from tmp.pricing_calculate_result_spark where time_id="201907171355" limit 10,1')
datas = cursor.fetchall()
print(datas)
cursor.close()
con.close()

端口和ip都换成自己的,auth和kerberos_service_name不要改

运行效果

以上为个人经验,希望能给大家一个参考,也希望大家多多支持本站。如有错误或未考虑完全的地方,望不吝赐教。

相关标签:

免责声明:本文内容仅供参考,不代表本站观点。转载或引用请注明出处。

Python 相关产品和应用

全部下载
Selenium

Selenium

4.51.0 · Windows

用于浏览器自动化和 Web 测试的 Python 工具。

scikit-learn

scikit-learn

1.9.1 · Windows

常用 Python 机器学习算法与模型评估工具库。

Black

Black

26.10.1 · Windows

强调一致性的 Python 自动代码格式化工具。

Ruff

Ruff

0.17.1 · macOS

由 Rust 编写的高速 Python 代码检查和格式化工具。

pytest

pytest

9.1.1 · Windows

简洁而强大的 Python 自动化测试框架。

FastAPI

FastAPI

0.146.0 · Windows

高性能 Python API 框架,支持类型提示和自动接口文档。

Flask

Flask

3.1.3 · Windows

轻量灵活的 Python Web 应用框架。

Django

Django

6.1.2 · Windows

成熟的 Python Web 框架,适合构建内容站点和业务系统。

Spyder

Spyder

6.1.7 · Windows

面向科学计算与数据分析的 Python 集成开发环境。

SciPy

SciPy

1.18.1 · Windows

面向科学、工程和技术计算的 Python 算法库。