python paramiko sftp连接问题 20
>>>sftp=paramiko.SFTPClient.from_transport(t)Traceback(mostrecentcalllast):File"<stdi...
>>> sftp = paramiko.SFTPClient.from_transport(t)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/site-packages/paramiko/sftp_client.py", line 132, in from_transport
return cls(chan)
File "/usr/lib/python2.7/site-packages/paramiko/sftp_client.py", line 99, in __init__
server_version = self._send_version()
File "/usr/lib/python2.7/site-packages/paramiko/sftp.py", line 105, in _send_version
t, data = self._read_packet()
File "/usr/lib/python2.7/site-packages/paramiko/sftp.py", line 177, in _read_packet
raise SFTPError('Garbage packet received')
paramiko.sftp.SFTPError: Garbage packet received
连另一台服务器没有问题,就这台一直报这个错,防火墙什么的都关了,不知道什么原因 展开
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/site-packages/paramiko/sftp_client.py", line 132, in from_transport
return cls(chan)
File "/usr/lib/python2.7/site-packages/paramiko/sftp_client.py", line 99, in __init__
server_version = self._send_version()
File "/usr/lib/python2.7/site-packages/paramiko/sftp.py", line 105, in _send_version
t, data = self._read_packet()
File "/usr/lib/python2.7/site-packages/paramiko/sftp.py", line 177, in _read_packet
raise SFTPError('Garbage packet received')
paramiko.sftp.SFTPError: Garbage packet received
连另一台服务器没有问题,就这台一直报这个错,防火墙什么的都关了,不知道什么原因 展开
1个回答
展开全部
1.SSH连接
>>> import paramiko
>>> client = paramiko.SSHClient()
>>> client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
>>> client.connect('192.168.126.128',22,username='test',password='passwd',timeout=5)
>>> stdin,stdout,stderr = client.exec_command('ls -l')
>>> for std in stdout.readlines():
print(std)
会输出家目录文件列表。
注意:如果需要连接的系统是ubuntu,直接使用root用户是登陆不了的,如果想使用root用户登陆,需要修改/etc/ssh/sshd_config
把PermitRootLogin prohibit-password改为PermitRootLogin yes
2.SFTP连接
>>> import paramiko
>>> username='test'
>>> password='Aa123456'
>>> host='192.168.126.128'
>>> port=22
>>> t=paramiko.Transport((host,port))
>>> t.connect(username=username,password=password)
>>> sftp=paramiko.SFTPClient.from_transport(t)
>>> sftp.get('/home/fangliang/Desktop/data.txt','D:\\test.txt') #下载文件
>>> sftp.put('D:\\data.txt','/home/fangliang/Desktop/data.txt') #上传文件
>>> sftp.listdir(path='/home/test/Desktop/') #列出目录结构
>>> import paramiko
>>> client = paramiko.SSHClient()
>>> client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
>>> client.connect('192.168.126.128',22,username='test',password='passwd',timeout=5)
>>> stdin,stdout,stderr = client.exec_command('ls -l')
>>> for std in stdout.readlines():
print(std)
会输出家目录文件列表。
注意:如果需要连接的系统是ubuntu,直接使用root用户是登陆不了的,如果想使用root用户登陆,需要修改/etc/ssh/sshd_config
把PermitRootLogin prohibit-password改为PermitRootLogin yes
2.SFTP连接
>>> import paramiko
>>> username='test'
>>> password='Aa123456'
>>> host='192.168.126.128'
>>> port=22
>>> t=paramiko.Transport((host,port))
>>> t.connect(username=username,password=password)
>>> sftp=paramiko.SFTPClient.from_transport(t)
>>> sftp.get('/home/fangliang/Desktop/data.txt','D:\\test.txt') #下载文件
>>> sftp.put('D:\\data.txt','/home/fangliang/Desktop/data.txt') #上传文件
>>> sftp.listdir(path='/home/test/Desktop/') #列出目录结构
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询