Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- naveen@ol4:>cat mycreds.json
- {
- "passwd": "redhat",
- "user": "root"
- }naveen@ol4:>cat remoteconnect.py
- #!/usr/bin/env python
- import paramiko,getpass,json
- class remoteconnect(object):
- def __init__(self, server, port):
- self.server=server
- self.port=port
- try:
- f=open('/root/pythoncode/classes/mycreds.json').read()
- mycreds=json.loads(f)
- user=mycreds['user']
- passwd=mycreds['passwd']
- except:
- user=raw_input("please enter the username:")
- passwd=getpass.getpass("password:")
- mydata={}
- mydata['user']=user
- mydata['passwd']=passwd
- f=open('/root/pythoncode/classes/mycreds.json','w')
- x=json.dump(mydata,f,indent=4)
- f.close()
- self.user=user
- self.passwd=passwd
- def exec_command(self, command):
- x=paramiko.SSHClient()
- x.set_missing_host_key_policy(paramiko.AutoAddPolicy())
- x.connect(self.server,username=self.user,password=self.passwd)
- stdin,stdout,stderr=x.exec_command(command)
- return stdout.readlines()
- naveen@ol4:>cat mycreds.json
- {
- "passwd": "redhat",
- "user": "root"
- }naveen@ol4:>cat mycode.py
- #!/usr/bin/env python
- from remoteconnect import remoteconnect
- def main():
- print "Enter the name of the server that you want to connect:"
- server=raw_input(":")
- connect=remoteconnect(server)
- print "Enter the command that you would like to get output from"
- command=raw_input(":")
- data_out=connect.exec_command(command)
- print "The ouput of that command is "
- print data_out
- if __name__=="__main__":
- main()
- naveen@ol4:>
Add Comment
Please, Sign In to add comment