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

作者:源神 时间:2023-07-29 09:39:52 

1、首先模拟python类似shell命令行操作的接口:

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

2、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)

补充知识:python操作有Kerberos认证的hive库

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

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

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

代码


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实现Kerberos用户的增删改查操作

以上为个人经验,希望能给大家一个参考,也希望大家多多支持

来源:https://blog.csdn.net/ZhouyuanLinli/article/details/78617484

标签:Python,Kerberos
0
投稿

猜你喜欢

  • Div的浮动+循环(描述的不清楚,请看图)

    2008-09-22 20:21:00
  • Python实现京东秒杀功能代码

    2021-08-14 15:41:27
  • mysql如何跨时区迁移数据

    2010-03-25 10:26:00
  • asp如何在网上查找链接?

    2010-06-22 21:10:00
  • Tensorflow卷积实现原理+手写python代码实现卷积教程

    2021-12-21 20:12:52
  • MYSQL自定义函数判断是否正整数的实例代码

    2024-01-19 19:14:20
  • 我的论坛源代码(九)

    2023-11-15 05:50:05
  • FrontPage 2000主页设计问与答

    2008-08-02 12:37:00
  • Mysql支持的数据类型(列类型总结)

    2024-01-28 01:31:31
  • 慢慢的网页

    2009-11-12 12:53:00
  • 3个常用的JS时间代码

    2009-03-22 15:29:00
  • Javascript:window对象出身何处

    2007-08-28 15:16:00
  • python命名空间(namespace)简单介绍

    2022-06-21 23:26:53
  • Oracle 10g 服务器端安装预备步骤(详细图文教程)

    2024-01-13 07:39:57
  • 分享一个超好用的php header下载函数

    2023-09-03 21:31:43
  • Keras官方中文文档:性能评估Metrices详解

    2023-07-15 00:27:40
  • golang 调用c语言动态库方式实现

    2024-05-22 17:51:02
  • js修改原型的属性使用介绍

    2024-04-10 10:51:03
  • 有感用户体验规划与系统实现

    2009-11-27 18:33:00
  • php隐藏IP地址后两位显示为星号的方法

    2023-08-16 13:05:17
  • asp之家 网络编程 m.aspxhome.com