Guest User

param.py

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