Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: utf-8 -*-
- # Data format
- # 64 bytes from 10.10.10.229: icmp_seq=8 ttl=64 time=263.922 ms
- from paramiko.ssh_exception import *
- f = open("ips.txt", "r")
- ips = []
- for line in f.readlines():
- ip = line.split(" ")[3]
- ips.append(ip[:len(ip)-1])
- f.close()
- import paramiko
- username = 'username'
- password = 'password'
- def create_ssh(host="localhost", username=username, password=password):
- ssh = paramiko.SSHClient()
- ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
- try:
- print "creating connection {}".format(host)
- ssh.connect(host, username=username, password=password, timeout=5)
- print "connected"
- yield ssh
- finally:
- print "closing connection {}".format(host)
- ssh.close()
- print "closed"
- for ip in set(ips):
- ssh = paramiko.SSHClient()
- ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
- try:
- print "creating connection {}".format(ip)
- ssh.connect(ip, username=username, password=password, timeout=5, look_for_keys=True)
- print "Connected!"
- ssh.close()
- raise Exception()
- except BadHostKeyException:
- print "Bad host"
- except AuthenticationException:
- print "Auth failed"
- except SSHException:
- print "Unable to establish connection {}".format(ip)
- except Exception as ex:
- if not type(ex).__name__ == 61:
- template = "An exception of type {0} occured. Arguments:\n{1!r}"
- message = template.format(type(ex).__name__, ex.args)
- else:
- message = u"Connected"
- raise Exception(message)
- print message
- finally:
- print "closing connection {}".format(ip)
- ssh.close()
- print "closed"
- #create_ssh(host=ip, username=username, password=password)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement