Created
March 13, 2019 13:29
-
-
Save YcSmile/f4a5499fb1583cbafd5e88cb4a80356b to your computer and use it in GitHub Desktop.
[侦测局域网ssh登录IP] #SSH #局域网SSH IP #多线程
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
# [usage] | |
# detectip 192.168.1 22 username | |
# passwd | |
# 安装 paramiko | |
# sudo apt-get install libffi-dev libssl-dev libxml2-dev libxslt1-dev libjpeg8-dev zlib1g-dev | |
# pip3 install paramiko | |
import threading | |
import paramiko | |
import sys | |
import time | |
import getpass | |
# 配置参数 | |
user = '1' | |
passwd = '1' | |
addr = '1' | |
port = 0 | |
threadNum = 10 | |
ip_count = 1 # 起始地址 | |
ip_max = 254 | |
running = False # 运行标志 | |
target = [] # | |
def ShowTask(**arg): | |
global running | |
global ip_count | |
while running: | |
print('Runing {0:3}/{1:3}'.format(ip_count,ip_max),end='\r') | |
time.sleep(0.200) | |
print('\n') | |
def ThreadTask(**arg): | |
global running | |
global ip_count | |
ssh = paramiko.SSHClient() | |
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) | |
while running: | |
if ip_count > ip_max: | |
running = False | |
return | |
cur_ip = ip_count | |
ip_count += 1 | |
# 进行登陆 | |
curr_addr = addr.format(cur_ip) | |
#这行代码的作用是允许连接不在know_hosts文件中的主机。 | |
try: | |
ssh.connect(curr_addr, port, user, passwd,timeout=0.5) | |
print('[*]',curr_addr, '登陆成功') | |
target.append(curr_addr) | |
except Exception as e: | |
pass | |
# 使用Crtl+c 退出 | |
import signal | |
def signal_handler(signal,frame): | |
global running | |
running = False | |
print('You pressed Ctrl+C!') | |
def DetectIP(): | |
global threadList | |
signal.signal(signal.SIGINT, signal_handler) | |
signal.signal(signal.SIGTERM, signal_handler) | |
global running | |
global target | |
print('Quering ... ') | |
running = True | |
threadList = [] | |
# 创建子线程 | |
showThread = threading.Thread(target=ShowTask,args=()) | |
showThread.start() | |
threadList.append(showThread) | |
for t_ in range(threadNum): | |
subThread = threading.Thread(target=ThreadTask,args=()) | |
subThread.start() | |
threadList.append(subThread) | |
# 启动rd | |
for thd_ in threadList: | |
thd_.join() | |
# print('Finish') | |
for tar_ in target: | |
print("ssh -p {port} {user}@{host}".format(port = port,user =user,host = tar_)) | |
import argparse | |
if __name__ == '__main__': | |
parser = argparse.ArgumentParser() | |
parser.add_argument(dest = "host", help = "地址前3,r 192.168.1") | |
parser.add_argument(dest = "port", help = "端口") | |
parser.add_argument(dest = "user", help = "账号") | |
# parser.add_argument(dest = "passwd", help = "密码") # 不推荐,容易泄露密码 | |
paras = parser.parse_args() | |
user = paras.user | |
passwd = getpass.getpass() | |
addr = paras.host+'.{0}' | |
port = int(paras.port) | |
DetectIP() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment