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开源项目教程] - 手把手教你使用移动跨平台开发框架Ionic开发一个新闻阅读APP
前言 这是一个系列文章,从环境搭建开始讲解,包括网络数据请求,将持续更新到项目完结.实战开发中遇到的各种问题的解决方案,也都将毫无保留的分享给大家. 关注订阅号:TongeBlog ,查看移动端跨平台 ...
- HDU 2144 (最长连续公共子列 + 并查集) Evolution
我发现我一直理解错题意了,这里的子序列指的是连续子序列,怪不得我写的LCS一直WA 顺便复习一下并查集 //#define LOCAL #include <iostream> #inclu ...
- linux下编译软件通用方法(memcached为例)
1)到软件的官网或其他网站下载软件的源码包 2)解压源码包,并切换到源码目录中 3)使用./configure --help查询配置帮助,里面可能会有安装指南(Installation directo ...
- 移植yaffs文件系统
需要下载yaffs2-d43e901.tar.gz,busybox-1.13.0.tar.bz2 使用的交叉编译器是4.33 1.修改配置编译busybox 修改Makefile CROSS_COMP ...
- 利用ICSharpCode.SharpZipLib.Zip进行文件压缩
官网http://www.icsharpcode.net/ 支持文件和字符压缩. 创建全新的压缩包 第一步,创建压缩包 using ICSharpCode.SharpZipLib.Zip; ZipOu ...
- 判断https
判断https <script> if(window.location.protocol=='https:'){ window.location.href='http://e.abchin ...
- pg psql命令
linux下使用psql命令操作数据库 下面主要用到了insert into ,pg_dump , pg_restore 命令 按步骤走 su postgres ...
- 微软官方的一段JavaScript判断.net环境
<HTML> <HEAD> <TITLE>Test for the .NET Framework 3.5</TITLE> <META HTTP-E ...
- ArcEngine9.3报错Create output feature class failed
ArcEngine9.3执行IFeatureDataConverter.ConvertFeatureClass Method出错如下错误信息: Create output feature class ...
- 被投资人“送”入看守所 z
http://news.cnblogs.com/n/506969/ 拜读了[[畅言]读<被投资人“送”入看守所>一文有感]一文有感,很想跟作者探讨几句.虽然他的看法很有理性,但站在一个刑案 ...