將linux系統使用者匯入mysql表

來源:互聯網
上載者:User

標籤:mysq   use   功能   nes   exce   split   final   charset   import   

下面這個程式實現的一個很簡單的功能,讀取passwd檔案,將裡面的使用者資訊寫入到mysql裡面,

具體代碼如下:

 

 1 #!/usr/bin/python 2  3 import pymysql 4 import time 5  6  7 ‘‘‘ 8  9 CREATE TABLE `os_user` (10   `user` varchar(20) NOT NULL,11   `haspass` char(5) NOT NULL,12   `uid` int(10) NOT NULL,13   `gid` int(10) NOT NULL,14   `descuser` varchar(100) NOT NULL,15   `homedir` varchar(100) NOT NULL,16   `loginshell` varchar(100) NOT NULL,17   PRIMARY KEY (`uid`),18   UNIQUE KEY `unique_var` (`user`,`uid`)19 ) ENGINE=InnoDB DEFAULT CHARSET=utf8;20 21 22 ‘‘‘23 24 try:25     conn = pymysql.connect(host=‘127.0.0.1‘,unix_socket=‘/tmp/mysql.sock‘,26                            user=‘root‘,passwd=‘123456‘,db=‘pydb‘)27     cur = conn.cursor()28     f = open(‘passwd‘)29 30     try:31         for i in f.readlines():32             Tmplist = list(i.strip(‘\n‘).split(‘:‘))33             sql = ‘INSERT INTO os_user 34                   (user,haspass,uid,gid,descuser,homedir,loginshell) values 35                   (\‘%s\‘,\‘%s\‘,%s,%s,\‘%s\‘,\‘%s\‘,\‘%s\‘)‘ % 36                   (Tmplist[0],Tmplist[1],Tmplist[2],Tmplist[3],Tmplist[4],Tmplist[5],Tmplist[6])37             cur.execute(sql)38             time.sleep(1)39             conn.commit()40             print ‘Create user %s Successful..‘ % Tmplist[0]41     except pymysql.err.IntegrityError as e:42          print ‘User %s exits.‘ % Tmplist[0]43          print ‘Please check, Exit...‘ 44     except Exception as e:45          print ‘Error : %s ‘ % e46 47 except Exception as e:48      print ‘Error : %s ‘ % e49 50 51 finally:52     cur.close()53     conn.close()54     f.close()

 

將linux系統使用者匯入mysql表

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.