I am a stater in python. I want to ssh to one server, & from there needs to ssh to another & give password based on the prompt from console. & have to execute some commands in the 2nd server. I have gone through many questions related to this, but didn't worked. Please help me.
here is my code
class SSHTest():
"""
class for testing dual ssh
"""
def __init__(self,server):
self.server = server
self.cmds_status = ""
user=credentials.login["username"]
passw=credentials.login["password"]
self.ssh = get_ssh('server1', user, passw)
try:
chan=self.ssh.invoke_shell()
chan.send("ssh server2")
stdin2, stdout2, stderr2=self.ssh.exec_command("ssh server2")
buff = ''
# while not buff.endswith('\'s (yes/no)? '):
# resp = chan.recv(1)
# buff += resp
# chan.send("yes\n")
stdin2.write('yes\n')
stdin2.flush()
output = stdout2.read()
print "outpu"
print output
# while not buff.endswith('\'s password '):
# resp = chan.recv(1)
# buff += resp
# Send the password and wait for a prompt.
chan.send('secretpassword\n')
stdin2.write("secretpassword\n")
stdin2.flush()
output2=stdout2.read()
print "output2"
print output2
chan.send("command to execute")
stdin, stdout, stderr = self.ssh.exec_command("command to execute")
print self.ssh
for i in stdout.readlines():
print i
print stdout
except:
self.cmds_status = "Failed"
print "Failed to execute the command"
Iam getting failed to execute command as o/p.
Appreciate any help