py3.5 Instances of Telnet (batch creation of users on remote machines)

Source: Internet
Author: User

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 ') + "\ r". Encode (' Utf-8 '))
Time.sleep (Wait_time)
#tn. Read_very_eager (). Decode (' Utf-8 ')
  
Time.sleep (Wait_time)
#tn. Read_until (b ' Administrator\n\rpassword: ')
Tn.write (Password.encode (' utf-8 ') + "\ r". Encode (' Utf-8 '))

Time.sleep (Wait_time)
Tn.read_very_eager (). Decode (' Utf-8 ') #这句话的意思只是读取信息, but the withdrawal can not run successfully, do not know why, the front if not read_until, all with the method of sleep, then the note off Read_very _eager must be added to succeed.

Tn.write (Command1.encode (' utf-8 ') + "\ r". Encode (' Utf-8 '))
Time.sleep (Wait_time)

Tn.write (Command2.encode (' utf-8 ') + "\ r". Encode (' Utf-8 '))
Time.sleep (Wait_time)

A = Tn.read_very_eager ()
Tn.close ()
Print (Host + a.decode (' GBK '))
Print (' \ n ')

Solve the problem of building users, add groups, so some usage did not delve into, here the transcoding is very cumbersome, and, \ n failed to execute the success \ R can

py3.5 instances of Telnet (bulk user creation on remote machines)

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.