客户端代码:
如果注释最下面三行socket发送,可以直接在本机修改密码不发送数据到服务端。
# -*- coding: utf-8 -*- #author:Guoyabin import random import string import os from socket import * serverip = ‘10.1.2.221‘ port = 9999 buffersize = 1024 addr = (serverip,port) client = socket(AF_INET,SOCK_STREAM) client.connect(addr) def Psswd(length): chars=string.ascii_letters+string.digits return ‘‘.join([random.choice(chars) for i in range(length)]) def get_wanip_info(): get_ip_cmd="/sbin/ifconfig |grep ‘inet addr‘|awk -F\: ‘{print $2}‘|awk ‘{print $1}‘ | grep -v ‘^127‘ | grep -v ‘^192‘" ethip=os.popen(get_ip_cmd).readline().strip() return ethip #密码长度 passwd = Psswd(18) username = ‘root‘ try: os.system(‘echo %s | /usr/bin/passwd --stdin %s‘ %(passwd,username)) print get_wanip_info(),‘=>‘,passwd print "root passwd change success!!!!" except: print "root passwd change error!!!!" data = str(get_wanip_info()+‘=>‘+passwd) client.send(‘%s\r\n‘ % str(get_wanip_info()+‘=>‘+passwd)) client.close()
本着方便的原则,增加服务端用来接收密码。使运维管理员更方便批量执行脚本,修改密码。
# -*- coding: utf-8 -*- #author:Guoyabin import socket import threading import time server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) #监听端口: server.bind((‘10.1.2.221‘, 9999)) server.listen(10) print(‘Waiting for connection...‘) def tcplink(sock, addr): while True: data = sock.recv(1024) time.sleep(1) if not data or data.decode(‘utf-8‘) == ‘exit‘: break #为防止恶意telnet发送数据包,增加=>验证。 if ‘=>‘ not in data: break print data savepasswd(data) sock.close() def savepasswd(passwd): now=time.strftime("%Y-%m-%d %H:%M:%S") errfile=open(‘passwd.txt‘,‘a‘) errfile.write(now+‘ ‘+str(passwd)) errfile.close() while True: # 接受一个新连接: sock, addr = server.accept() # 创建新线程来处理TCP连接: t = threading.Thread(target=tcplink, args=(sock, addr)) t.start()
本文出自 “hbbdgyb” 博客,请务必保留此出处http://hbbdgyb.blog.51cto.com/5031387/1977784
原文地址:http://hbbdgyb.blog.51cto.com/5031387/1977784