1 import paramiko
 2 import MySQLdb
 3 conn = MySQLdb.connect(host='192.168.1.101',user='root',passwd='123',db='host')
 4 cur = conn.cursor(cursorclass=MySQLdb.cursors.DictCursor)
 5 reCout = cur.execute('select pass,users,ip,name from  host,user WHERE `user`.name = "root" and `user`.id=`host`.id')
 6 nRet = cur.fetchall()
 7 conn.commit()
 8 cur.close()
 9 conn.close()
10 for i in  nRet:
11     hosts = i['ip']
12     users = i ['users']
13     passs = i['pass']
14     print hosts,users,passs
15     transport = paramiko.Transport((hosts, 22))
16     transport.connect(username=users, password=passs)
17     ssh = paramiko.SSHClient()
18     ssh._transport = transport
19     stdin, stdout, stderr = ssh.exec_command('ls /root')
20     print stdout.read()
21     transport.close()

数据表

python数据库操作对主机批量管理

2.以组的方式批量管理

import paramiko
import MySQLdb
conn = MySQLdb.connect(host='192.168.1.101',user='root',passwd='123',db='host')
cur = conn.cursor(cursorclass=MySQLdb.cursors.DictCursor)
reCout = cur.execute('select pass,users,ip,name from  host,user WHERE `user`.name = "admin" and `user`.id=`host`.id')
nRet = cur.fetchall()
conn.commit()
cur.close()
conn.close()
for i in  nRet:
    hosts = i['ip']
    #users = i ['users']
    groups =i['name']
    passs = i['pass']
    print hosts,groups,passs
    transport = paramiko.Transport((hosts, 22))
    transport.connect(username=groups, password=passs)
    ssh = paramiko.SSHClient()
    ssh._transport = transport
    stdin, stdout, stderr = ssh.exec_command('ls /home/')
    print stdout.read()
    transport.close()

数据结构

主机列表

python数据库操作对主机批量管理

 

组列表

python数据库操作对主机批量管理

组列表的id为主机列表id的外键

查询语句

select pass,users,ip,name from  host,user WHERE `user`.name = "admin" and `user`.id=`host`.id

 

相关文章:

  • 2022-02-22
  • 2021-07-10
  • 2021-08-25
  • 2022-01-28
  • 2021-08-21
  • 2021-09-10
  • 2021-12-25
  • 2021-10-29
猜你喜欢
  • 2022-12-23
  • 2023-02-08
  • 2022-12-23
  • 2021-11-20
  • 2022-01-15
  • 2022-02-24
  • 2022-12-23
相关资源
相似解决方案