如何使用paramiko

时间:2015-08-01 09:20:03

标签: python ssh

我使用的是需要root访问权限的Python脚本。

首先,我成为normal用户,然后我想成为root。但是,stdin.write仍在抛出错误。

这是我的剧本:

import os
import paramiko
import subprocess

user="a554511"
p="xxxxxxx"
root="abcdefgh"
x="vljhggsajgfj"
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(x,username=user,password=p)
stdin.write('su -')

stdin.write("\n")
stdin.write(root)
stdin.flush()
stdin,stdout,stderr=ssh.exec_command('pwd')

x=stdout.readlines()

print(x)
ssh.close()

我得到的错误是:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.6/site-packages/paramiko-1.15.2-py2.6.egg/paramiko/file.py", line 339, in write
    self._write_all(data)
  File "/usr/lib/python2.6/site-packages/paramiko-1.15.2-py2.6.egg/paramiko/file.py", line 456, in _write_all
    count = self._write(data)
  File "/usr/lib/python2.6/site-packages/paramiko-1.15.2-py2.6.egg/paramiko/channel.py", line 1220, in _write
    self.channel.sendall(data)
  File "/usr/lib/python2.6/site-packages/paramiko-1.15.2-py2.6.egg/paramiko/channel.py", line 744, in sendall
    sent = self.send(s)
  File "/usr/lib/python2.6/site-packages/paramiko-1.15.2-py2.6.egg/paramiko/channel.py", line 698, in send
    return self._send(s, m)
  File "/usr/lib/python2.6/site-packages/paramiko-1.15.2-py2.6.egg/paramiko/channel.py", line 1058, in _send
    raise socket.error('Socket is closed')
socket.error: Socket is closed

0 个答案:

没有答案