前言
有个小想法需要去实现,具体是扫描一个网段,找到那个开着ssh端口的ip,然后用现有的密钥连接或者用密码本连接。
涉及到的知识点
代码片段解析
整体代码
#!/usr/bin/python
# -*- coding:utf-8 -*-
import paramiko
import socket
import time
import sys
import IPy
def sshexeccmd(ip,cmd):
try:
ssh = paramiko.SSHClient()
#add to host_allow
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
# pkey = paramiko.RSAKey.from_private_key_file('/home/super/.ssh/id_rsa', password='yourpassword')
pkey = paramiko.RSAKey.from_private_key_file('c:/Users/QY/Desktop/id_rsa')
ssh.connect(hostname=ip,
port=22,
username='root',
pkey=pkey)
stdin, stdout, stderr = ssh.exec_command(cmd)
print(stdout.read())
print(stderr.read())
ssh.close()
except Exception as e:
print(e)
def portScanner(ip,port):
server = (ip,port)
sockfd = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
sockfd.settimeout(0.1)
ret = sockfd.connect_ex(server) #返回0则成功
if not ret:
sockfd.close()
print('%s:%s is opened...' % (ip,port))
return True
else:
sockfd.close()
return False
pass
if __name__ =='__main__':
if len(sys.argv) != 4:
print('python pyfile ip/24 port cmd')
sys.exit()
print('-' * 100)
print('HOST: %s' % sys.argv[1])
print('PORT: %s' % sys.argv[2])
print('CMD: %s' % sys.argv[3])
ips = IPy.IP(sys.argv[1])
print('将要扫面 %s 个IP' % len(ips))
for ip in ips:
print("/*****%s*****/" % ip.strNormal(0))
if portScanner(ip.strNormal(0),int(sys.argv[2])):
print("开始对其进行ssh处理")
sshexeccmd(ip.strNormal(0),sys.argv[3])
print("/***********************/")
# sshexeccmd(sys.argv[1],sys.argv[2])
print('-' * 100)
《爱的守护国语》泰国剧高清在线免费观看:https://www.jgz518.com/xingkong/23449.html