Python implements batch password modification on linux servers and generates execl

Source: Internet
Author: User
This article mainly introduces how to implement batch password modification and generate an execl example for linux servers using python. For more information, see batch linux server password modification and generate an execl table.

The code is as follows:


#! /Usr/bin/env python
# Coding: utf8
# Randomly generate a custom length password
From random import choice
Import string, pickle

Def GenPassword (length = 8, chars = string. ascii_letters + string. digits ):
Return ''. join ([choice (chars) for I in range (length)])
Def passlist (r_user, c_user, ip_list, web_list ):
Dic = {}
For I in ip_list:
If I. split () [1] in web_list:
Dic [I. split () [1] = [I. split () [0], {r_user: GenPassword (32), c_user: GenPassword (32)}]
Else:
Dic [I. split () [1] = [I. split () [0], {r_user: GenPassword (32)}]
Return dic
Def ip_list (ip_file ):
With open (ip_file) as file:
Ip_file = file. read (). strip (). split ("\ n ")
# Web_list = [I for I in ip_file if I in web_list]
Return ip_file
Def save_dic ():
Dic = main ()
With open ("../host_message.pickle", "w") as fd:
Pickle. dump (dic, fd)
Def main ():
File = "/root/cui/script/python/ip.txt"
Web_list = [ip1, ip2, ip3, ip4]
File_list = ip_list (file)
File_dic_pass = passlist ("root", "www", file_list, web_list)
Return file_dic_pass

If _ name __= = "_ main __":
Save_dic ()

The code is as follows:


#! /Usr/bin/env python
# Coding: utf8
# Main program for batch password modification
Import pickle
From ssh_co.ssh_connect import sshd
From command. ip_passwd import save_dic, main
From execl import set_execl
Def read_dic ():
With open ("host_message.pickle", "rb") as f:
Return pickle. load (f)
Def ssh_main ():
Pid_host = 1
Host_message = read_dic ()
Host_list = host_message.keys ()
Print host_list
For host_msg in xrange (len (host_list )):
Host_attribute = (host_list [host_msg], port, user, passwd)
Ssh_conn = sshd (host_attribute, pid_host)
If "www" in host_message [host_list [host_msg] [1]. keys ():
For username in xrange (len (host_message [host_list [host_msg] [1]. keys ())):
Try:
User = host_message [host_list [host_msg] [1]. keys () [username]
Passwd = host_message [host_list [host_msg] [1] [host_message [host_list [host_msg] [1]. keys () [username]
Results = ssh_conn.set_ssh_cmd (u "/bin/echo % s |/usr/bin/passwd -- stdin % s" % (passwd, user ))
Print results, host_list [host_msg], user + "-----------" + passwd
Except t:
Print results, host_list [host_msg], user + "----- cannot ------" + passwd
Else:
User = host_message [host_list [host_msg] [1]. keys () [0]
Passwd = host_message [host_list [host_msg] [1] [host_message [host_list [host_msg] [1]. keys () [0]
Result = ssh_conn.set_ssh_cmd (u "/bin/echo % s |/usr/bin/passwd -- stdin % s" % (passwd, user ))
Print result, host_list [host_msg], user + "-----------" + passwd
If _ name _ = "_ main __":
Dic = main ()
With open ("host_message.pickle", "w") as fd:
Pickle. dump (dic, fd)
Set_execl ()
Ssh_main ()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.