python123 考试远程共享_复制远程共享文件夹内容到本地(python脚本实例)

#-*- coding:utf-8 -*-

importsys , os , redefcopyFileDir(srcFilename , desFilename):

status=Falsetry:

fileList=os.listdir(srcFilename)for eachFile infileList:

sourceF=os.path.join(srcFilename,eachFile)

targetF=os.path.join(desFilename,eachFile)ifos.path.isdir(sourceF):if notos.path.exists(targetF):

os.makedir(targetF)

status=copyFileDir(sourceF,targetF)else:

status=copyFile(sourceF,targetF)exceptException,e:print(e)

status=Falsefinally:print ('copyFileDir function is quit!')returnstatusdefcopyFile(srcFilename , desFilename):

status=False

copyCommand= 'copy %s %s'%(srcFilename,desFilename)try:if(os.popen(copyCommand)): #不用op.system(copyCommand),因为这个会弹出命令行界面

print ('copy done!')

status=Trueelse:print ('copy failed!')

status=FalseexceptException,e:print(e)

status=Falsefinally:print ('copyFile function is quit!')returnstatusdefcopyFromSharePath(srcFilename,desFilename):if notos.path.exists(srcFilename):print ('no found'+srcFilename)returnFalseif notos.path.exists(desFilename):print ('no found'+desFilename)

os.makedirs(str(desFilename))print ('create'+desFilename)

copyStatus=Falseifos.path.isdir(srcFilename):

copyStatus=copyFileDir(srcFilename,desFilename)else:

copyStatus=copyFile(srcFilename,desFilename)returncopyStatusdef main(argv =sys.argv):if not len(argv) == 3:print ('input parameters\'s count should be 3,not %s'%(len(argv)))return

print (u'脚本名字是:' +argv[0])

srcFilename= argv[1]print (u'源目录:' + argv[1])

desFilename= argv[2]print (u'目标目录:' + argv[2])ifos.path.isdir(srcFilename):ifos.path.isfile(desFilename):print ('can not copy a folder to a file')returncopyFromSharePath(srcFilename,desFilename)if __name__=='__main__':

hostIp= 'x.x.x.x'sharePath= '\\xxxxx'filename= 'xxx'resultStr=[]

resultStr.append([])

srcFilename= '\\\\' + hostIp + sharePath + '\\' +filename

desFilename= 'd:\\tmp\\pycopy\\ftp_download'cmd=['d:\\tmp\\pycopy\\copyShareFile.py',

srcFilename,

desFilename

]

main(cmd)print 'ok'

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 可以尝试使用Python的paramiko库来编写一个Python脚本,它可以实现远程连接SSH的功能。可以参考以下示例代码:import paramiko#create SSH clientssh = paramiko.SSHClient()#Auto add host keyssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())#connect to remote hostssh.connect(hostname='remote_hostname_or_ip', username='username', password='password')#run commandstdin, stdout, stderr = ssh.exec_command('ls -l')#print outputfor line in stdout.readlines(): print line#close ssh connectionssh.close() ### 回答2: 要编写一个可以远程连接SSH的Python脚本,你可以使用Paramiko库来实现。Paramiko库是一个用于SSHv2协议编程的Python实现。 首先,需要安装Paramiko库。在终端中运行以下命令来安装Paramiko库: ``` pip install paramiko ``` 然后,在Python脚本中导入Paramiko库: ```python import paramiko ``` 创建一个SSHClient对象,并连接到远程主机: ```python client = paramiko.SSHClient() client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) client.connect('远程主机IP', port=22, username='用户名', password='密码') ``` 在建立SSH连接后,你可以执行远程命令: ```python stdin, stdout, stderr = client.exec_command('远程命令') ``` 可以通过`stdout.read()`方法读取执行结果: ```python print(stdout.read().decode()) ``` 最后,记得在使用完SSH连接后关闭连接: ```python client.close() ``` 这是一个简单的示例,可以启发你编写一个脚本来远程连接SSH。当然,还有一些其他的高级用法,如上传和下载文件等,你可以查看Paramiko库的官方文档来进一步学习和了解。 ### 回答3: 要编写一个可以远程连接SSH的Python脚本,你可以借助paramiko库来实现。Paramiko是一个支持SSHv2协议的Python模块,它可以让你通过SSH连接到远程服务器,并执行命令。 首先,你需要在你的Python环境中安装paramiko库。你可以通过运行以下命令来安装它: ``` pip install paramiko ``` 接下来,你可以使用以下代码编写一个远程连接SSH的脚本: ```python import paramiko # 创建SSH客户端实例 client = paramiko.SSHClient() # 自动添加和保存远程服务器的 SSH RSA 密钥 client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) # 连接远程服务器 client.connect('your_remote_server_ip', username='your_username', password='your_password') # 执行命令 stdin, stdout, stderr = client.exec_command('your_command') # 打印命令输出结果 print(stdout.read().decode()) # 关闭连接 client.close() ``` 在这个脚本中,你需要将`your_remote_server_ip`替换为远程服务器的IP地址,`your_username`替换为登录远程服务器的用户名,`your_password`替换为密码,`your_command`替换为你希望远程执行的命令。 当你运行这个脚本时,它将会连接到远程服务器,执行指定的命令,并将命令的输出结果打印到控制台上。 但需要注意的是,为了安全起见,推荐使用SSH密钥对进行身份验证,而不是使用密码登录。你可以使用paramiko库提供的`keyfile`参数来指定SSH私钥文件。 希望对你有帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值