Chinaunix首页 | 论坛 | 博客
  • 博客访问: 26917
  • 博文数量: 8
  • 博客积分: 10
  • 博客等级: 民兵
  • 技术积分: 60
  • 用 户 组: 普通用户
  • 注册时间: 2012-05-17 13:03
文章分类

全部博文(8)

文章存档

2013年(8)

我的朋友

分类: LINUX

2013-08-19 11:57:42


点击(此处)折叠或打开

  1. #!/usr/bin/python
  2. import sys
  3. import subprocess
  4. import getopt
  5. import pexpect
  6. import string
  7. import datetime

  8. opts,args = getopt.getopt(sys.argv[1:], "ha:l:u:")
  9. hostip = ""
  10. hostip_list = ""
  11. username = ""
  12. useruid = ""
  13. usergid = ""
  14. user_NUG_list =[["pup",107,111],["zax",903,903],["hic",901,901],["apos",889,889],["admin",888,888],["dev",1231,1102],["sear",1233,1102],["hise",1102,1102],["kan",1107,1107],["tgo",1104,1104],["rcce",1234,1102],["pe_hid",1001,1001],["pe_hig",1002,1002],["nooc",890,890],["tte_1",1271,1106],["rocs",892,892],["t551",1272,100],["clus_hk",1115,1115],["mysql",60,112],["audde",1251,1251],["ttsic",1109,1109],["ttse",1255,1255],["bank",1110,1110],["vallmall",1111,1111],["semo",1116,1116],["imo",3112,1112],["aace",1114,1114],["ahd",1121,1121],["clus",1105,1105],["cc",1306,1306],["devbl",1235,1102],["fhpush",1213,1101],["hiud",1101,1101],["himg",1103,1103],["hiro",2103,1103],["iyoumax",1112,1112],["utep",1108,1108],["ute",1243,1108],["opw",1211,1101],["ocesec",891,891],["asdsus",1305,1305],["skkge",1261,1110],["sepue",1262,1110],["db_root",2110,1110],["acefe",1237,1237],["pklsim",1236,1236],["web_admin",1263,1110],["tk_edu",1239,1239],["bi_gov",1245,1245],["test",3333,3333],["nsp_dev",1999,1999]]

  15. user_N_list = [ n for n,u,g in user_NUG_list ]
  16. user_U_list = [ u for n,u,g in user_NUG_list ]
  17. user_G_list = [ g for n,u,g in user_NUG_list ]

  18. def usage():
  19.     print "usage: %s -l hostip.list -u username\n or \nusage: %s -a hostip -u username" %(sys.argv[0],sys.argv[0])

  20. def get_userinfo(Uid,Gid):
  21.     Uid = input('Please input User UID(1000~60000): ')
  22.     Gid = input('Please input User GID(1000~60000): ')
  23.     if (isinstance(Uid,int) and isinstance(Gid,int)):
  24.         return Uid,Gid
  25.     else:
  26.         print "UID or GID was no number!!"
  27.         sys.exit()

  28. def ssh_cmd(hostip):
  29.     cmd = "groupadd -g %d %s;useradd -g %d -u %d -m %s" %(usergid,username,usergid,useruid,username)
  30.     ssh = pexpect.spawn('ssh %s "%s"' %(hostip,cmd))
  31.     ssh.sendline(cmd)
  32.     r = ssh.read()
  33.     ssh.close()
  34.     print "*** %s information:\n%s***************" %(hostip,r)
  35.     now = datetime.datetime.now().strftime('%Y-%m-%d %H:%M')
  36.     file_log = open('User_add.log','a')
  37.     file_log.write(str(hostip)+' '+str(now)+' add user:'+str(username)+' gid:'+str(usergid)+' uid:'+str(useruid)+'\n')
  38.     file_log.close()


  39. def add_user(hostip):
  40.     if hostip_list != "":
  41.         try:
  42.             f_list = open(hostip_list,'r')
  43.         except IOError,e:
  44.             print '*** file open error ',e
  45.             sys.exit()
  46.         else:
  47.             for hostip in open(hostip_list).readlines():
  48.                 ssh_cmd(hostip)
  49.     else:
  50.         ssh_cmd(hostip)


  51. if "-h" in sys.argv:
  52.         usage()
  53.         sys.exit()
  54. elif len(sys.argv) != 5 or "-u" not in sys.argv:
  55.         print "Options error"
  56.         usage()
  57.         sys.exit()


  58. for op,value in opts:
  59.     if op == "-a":
  60.         hostip = value
  61.     elif op == "-u":
  62.        username = value
  63.     elif op == "-l":
  64.         hostip_list = value

  65. if username in user_N_list:
  66.     useruid = user_U_list[user_N_list.index(username,)]
  67.     usergid = user_G_list[user_N_list.index(username,)]
  68.     add_user(hostip)
  69. else:
  70.     useruid,usergid = get_userinfo(useruid,usergid)
  71.     add_user(hostip)


阅读(638) | 评论(0) | 转发(0) |
0

上一篇:没有了

下一篇:sys_che.sh

给主人留下些什么吧!~~