Python pexpec 解决scp ssh
paswd_key = '.*assword.*' 匹配Password
ssh_newkey = '.*(yes/no).*' 匹配 Are you sure you want to continue connecting (yes/no)
- #!/usr/bin/python2.7
- import pexpect
- import os, sys, getpass
- def ssh_command(user, host, password, command):
- ssh_newkey = '.*(yes/no).*'
- passwd_key = '.*assword.*'
- child = pexpect.spawn('ssh -l %s %s %s' %(user, host, command))
- child.logfile = sys.stdout
- i = child.expect([pexpect.TIMEOUT, ssh_newkey, passwd_key])
- if i == 0: #timeout
- print child.before
- print "Error time out"
- print child.after
- return None
- if i ==1 :
- child.sendline('yes')
- i = child.expect([pexpect.TIMEOUT, passwd_key])
- if i == 0:
- print child.before
- print 'time out ERROR'
- print child.after
- return None
- child.sendline(password)
- return child
- def scp2(ip, user, passwd, dst_path, filename):
- passwd_key = '.*assword.*'
- if os.path.isdir(filename):
- cmdline = 'scp -r %s %s@%s:%s' % (filename, user, ip, dst_path)
- else:
- cmdline = 'scp %s %s@%s:%s' % (filename, user, ip, dst_path)
- try:
- child = pexpect.spawn(cmdline)
- child.expect(passwd_key)
- child.sendline(passwd)
- child.expect(pexpect.EOF)
- #child.interact()
- #child.read()
- #child.expect('$')
- print "uploading"
- except:
- print "upload faild!"
- def main():
- host = raw_input('Hostname:')
- user = raw_input('User:')
- password = getpass.getpass()
- command = raw_input('Command:')
- child = ssh_command(user, host, password, command)
- child.expect(pexpect.EOF)
- print child.before
- if __name__ == "__main__":
- main()
Python pexpec 解决scp ssh的更多相关文章
- 解决 scp 和rsync 同步失败【rsync error: unexplained error (code 255) at io.c(226) [sender=3.1.2]】
解决 scp 和rsync 同步失败 报错信息截图: 解决问题的步骤: 1.检查对方的scp和rsync 是否能使用. rsync 在使用的时候,需要客户端和服务端都有rsync工具.scp 和 rs ...
- 深入super,看Python如何解决钻石继承难题 【转】
原文地址 http://www.cnblogs.com/testview/p/4651198.html 1. Python的继承以及调用父类成员 python子类调用父类成员有2种方法,分别是普通 ...
- paip.python错误解决24
paip.python错误解决 作者Attilax 艾龙, EMAIL:1466519819@qq.com 来源:attilax的专栏 地址:http://blog.csdn.net/attilax ...
- paip.python错误解决23
paip.python错误解决 作者Attilax 艾龙, EMAIL:1466519819@qq.com 来源:attilax的专栏 地址:http://blog.csdn.net/attilax ...
- 无需输入密码的scp/ssh/rsync操作方法
一般使用scp/ssh/rsync传输文件时,都需要输入密码.下面是免密码传输文件的方法. 假设要在两台主机之间传送文件,host_src & host_dst.host_src是文件源地址所 ...
- 深入super,看Python如何解决钻石继承难题
1. Python的继承以及调用父类成员 python子类调用父类成员有2种方法,分别是普通方法和super方法 假设Base是基类 class Base(object): def __init_ ...
- (转)python通过paramiko实现,ssh功能
python通过paramiko实现,ssh功能 1 import paramiko 2 3 ssh =paramiko.SSHClient()#创建一个SSH连接对象 4 ssh.set_missi ...
- shell脚本中解决SCP命令需要输入密码的问题
使用密钥文件. 这里假设主机A(192.168.100.3)用来获到主机B(192.168.100.4)的文件. 在主机A上执行如下命令来生成配对密钥: ssh-keygen -t r ...
- (转载)深入super,看Python如何解决钻石继承难题
1. Python的继承以及调用父类成员 python子类调用父类成员有2种方法,分别是普通方法和super方法 假设Base是基类 class Base(object): def __init_ ...
随机推荐
- [ionic开源项目教程] - 第13讲 Service层优化,提取公用Service,以及生活和农业两大模块的实现
关注微信订阅号:TongeBlog,可查看[ionic开源项目]全套教程. 这一讲主要实现生活和农业两大模块的实现,在这个过程中,对service层提取出一个公用的BaseService. 这一讲分为 ...
- jQuery中filter(),not(),split()的用法
filter(),not(): <script type="text/javascript"> $(document).ready(function() { //输出 ...
- UVA 489 Hangman Judge (字符匹配)
题意:给一个字符串A,只含小写字符数个.再给一个字符串B,含小写字符数个.规则如下: 1.字符串B从左至右逐个字符遍历,对于每个字符,如果该字符在A中存在,将A中所有该字符删掉,若不存在,则错误次数+ ...
- 流程引擎的API和服务基础
RepositoryService : 管理和控制 发布包 和 流程定义(包含了一个流程每个环节的结构和行为) 的操作 除此之外,服务可以 查询引擎中的发布包和流程定义. 暂停或激活发布包,对应全部 ...
- C# 创建系统服务并定时执行【转载】
[转载]http://www.cnblogs.com/hfzsjz/archive/2011/01/07/1929898.html C# 创建系统服务并定时执行 1.新建项目 --> Windo ...
- 剑指offer—第二章算法之二分查找(旋转数组的最小值)
旋转数组的最小数字 题目:把一个数组最开始的若干元素搬到数组的末尾,我们称之为数组的旋转.输入一个递增排序的数组的一个旋转,输出旋转数组的最小元素.例如:数组{3,4,5,1,2}为{1,2,3,4, ...
- 为什么要用Java泛型
啥是泛型? 泛型(generic)是指参数化类型的能力.可以定义带泛型类型的类或方法,随后编译器会用具体的类型来代替它. 举个栗子 上述代码在编译期没有问题,但在运行期,将会报错.就是因为List的a ...
- 【转】uboot移植(一)BootLoader基本概念
原文网址:http://blog.chinaunix.net/uid-25445243-id-3869348.html 一.BootLoader简介1.1.嵌入式Linux软件结构与分布 在一般情况下 ...
- SmartWeatherAPI_Lite_WebAPI C# 获取key加密
中国气象局面向网络媒体.手机厂商.第三方气象服务机构等用户,通过 web 方式提供数据气象服务的官方载体. 在一周前已经申请到appid,但是苦于没有C#版的key 的算法,一直验证不通过,经过几天查 ...
- 实用js+css多级树形展开效果导航菜单
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...