Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- import paramiko,getpass,json
- class remoteconnect(object):
- def __init__(self, servername):
- self.servername=server
- self.value1=10
- 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):
- print "self.value1 is holding data", self.value1
- x=paramiko.SSHClient()
- x.set_missing_host_key_policy(paramiko.AutoAddPolicy())
- x.connect(self.servername,username=self.user,password=self.passwd)
- stdin,stdout,stderr=x.exec_command(command)
- return stdout.readlines()
- class myconnections(remoteconnect):
- def __init__(self, servername):
- self.servername=servername
- self.value1=20
- 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 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 execute(self, command):
- print "self.value1 is holding data", self.value1
- x=paramiko.SSHClient()
- x.set_missing_host_key_policy(paramiko.AutoAddPolicy())
- x.connect(self.servername,username=self.user,password=self.passwd)
- stdin,stdout,stderr=x.exec_command(command)
- if len(stderr.readlines())>0:
- print "command entered is not valid, please re-enter a new one:"
- command=raw_input("Please enter new command:")
- stdin,stdout,stderr=x.exec_command(command)
- return stdout.readlines()
Add Comment
Please, Sign In to add comment