Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (devproject) naveen@ol4:>cat param.py
- #!/usr/bin/env python
- import paramiko
- import json
- def runcommand(servername,user_name,user_passwd,command):
- ssh=paramiko.SSHClient()
- ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
- ssh.connect(servername,username=user_name,password=user_passwd)
- stdin,stdout,stderr=ssh.exec_command(command)
- outcome=stdout.readlines()
- ssh.close()
- return outcome
- def main():
- f=open("creds.json","r")
- mydata=json.load(f)
- servername=mydata['servername']
- user_name=mydata['user_name']
- user_passwd=mydata['user_password']
- print "loaded the credentials from the json file"
- print "server name is {}".format(servername)
- print "user name is {}".format(user_name)
- print "user password is {}".format(user_passwd)
- print "taking connection to {}".format(servername)
- print "will be running the below command on {}".format(servername)
- cmd="hostnae"
- print cmd
- ssh=paramiko.SSHClient()
- ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
- ssh.connect(servername,username=user_name,password=user_passwd)
- stdin,stdout,stderr = ssh.exec_command(cmd)
- ssh.close()
- print "output of the command is "
- if stdout.readlines():
- print stdout.readlines()
- else:
- print "command entered is wrong"
- newcmd=raw_input("enter a new command:")
- outcome=runcommand(servername,user_name,user_passwd,newcmd)
- print "new command {} output is {}".format(newcmd, outcome)
- if __name__=="__main__":
- main()
- (devproject) naveen@ol4:>cat creds.json
- {
- "servername": "devbox.localdomain",
- "user_name": "root",
- "user_password": "redhat"
- }(devproject) naveen@ol4:>
Add Comment
Please, Sign In to add comment