zoukankan      html  css  js  c++  java
  • py3.5 telnet的实例(在远程机器上批量创建用户)

    import sys
    import telnetlib
    import time

    HOST = ["172.18.217.12","172.18.217.13"]#往后一直加即可

    USER = "username"

    PASSWORD = "password"

    command1 = "net user test 1 /add" #添加用户

    command2 = "net localgroup administrators test /add"  #给将用户添加到组

    wait_time = 1

    for host in HOST:

      tn = telnetlib.Telnet(host,timeout = 10)
      #tn.read_eager().decode('utf-8')

      tn.read_until(b'login:')
      tn.write(USER.encode('utf-8') + " ".encode('utf-8'))
      time.sleep(wait_time)
      #tn.read_very_eager().decode('utf-8')
      
      time.sleep(wait_time)
      #tn.read_until(b' Administrator password: ')
      tn.write(PASSWORD.encode('utf-8') + " ".encode('utf-8'))

      time.sleep(wait_time)
      tn.read_very_eager().decode('utf-8') #这句话的意思只是读取信息,但是撤掉就不能运行成功,不知原因,前面的如果不用read_until,全部用sleep的方法,则注掉的read_very_eager都要加上才能成功

      tn.write(command1.encode('utf-8') + " ".encode('utf-8'))
      time.sleep(wait_time)

      tn.write(command2.encode('utf-8') + " ".encode('utf-8'))
      time.sleep(wait_time)

      a = tn.read_very_eager()
      tn.close()
      print(host + a.decode('gbk'))
      print(' ')

    解决了挨个建用户、加组的问题,所以一些用法并没有深究,这里的转码非常繁琐,而且, 没能执行成功 才可以

  • 相关阅读:
    串口通信
    无法安装SQL提示文件被挂起
    flash图片幻灯片浏览
    document.all与WEB标准
    在新窗口中打开页面 的asp.net后台代码
    sql 触发器
    无技术含量的正则表达式笔记
    两个FTP服务器之间传送数据
    repeater嵌套
    登录
  • 原文地址:https://www.cnblogs.com/shuchengxiang/p/6862162.html
Copyright © 2011-2022 走看看