Guest User

test

a guest
Jan 27th, 2018
24
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.98 KB | None | 0 0
  1. #!/usr/bin/env python
  2. import paramiko,getpass,json
  3. class remoteconnect(object):
  4. def __init__(self, servername):
  5. self.servername=server
  6. self.value1=10
  7. try:
  8. f=open('/root/pythoncode/classes/mycreds.json').read()
  9. mycreds=json.loads(f)
  10. user=mycreds['user']
  11. passwd=mycreds['passwd']
  12. except:
  13. user=raw_input("please enter the username:")
  14. passwd=getpass.getpass("password:")
  15. mydata={}
  16. mydata['user']=user
  17. mydata['passwd']=passwd
  18. f=open('/root/pythoncode/classes/mycreds.json','w')
  19. x=json.dump(mydata,f,indent=4)
  20. f.close()
  21. self.user=user
  22. self.passwd=passwd
  23. def exec_command(self, command):
  24. print "self.value1 is holding data", self.value1
  25. x=paramiko.SSHClient()
  26. x.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  27. x.connect(self.servername,username=self.user,password=self.passwd)
  28. stdin,stdout,stderr=x.exec_command(command)
  29. return stdout.readlines()
  30. class myconnections(remoteconnect):
  31. def __init__(self, servername):
  32. self.servername=servername
  33. self.value1=20
  34. try:
  35. f=open('/root/pythoncode/classes/mycreds.json').read()
  36. mycreds=json.loads(f)
  37. user=mycreds['user']
  38. passwd=mycreds['passwd']
  39. except:
  40. user=raw_input("Please enter username:")
  41. passwd=getpass.getpass("password:")
  42. mydata={}
  43. mydata['user']=user
  44. mydata['passwd']=passwd
  45. f=open('/root/pythoncode/classes/mycreds.json','w')
  46. x=json.dump(mydata,f,indent=4)
  47. f.close()
  48. self.user=user
  49. self.passwd=passwd
  50. def execute(self, command):
  51. print "self.value1 is holding data", self.value1
  52. x=paramiko.SSHClient()
  53. x.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  54. x.connect(self.servername,username=self.user,password=self.passwd)
  55. stdin,stdout,stderr=x.exec_command(command)
  56. if len(stderr.readlines())>0:
  57. print "command entered is not valid, please re-enter a new one:"
  58. command=raw_input("Please enter new command:")
  59. stdin,stdout,stderr=x.exec_command(command)
  60. return stdout.readlines()
Add Comment
Please, Sign In to add comment