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_ ...
随机推荐
- 【笨嘴拙舌WINDOWS】消息机制
如果将WINDOWS比做一个人,那么他就是为你提供各种服务的巫师,他手上有各式各样,奇形怪状的奇葩物品.他脑子充满了智慧,能够为你解决你所不能解决的疑难杂症.但是他不认识你! 你从小立志要想考状元,去 ...
- HDU 1513 Palindrome
题目就是给一个字符串问最少插入多少个字符能让原字符串变为回文字符串. 算法: 用原串的长度减去原串与翻转后的串的最大公共字串的长度,就是所求答案. //#define LOCAL #include & ...
- js获得鼠标的位置
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 免费Gif图片录制工具
/************************************************************************* * 免费Gif图片录制工具 * 说明: * 最近在 ...
- Linux管道的实现机制
7.1.1 Linux管道的实现机制 在Linux中,管道是一种使用非常频繁的通信机制.从本质上说,管道也是一种文件,但它又和一般的文件有所不同,管道可以克服使用文件进行通信的两个问题,具体表现为: ...
- Mysql管理工具SQLyog
SQLyog_Enterprise 用户名:yunjian 注册码:81f43d3dd20872b6 http://download.csdn.net/detail/shel_lee/585361 ...
- 【英语】Bingo口语笔记(44) - 进餐时的表达
- win32 API 学习
SendMessage 函数原型 LRESULT SendMessage(HWND hWnd,UINT Msg,WPARAM wParam,LPARAM IParam) 详情:百度百科 msd ...
- liux环境下配置jdk
大家都知道,现在JAVA的发展可谓是如日中天,它覆盖面非常广泛,小到个人PC,大到商业应用都能见到它的身影.以前它是由SUN公司来维护的,现在已经归属到甲骨文旗下了. 今天我们来学习一下Java JD ...
- Team Foundation Server 2010下载安装配置方法
一.Team Foundation Server 2010下载: ed2k://|file|cn_visual_studio_team_foundation_server_2010_x86_x64_d ...