Guest User

Untitled

a guest
Mar 25th, 2018
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.43 KB | None | 0 0
  1. import paramiko
  2. import telnetlib
  3.  
  4. class a_ssh:
  5. def __init__(self,ip):
  6. self.ip=ip
  7.  
  8. def command(self,komenda,login,haslo):
  9. ssh=paramiko.SSHClient()
  10. ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  11. ssh.connect(self.ip,username=login,password=haslo,look_for_keys=False)
  12. stdin, stdout, stderr = ssh.exec_command(komenda)
  13. data=stdout.readlines()
  14. return data
  15.  
  16. class a_telnet:
  17. def __init__(self,ip):
  18. self.ip=ip
  19.  
  20. def command(self,komenda,login,haslo,until=None):
  21. r=[]
  22. tn=telnetlib.Telnet(self.ip)
  23. tn.read_until("sername:",1)
  24. tn.write(login+ "\n")
  25. tn.read_until("assword:",1)
  26. tn.write(haslo+ "\n")
  27. while True:
  28. line=tn.read_until("\n",1)
  29. if len(line)>3:
  30. end=line.strip()
  31. r.append(end)
  32. break
  33. tn.write(komenda+"\n")
  34. if until is not None:
  35. while True:
  36. line = tn.read_until(until,1)
  37. r.append(line)
  38. return ''.join(r)
  39. else:
  40. while True:
  41. line = tn.read_until("\n",1)
  42. r.append(line)
  43. if end in line:
  44. break
  45. return ''.join(r)
  46.  
  47. #Examples:
  48. #a=a_ssh('10.2.2.241')
  49. #print a.command('sh version','username','password')
  50.  
  51. #b=a_telnet('10.200.0.1')
  52. #print b.command('sh running-config','username','password')
Add Comment
Please, Sign In to add comment