#!/usr/bin/python2.7
import pexpect
import os, getpass
def ssh_command(user, host, password, command):
ssh_newkey = 'Are you sure you want to continue connecting'
child = pexpect.spawn('ssh -l %s %s %s' %(user, host, command))
i = child.expect([pexpect.TIMEOUT, ssh_newkey, 'password: '])
if i == 0: #timeout
print child.before
print "Error time out"
print child.after
return None
if i ==1 :
child.sendline('yes')
child.expect('password: ')
i = child.expect([pexpect.TIMEOUT, 'password: '])
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):
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('password:')
child.sendline(passwd)
child.expect(pexpect.EOF)
#child.interact()
#child.read()
#child.expect('$')
print "uploading"
except:
print "upload faild!"
def main1():
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
def main():
pass
if __name__ == "__main__":
pass
分享到:
相关推荐
用python 实现ssh的执行远端命令和scp的不同机器文件传输功能。代码简练易懂。
今天小编就为大家分享一篇python执行scp命令拷贝文件及文件夹到远程主机的目录方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
资源分类:Python库 所属语言:Python 资源全名:scp-0.13.2.tar.gz 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059
在两台服务器之间建立信任关系解决scp,ssh等不用输入密码等问题.files.rar
使用ssh、scp不需要密码。刚好项目遇到这个问题,学学挺好的。
通过shell脚本执行scp和ssh命令
(b) 自动化测试、自动任务均可以通过python脚本实现,工具集成解析器和调用执行,封装应用包括:linux ssh连接和scp,WinAPI、Web测试等。 (c) 可以不断扩展添加python或.net开源dll,在python脚本中调用。 (3)...
ssh子进程 ssh-subprocess是一个小的Python模块,提供进程的API,用于通过SSH远程执行命令。 该模块依赖于OpenSSH的SSH功能,并且需要非交互式(例如,公钥)身份验证。 该模块还支持SCP文件传输,以上传和下载文件...
Linux下SSH及SCP命令简单使用,如果通过ssh远程到其他linux机器、通过scp命令进行文件传输
scp -P 1000 192.168.199.10:/temp/data /temp 由于权限限制,不能免ssh密码拷贝,会要求输入B机器的密码。本想直接在bash文件中使用Pexpect 来实现自动输入密码,并scp拷贝文件,但是权限的限制导致不能安装使用...
SSH-SCP使用方法 ,如何不使用密码在服务器之间传输文件
(b) 自动化测试、自动任务均可以通过python脚本实现,工具集成解析器和调用执行,封装应用包括:linux ssh连接和scp,WinAPI、Web测试等。 (c) 可以不断扩展添加python或.net开源dll,在python脚本中调用。 (3)...
最近做的项目中有一个小功能:在python中调用scp命令 使用ssh登录,然后将指定目录中的图片上传到服务器指定目录 网上搜了很多方法都没用,最后终于碰到一个可以成功上传的,特此记录 import paramiko # 用于调用...
ssh,scp 连接远程ssh非22端口的服务器方法
Scripts目录下含多个应用例子,窗体测试(WinAPI)、Web测试(WatiN)、WebAPI测试、LinuxSSH和SCP等。 3)命令行调用支持: IronPythonTest.exe -log logPath ;执行命令后输出日志全路径,默认Log/ -py filePath ;...
-1: 强制scp命令使用协议ssh1 -2: 强制scp命令使用协议ssh2 -4: 强制scp命令只使用IPv4寻址 -6: 强制scp命令只使用IPv6寻址 -B: 使用批处理模式(传输过程中不询问传输口令或短语) -C: 允许压缩。(将-C标志...
官方离线安装包,亲测可用
官方离线安装包,亲测可用。使用rpm -ivh [rpm完整包名] 进行安装
python 实现自动远程登陆scp文件实例代码 实现实例代码: #!/usr/bin/expect if {$argc!=3} { send_user Usage: $argv0 {path1} {path2} {Password}\n\n exit } set path1 [lindex $argv 0] set path2 [lindex $...
SSH中SCP命令的使用,举例说明清楚易懂.