Guest User

python/classes

a guest
Sep 10th, 2017
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.51 KB | None | 0 0
  1. naveen@ol4:>cat mycreds.json
  2. {
  3. "passwd": "redhat",
  4. "user": "root"
  5. }naveen@ol4:>cat remoteconnect.py
  6. #!/usr/bin/env python
  7. import paramiko,getpass,json
  8. class remoteconnect(object):
  9. def __init__(self, server, port):
  10. self.server=server
  11. self.port=port
  12. try:
  13. f=open('/root/pythoncode/classes/mycreds.json').read()
  14. mycreds=json.loads(f)
  15. user=mycreds['user']
  16. passwd=mycreds['passwd']
  17. except:
  18. user=raw_input("please enter the username:")
  19. passwd=getpass.getpass("password:")
  20. mydata={}
  21. mydata['user']=user
  22. mydata['passwd']=passwd
  23. f=open('/root/pythoncode/classes/mycreds.json','w')
  24. x=json.dump(mydata,f,indent=4)
  25. f.close()
  26. self.user=user
  27. self.passwd=passwd
  28. def exec_command(self, command):
  29. x=paramiko.SSHClient()
  30. x.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  31. x.connect(self.server,username=self.user,password=self.passwd)
  32. stdin,stdout,stderr=x.exec_command(command)
  33. return stdout.readlines()
  34. naveen@ol4:>cat mycreds.json
  35. {
  36. "passwd": "redhat",
  37. "user": "root"
  38. }naveen@ol4:>cat mycode.py
  39. #!/usr/bin/env python
  40. from remoteconnect import remoteconnect
  41.  
  42. def main():
  43. print "Enter the name of the server that you want to connect:"
  44. server=raw_input(":")
  45. connect=remoteconnect(server)
  46. print "Enter the command that you would like to get output from"
  47. command=raw_input(":")
  48. data_out=connect.exec_command(command)
  49. print "The ouput of that command is "
  50. print data_out
  51.  
  52.  
  53. if __name__=="__main__":
  54. main()
  55. naveen@ol4:>
Add Comment
Please, Sign In to add comment