from __future__ import unicode_literals
import pexpect
import sys
child = pexpect.spawn('ftp ftp.sjtu.edu.cn')
child.expect('(?i)root')
child.sendline('anonymous')
child.expect('(?i)password')
child.sendline('anonymous')
child.expect('ftp> ')
child.sendline('bin')
child.expect('ftp> ')
child.sendline('cd /centos/7.3.1611/cloud/x86_64/openstack-ocata/')
child.expect('ftp> ')
child.sendline('get openstack-aodh-api-4.0.0-1.el7.noarch.rpm')
child.expect('ftp> ')
sys.stdout.write(child.before)
print ("Escape character is '^]'.\n")
sys.stdout.write(child.after)
sys.stdout.flush()
child.interact()
child.sendline('bye')
child.close()
centos镜像的一个下载的ftp网站:
测试连通性(ip地址为 202.38.97.230)
参考:python自动化开发运维