0

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

GuangshengZuo
  • 4,447
  • 21
  • 27
Priya v v
  • 143
  • 2
  • 3
  • 9
  • replace except statement by `except Exception as e: print e` to make it verbose and more relevant – PRMoureu Aug 19 '17 at 13:39
  • Thanks PRMoureu...now getting socket closed as exception – Priya v v Aug 19 '17 at 14:07
  • also to get more help, you should add the package you are using (or the import statements) – PRMoureu Aug 19 '17 at 14:26
  • These are the import statements, first one i am importing a class which connect to ssh & second is the python file contains the username & password ----> from ssh_connect import get_ssh, import credentials – Priya v v Aug 19 '17 at 14:33
  • maybe you can find some help here : https://stackoverflow.com/questions/20147902/how-to-know-if-a-paramiko-ssh-channel-is-disconnected – PRMoureu Aug 19 '17 at 15:07

0 Answers0