加载中…
个人资料
-渣fat人-
-渣fat人-
  • 博客等级:
  • 博客积分:0
  • 博客访问:105,598
  • 关注人气:2
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
相关博文
推荐博文
谁看过这篇博文
加载中…
正文 字体大小:

scptf

(2015-11-27 01:03:06)
分类: OSX
#! /usr/bin/env python
import os
import sys
import subprocess
##################################################

tarusr = 'qhuang'
tarsys = 'Darwin'
tarIP = 'imac'

##################################################


# local system
locsys = subprocess.Popen('uname', stdout=subprocess.PIPE).stdout.readlines()[0][:-1]
locusr = subprocess.Popen('whoami', stdout=subprocess.PIPE).stdout.readlines()[0][:-1]
if (locsys=='Darwin' or locsys=='darwin' or locsys=='DARWIN'): 
lochome = '/Users/'+locusr+'/'
else : lochome = '/home/'+locusr+'/'


# target system
if (tarusr[-1] == '/') : tarusr = tarusr[:-1]
if (tarsys=='osx' or tarsys=='OSX' or tarsys=='osX' or tarsys=='Darwin' or tarsys=='darwin' or tarsys=='DARWIN') : 
# tarfronts = ['/', 'Usrs/', tarusr+'/']
tarhome = '/Users/'+tarusr+'/'
else : 
# tarfronts = ['/', 'home/', tarusr+'/']
tarhome = '/home/'+tarusr+'/'


# current path
locpath = subprocess.Popen('pwd', stdout=subprocess.PIPE).stdout.readlines()[0][:-1]+'/'
if (locpath[:len(lochome)] == lochome) : 
tarpath = tarhome + locpath[len(lochome):] 
else : tarpath = locpath
locpath2d, tarpath2d = '', ''
for i in range(len(locpath)-2, -1, -1) : 
if (locpath[i] == '/') : 
locpath2d = locpath[:i+1]
break
for i in range(len(tarpath)-2, -1, -1) : 
if (tarpath[i] == '/') : 
tarpath2d = tarpath[:i+1]
break


# Check input arguments
a = sys.argv[1:]
if (len(a) < 2) : 
print 'missing arguments, must be "scptf -t file(s)" or "scptf -f file(s)"'
exit()
if (a[0]!='-t' and a[0]!='-f') : 
print '"scptf '+a[0]+'", wrong action, must be "scptf -t" or "scptf -f"'
exit()
act, a, sa = a[0], a[1:], ''
for i in range(len(a)) : 
if (a[i][0] == '-') : sa = sa + a[i] + ' '
else : break
a = a[i:]


# Check a
per, b, n = [], [], []
for i in range(len(a)) : 

withdir = 0
for j in range(len(a[i])-1, -1, -1) : 
if (a[i][j] == '/') : 
withdir = 1
break

if (withdir == 0) : 
b = b + [tarpath + a[i]]
a[i] = locpath + a[i]

if (a[i][:2] == './') : 
b = b + [tarpath + a[i][2:]]
a[i] = locpath + a[i][2:]
elif (a[i][:3] == '../') : 
b = b + [tarpath2d + a[i][3:]]
a[i] = locpath2d + a[i][3:]
elif (a[i][:len(lochome)] == lochome) : 
b = b + [tarhome + a[i][len(lochome):]]
elif (a[i][0] != '/') : 
b = b + [tarpath + a[i]]
a[i] = locpath + a[i]
else : b = b + [a[i]]

if (a[i][:len(lochome)] != lochome) : per = per + [0]
else : per = per + [1]

if (a[i][-1] == '/') : 
a[i] = a[i][:-1]
b[i] = b[i][:-1]
for j in range(-1, -len(a[i]), -1) : 
if (a[i][j] == '/') : 
n = n + [j+1]
break


# run command
for i in range(len(a)) : 
if (act == '-f') : 
cmd = 'scp '+sa+tarusr+'@'+tarIP+':'+b[i]+' '+a[i][:n[i]]
ai, bi = '~/'+a[i][:n[i]][len(lochome):], '~/'+b[i][len(tarhome):]
printcmd = 'scp '+sa+tarusr+'@'+tarIP+':'+bi+' '+ai
else : 
cmd = 'scp '+sa+a[i]+' '+tarusr+'@'+tarIP+':'+b[i][:n[i]]
ai, bi = '~/'+a[i][len(lochome):], '~/'+b[i][:n[i]][len(tarhome):]
printcmd = 'scp '+sa+ai+' '+tarusr+'@'+tarIP+':'+bi
if (per[i] == 0) : 
print cmd
print "  * file is out of user's directory, need root permission"
else : 
print printcmd
os.system(cmd)

0

阅读 评论 收藏 转载 喜欢 打印举报/Report
  • 评论加载中,请稍候...
发评论

    发评论

    以上网友发言只代表其个人观点,不代表新浪网的观点或立场。

      

    新浪BLOG意见反馈留言板 电话:4000520066 提示音后按1键(按当地市话标准计费) 欢迎批评指正

    新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 会员注册 | 产品答疑

    新浪公司 版权所有