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_ ...
随机推荐
- Android Studio插件
awesome-android-ui(UI范例库) https://github.com/wasabeef/awesome-android-ui/tree/master/pages LMBanners ...
- 51nod1394 差和问题
我只会用线段树写...不喜欢树状数组..其实跑的也不算慢?然后各种*的时候忘了longlong一直WA...药丸! 而且我不怎么会用map离散化...那么就sort+unique #include&l ...
- Windows 8获取开发者账户
使用PowerShell获取开发者账户,可以在本地调试Metro APP C:\PS> Show-WindowsDeveloperLicenseRegistration //安装licence ...
- mysql-主从复制(二)
1)主服务器上开启binlog服务器 log-bin=mysql-bin 2)用户授权(并不是privileges授权!!!!),正确有从服务器授权如下 grant replication slave ...
- VFL示例
•[cancelButton(72)]-12-[acceptButton(50)] •取消按钮宽72point,accept按钮宽50point,它们之间间距12point • •[wideView( ...
- Android 系统属性
/************************************************************************ * Android 系统属性 * 说明: * 由于需 ...
- 判断https
判断https <script> if(window.location.protocol=='https:'){ window.location.href='http://e.abchin ...
- ora-0000 normal跟/etc/hosts有关
当hosts文件配置错误时,用sqlplus登录后startup nomount,就会报错ORA-00000 [oracle11g@testdb2 dbs]$ sqlplus "/ as s ...
- 怎样学法学?——民法学泰斗王利明教授的演讲 z
今晚我讲“怎样学习法律”,但不是讲一般的学习法学的方法,而是主要从法学.法律的特征讲起.(因为)我们学习任何东西,都首先要搞清楚我们的学习对象有什么特征.性质. 我们要了解法律.法学本身的性质,要了解 ...
- C++ 虚拟继承
1.为什么要引入虚拟继承 虚拟继承是多重继承中特有的概念.虚拟基类是为解决多重继承而出现的.如:类D继承自类B1.B2,而类B1.B2都继 承自类A,因此在类D中两次出现类A中的变量和函数.为了节省内 ...