Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import cmd
- import paramiko
- import socket
- def ssh_check():
- with open('switch.txt', 'r') as file:
- IPs = file.readlines()
- for IP in IPs:
- try:
- ssh = paramiko.SSHClient()
- ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
- ssh.connect(IP.strip(), username= User,password=Password)
- except paramiko.SSHException as e:
- print("{} Password is invalid:{}".format(IP.strip(),e))
- IPs.remove(IP)
- except paramiko.AuthenticationException:
- print("{} Authentication failed for some reason".format(IP.strip()))
- IPs.remove(IP)
- except socket.error as e:
- print("{} Socket connection failed: {}".format(IP.strip(), e))
- IPs.remove(IP)
- return IPs
- class command(cmd.Cmd):
- prompt = 'ssh >'
- def do_run(self, command):
- hosts = ssh_check()
- for host in hosts:
- ssh = paramiko.SSHClient()
- ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
- ssh.connect(host.strip(),username = User,password=Password)
- remote_conn = ssh.invoke_shell()
- remote_conn.send(command+'\n')
- output =remote_conn.recv(5000)
- print('{} {}'.format(host,output.decode()))
- ssh.close()
- def do_bye(self, arg):
- return True
- if __name__ == "__main__":
- User = input('Enter your username:')
- Password = input('Enter your password:')
- command().cmdloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement