Guest User

python/classes

a guest
Sep 24th, 2017
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.89 KB | None | 0 0
  1. naveen@ol4:>cat remoteconnect.py
  2. #!/usr/bin/env python
  3. import paramiko,getpass,json
  4. class remoteconnect(object):
  5. def __init__(self, server, port):
  6. self.server=server
  7. self.port=port
  8. try:
  9. f=open('/root/pythoncode/classes/mycreds.json').read()
  10. mycreds=json.loads(f)
  11. user=mycreds['user']
  12. passwd=mycreds['passwd']
  13. except:
  14. user=raw_input("please enter the username:")
  15. passwd=getpass.getpass("password:")
  16. mydata={}
  17. mydata['user']=user
  18. mydata['passwd']=passwd
  19. f=open('/root/pythoncode/classes/mycreds.json','w')
  20. x=json.dump(mydata,f,indent=4)
  21. f.close()
  22. self.user=user
  23. self.passwd=passwd
  24. def exec_command(self, command):
  25. x=paramiko.SSHClient()
  26. x.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  27. x.connect(self.server,username=self.user,password=self.passwd)
  28. stdin,stdout,stderr=x.exec_command(command)
  29. return stdout.readlines()
Add Comment
Please, Sign In to add comment