Guest User

naveen

a guest
Mar 3rd, 2018
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.72 KB | None | 0 0
  1. (devproject) naveen@ol4:>cat param.py
  2. #!/usr/bin/env python
  3. import paramiko
  4. import json
  5.  
  6. def runcommand(servername,user_name,user_passwd,command):
  7. ssh=paramiko.SSHClient()
  8. ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  9. ssh.connect(servername,username=user_name,password=user_passwd)
  10. stdin,stdout,stderr=ssh.exec_command(command)
  11. outcome=stdout.readlines()
  12. ssh.close()
  13. return outcome
  14.  
  15. def main():
  16. f=open("creds.json","r")
  17. mydata=json.load(f)
  18. servername=mydata['servername']
  19. user_name=mydata['user_name']
  20. user_passwd=mydata['user_password']
  21. print "loaded the credentials from the json file"
  22. print "server name is {}".format(servername)
  23. print "user name is {}".format(user_name)
  24. print "user password is {}".format(user_passwd)
  25. print "taking connection to {}".format(servername)
  26. print "will be running the below command on {}".format(servername)
  27. cmd="hostnae"
  28. print cmd
  29. ssh=paramiko.SSHClient()
  30. ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  31. ssh.connect(servername,username=user_name,password=user_passwd)
  32. stdin,stdout,stderr = ssh.exec_command(cmd)
  33. ssh.close()
  34. print "output of the command is "
  35. if stdout.readlines():
  36. print stdout.readlines()
  37. else:
  38. print "command entered is wrong"
  39. newcmd=raw_input("enter a new command:")
  40. outcome=runcommand(servername,user_name,user_passwd,newcmd)
  41. print "new command {} output is {}".format(newcmd, outcome)
  42.  
  43.  
  44. if __name__=="__main__":
  45. main()
  46. (devproject) naveen@ol4:>cat creds.json
  47. {
  48. "servername": "devbox.localdomain",
  49. "user_name": "root",
  50. "user_password": "redhat"
  51. }(devproject) naveen@ol4:>
Add Comment
Please, Sign In to add comment