首页 » Web前端 » php实现长途更新技巧_若何运用Python批量更新做事器文件

php实现长途更新技巧_若何运用Python批量更新做事器文件

访客 2024-12-13 0

扫一扫用手机浏览

文章目录 [+]

  

  代码

php实现长途更新技巧_若何运用Python批量更新做事器文件

  importparamiko

php实现长途更新技巧_若何运用Python批量更新做事器文件
(图片来自网络侵删)

  importos

  #连接信息

  host='xxx.65.9.191'

  port=22

  username='root'

  password='root'

  #忽略的目录

  skipArry=['kai.xxxx.com','demo.xxxx.com']

  fullpathArry=[]

  currentIndex=''

  #判断文件是否存在

  defjudgeFileExist():

  globalcurrentIndex;

  currentIndex=os.getcwd()+'/Index.php'

  ifos.path.isfile(currentIndex)==False:

  print('Index文件不存在')

  exit()

  print('文件检测成功,准备连接做事器...')

  defcreatConnect():

  try:

  print('开始连接做事器...')

  s=paramiko.Transport((host,port))

  s.connect(username=username,password=password)

  sftp=paramiko.SFTPClient.from_transport(s)

  print('连接:'+host+'成功')

  returnsftp,s

  exceptExceptionase:

  print('连接做事器失落败:'+str(e))

  #

  #获取目录保存为数组

  defgetDirectory(sftp):

  print('开始获取目录...')

  sftp.chdir('/www/wwwroot')

  pathlist=sftp.listdir(path='.')

  forpathinpathlist:

  fullpath='/www/wwwroot/'+path+'/application/index/controller'

  ifpathinskipArry:

  continue

  fullpathArry.append(fullpath)

  print('目录获取完毕')

  #上传Index文件

  defuploadIndex(sftp):

  forfullpathiteminfullpathArry:

  remoteIndex=fullpathitem+'/Index.php'

  print('开始上传:'+remoteIndex)

  try:

  sftp.put(currentIndex,remoteIndex)

  try:

  sftp.file(remoteIndex)

  sftp.chmod(remoteIndex,int(\"大众775\公众,8))

  print('修正'+remoteIndex+'权限为755')

  print(fullpathitem+'上传成功')

  except:

  print(fullpathitem+'上传失落败')

  continue

  exceptExceptionase:

  print('缺点信息:'+str(e))

  continue

  if__name__==\"大众__main__\公众:

  judgeFileExist()

  sftp,s=creatConnect()

  getDirectory(sftp)

  uploadIndex(sftp)

  s.close()

  代码Show完了,开始详细阐明一波

  这个方法是检测我当前目录下有没有Index.php这个文件,如果没有的话就直接退出不进行下一步了,这里有个小坑,便是你Index.php这个文件名,你写小写的index.php,也能为True,这里有个要把稳的地方,便是要修正currentIndex的值,必须在前面加上global,否则还是为空

  defjudgeFileExist():

  globalcurrentIndex;

  currentIndex=os.getcwd()+'/Index.php'

  ifos.path.isfile(currentIndex)==False:

  print('Index文件不存在')

  exit()

  print('文件检测成功,准备连接做事器...')

  这是连接做事器并创建SFTP,利用了Try来捕获非常缺点

  defcreatConnect():

  try:

  print('开始连接做事器...')

  s=paramiko.Transport((host,port))

  s.connect(username=username,password=password)

  sftp=paramiko.SFTPClient.from_transport(s)

  print('连接:'+host+'成功')

  returnsftp,s

  exceptExceptionase:

  print('连接做事器失落败:'+str(e))

  这里便是实行操作命令了,利用sftp工具来操作,sftp.chdir是用于切换目录,相称于shell命令的cd/www/wwwroot

  sftp.listdir(path='.')是返回当前目录下的文件夹,且因此数组形式返回,然后将其拼接成完全路径后再保存在本地数组里备用,这里有个ifin是用来跳过一些网站目录,比如我xxx.demo.com这个目录不想更新,就在开头的SkipArry里写上,用来跳过

  defgetDirectory(sftp):

  print('开始获取目录...')

  sftp.chdir('/www/wwwroot')

  pathlist=sftp.listdir(path='.')

  forpathinpathlist:

  fullpath='/www/wwwroot/'+path+'/application/index/controller'

  ifpathinskipArry:

  continue

  fullpathArry.append(fullpath)

  print('目录获取完毕')

  这里便是关键的上传部分了,首先遍历出我们须要修正的文件夹目录,后面拼接上须要修正的文件Index.php形发展途做事器的文件路径,然后利用sftp.put函数来上传我们的文件,第一个参数是本地文件的路径,第二个参数是远程做事器上的路径,上传成功后利用sftp.file来验证该文件是否存在,实在这里我是做了个备份处理的(有点bug就暂时先注释掉了),先将原来的Index.php改名为BackIndex.php在上传新的Index.php,这个判断函数才有用,不然我这样写没啥用,由于上没上传成功肯定都会存在一个Index.php文件.上传好了之后利用sftp.chmod方法来改变该文件的权限为755,这里有个坑,你直接在第二个参数写755,会创造天生的文件权限为363,经由多次试验创造,第二个参数要传入8进制的755,也便是493,天生的权限便是755了,觉得有点坑爹。

  defuploadIndex(sftp):

  forfullpathiteminfullpathArry:

  remoteIndex=fullpathitem+'/Index.php'

  print('开始上传:'+remoteIndex)

  try:

  sftp.put(currentIndex,remoteIndex)

  try:

  sftp.file(remoteIndex)

  sftp.chmod(remoteIndex,int(\"大众775\公众,8))

  print('修正'+remoteIndex+'权限为755')

  print(fullpathitem+'上传成功')

  except:

  print(fullpathitem+'上传失落败')

  continue

  exceptExceptionase:

  print('缺点信息:'+str(e))

  continue

  然后在main里依次实行,就能将做事器上对应的目录下的文件全部更换成我本地的文件了,代码不多,但效果好使。

  以上便是这篇文章的全部内容了,希望本文的内容对大家的学习或者事情具有一定的参考学习代价。

转发此文+关注 并私信

不定期分享干货,欢迎初学和进阶中的小伙伴!

标签:

相关文章

access掉败php技巧_PHP 运用类

类是变量与浸染于这些变量的函数的凑集。工具:实际存在该类事物中每个实物的个体。$a =new User( ; 实例化后的$a。创建...

Web前端 2024-12-15 阅读0 评论0

php其他来路技巧_实例讲解防盗链技能

盗链的定义此内容不在自己做事器上,而通过技能手段,绕过别人放广告有利益的终极页,直接在自己的有广告有利益的页面上向终极用户供应此内...

Web前端 2024-12-15 阅读0 评论0