Python Paramiko Error
Question: I wrote a python program. I have conf file, I wrote router configuration
commands inside the conf file and I want to execute these commands inside
paramiko. I have a problem - the error message is below. Can you help me
please ?

# CODE:

    
    
    #!/usr/bin/env python
    import paramiko
    ip="10.100.1.200"
    ssh=paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    ssh.connect(ip,username="admin",password="pass")
    
    text=open("conf")
    
    for komut in text.readlines():
    
            stdin, stdout, stderror = ssh.exec_command(komut)
            for line in stdout.readlines():
                            print line.strip()
    
    
    ssh.close()
    
    text.close()
    

# error message:

    
    
    Traceback (most recent call last):
      File "./configmaker.py", line 13, in <module>
        stdin, stdout, stderror = ssh.exec_command(str(komut.strip()))
      File "/usr/lib/python2.7/dist-packages/paramiko/client.py", line 370, in exec_command
        chan = self._transport.open_session()
      File "/usr/lib/python2.7/dist-packages/paramiko/transport.py", line 662, in open_session
        return self.open_channel('session')
      File "/usr/lib/python2.7/dist-packages/paramiko/transport.py", line 764, in open_channel
        raise e
    EOFError
    


Answer: Try with this code:

    
    
    import paramiko
    
    if __name__ == "__main__":
        ip = "127.0.0.1"
        username = "admin"
        password = "root"
    
        ssh = paramiko.SSHClient()
        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        ssh.connect(ip,username=username,password=password)
        ssh_transport = ssh.get_transport()
    
        for command in ("ls /tmp", "date"):
            chan = ssh_transport.open_session()
            chan.exec_command(command)
            exit_code = chan.recv_exit_status()
            stdin = chan.makefile('wb', -1)         # pylint: disable-msg=W0612
            stdout = chan.makefile('rb', -1)
            stderr = chan.makefile_stderr('rb', -1)  # pylint: disable-msg=W0612
            output = stdout.read()
            print output
    

