· 7 years ago · Aug 06, 2018, 09:20 AM
1#!/usr/bin/python
2# -*- coding: utf-8 -*-
3import getpass
4import telnetlib
5from threading import Thread
6import subprocess
7from Queue import Queue
8import sys, os, os.path, socket
9
10user = "admin"
11#password = getpass.getpass() #ÑпроÑит пароль через ввод вручную
12password = "admin"
13HOST = "192.168.1.1"
14
15num_threads = 4
16queue = Queue()
17#ips = ["olololdddd", "google.com", "mail.ru", "yandex.ru"]
18ips = ["lala"]
19#wraps system ping command
20def pinger(i, q):
21 """Пингуем адреÑа"""
22 while True:
23 ip = q.get()
24 print "Thread %s: Pinging %s" % (i, ip)
25 ret = subprocess.call("ping -c 1 %s" % ip,
26 shell=True,
27 stdout=open('/dev/null', 'w'),
28 stderr=subprocess.STDOUT)
29 if ret == 0:
30 print "%s: работает." % ip
31 else:
32 print "%s: не отвечает" % ip
33 print "now we try to telnet"
34 tn = telnetlib.Telnet(HOST)
35 tn.read_until("login: ")
36 tn.write(user + "\n")
37 if password:
38 tn.read_until("Password: ")
39 tn.write(password + "\n")
40
41 tn.write("ls\n")
42 tn.write("netstat -nl\n")
43 tn.write("exit\n")
44 print tn.read_all()
45 q.task_done()
46
47
48#Spawn thread pool
49for i in range(num_threads):
50
51 worker = Thread(target=pinger, args=(i, queue))
52 worker.setDaemon(True)
53 worker.start()
54#Place work in queue
55for ip in ips:
56 queue.put(ip)
57#Wait until worker threads are done to exit
58queue.join()