0
votes

Ssh Socket Closed . Wanted an Interactive Ssh shell automation for Linux Box

    import paramiko

    ssh = paramiko.SSHClient()

    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    ##Creating Ssh Session

    ssh.connect("gfr4123408", port=22, username='rstrusr',password='Password')


    stdin,stdout,stderr = ssh.exec_command('symcfg -lockbox reset -ssv')

    #Here it asks for password and i want to write password below

    stdin.write("Password")

    stdin.write('\n')

    stdin.flush()

    output=stdout.readlines()

    print(output)
I Get the following Error

Traceback (most recent call last): File "", line 1, in stdin.write('password') File "C:\Users\venkar2\AppData\Local\Programs\Python\Python36\lib\site-packages\paramiko\file.py", line 402, in write self._write_all(data) File "C:\Users\venkar2\AppData\Local\Programs\Python\Python36\lib\site-packages\paramiko\file.py", line 519, in _write_all count = self._write(data) File "C:\Users\venkar2\AppData\Local\Programs\Python\Python36\lib\site-packages\paramiko\channel.py", line 1333, in _write self.channel.sendall(data) File "C:\Users\venkar2\AppData\Local\Programs\Python\Python36\lib\site-packages\paramiko\channel.py", line 831, in sendall sent = self.send(s) File "C:\Users\venkar2\AppData\Local\Programs\Python\Python36\lib\site-packages\paramiko\channel.py", line 785, in send return self._send(s, m) File "C:\Users\venkar2\AppData\Local\Programs\Python\Python36\lib\site-packages\paramiko\channel.py", line 1169, in _send raise socket.error('Socket is closed') OSError: Socket is closed

How can i resolve this as i have to configure for 200 + Devices ??

2
It seems like you really want fabric.wkl
I got it I used channel.send in Paramiko Module . ThanksRam

2 Answers

0
votes

Two things that you'll find useful:

  1. exec_command takes an optional argument of get_pty. You can use it like this:

    (stdin, stdout, stderr) = ssh.exec_command("sudo ls", get_pty = True)
    
  2. Throw the password into stdin, with a line return and flush to make sure it gets delivered. This ensures it receives the password if it asks (you could do something more sophisticated to check if it actually asked or not... simply throwing it in always hasn't caused problems for me, yet.)

    stdin.write('passwd' + '\n')
    stdin.flush()
    

Taken together, those should fix your sudo over paramiko issues.

0
votes

Most of the time this problem is related to Time Between sending two commands. I had this problem.when i used "time.sleep(X)" between my commands , this problem gone.