0
点赞
收藏
分享

微信扫一扫

python 扫描ip以及端口,然后Telnet远程设置IP


from ipaddress import ip_address
import os
import telnetlib
import time
import subprocess
from unittest import result
import telnetlib
import threading
import telnetlib

#Telnet远程设置IP
def ipset(ip):
# 连接Telnet服务器
tn = telnetlib.Telnet(ip, 24, 10)

# 输入登录用户名a
tn.read_until(b'login:')
tn.write(b"fuwit\n")
# 输入登录密码
tn.read_until(b'password:')
tn.write(b"123456\n")
# 登录完毕后,执行tail命令
#shell="ipset 192.168.110.159 255.255.255.0 192.168.110.1"
# tn.write(b"ipset 192.168.110.159 255.255.255.0 192.168.110.1\n")

tn.write(b"ipget\n")
# ls命令执行完毕后,终止Telnet连接(或输入exit退出)
print("修改成功!")
tn.write(b'exit\r\n')
print(tn.read_all().decode('ascii'))

#扫描IP开放端口
def get_ip_status(ip,port):
server=telnetlib.Telnet()
try:
server.open(ip,port)
print('{0} port {1} is open'.format(ip, port))
ipset(ip)
except Exception as err:
#print('{0} port {1} is not open'.format(ip,port))
x=1
finally:
server.close()


#扫描ip网段
def get_port(ping_ip,ip,port):
#start_time = time.time()
fnull = open(os.devnull, 'w')
result=subprocess.call(ping_ip+" -n 2",shell=True,stdout=fnull,stderr=fnull)
#current_time=time.strftime('%Y%m%d-%H:%M:%S', time.localtime())
if result:
x=1
#print('时间:{} ip地址:{} ping fall'.format(current_time, ip))
else:
#print('时间:{} ip地址:{} ping ok'.format(current_time, ip))
get_ip_status(ip,port)
#print('程序耗时{:.2f}'.format(time.time() - start_time))
fnull.close()


if __name__ == '__main__':
threads=[]
for i in range(1,256):
ping_ip="ping 192.168.110."+str(i)
ip="192.168.110."+str(i)
port=8086
t = threading.Thread(target=get_port,args=(ping_ip,ip,port))
t.start()
threads.append(t)
for t in threads:
t.join()



# # -*- coding: utf-8 -*-

# import telnetlib

# '''Telnet远程登录:Windows客户端连接Linux服务器'''

# # 配置选项
# Host = '192.168.110.159' # Telnet服务器IP
# username = 'fuwit' # 登录用户名
# password = '123456' # 登录密码

# # 连接Telnet服务器
# tn = telnetlib.Telnet(Host, 24, 10)

# # 输入登录用户名a
# tn.read_until(b'login:')
# tn.write(b"fuwit\n")
# # 输入登录密码
# tn.read_until(b'password:')
# tn.write(b"123456\n")
# # 登录完毕后,执行tail命令
# #shell="ipset 192.168.110.159 255.255.255.0 192.168.110.1"
# tn.write(b"ipset 192.168.110.159 255.255.255.0 192.168.110.1\n")
# print("!111111111")
# # ls命令执行完毕后,终止Telnet连接(或输入exit退出)
# tn.write(b'exit\r\n')
# print(tn.read_all().decode('ascii'))

举报

相关推荐

0 条评论