概述
前言:Python初学者。
脚本实现目的:
通过一台集中控制的服务器管理其他所有服务器。
主要功能有:远程执行命令、上传文件、获取文件
此脚本只能实现同时管理一台服务器,批量多进程管理多台服务器需要与另一个脚本配合实现。
脚本思路:
利用Python的paramiko模块(初步思路来源于Python入门老师Alex)
脚本不足:
不能实现多个文件或者目录的传输
执行命令时,当命令中有空格时必须使用 转义,否则会有报错
以上不足后期慢慢解决,毕竟脚本都是长出来的。
脚本内容:#!/usr/bin/env python
# coding=UTF-8
#This script is use for host manage
#Create by eleven in 2014-06-01
#Last changed in 2014-06-09
import paramiko as PO
import sys,os,time,socket
help_info='''
Options:
cmdRun command in remote host
getDownload file from remote host
putUpload file to remote host
hostRemote host ipaddress or hostname
userRemote host username
keyUse ssh-key authentication
password Use password authentication
nullIf use ssh-key,then password is null
commandRun command
filename1Download filename or upload filename
filename2Local save filename or remote save filename
Example:
%s cmd 10.0.0.1 test key null ifconfig
%s get 10.0.0.1 test password 123 '/etc/network' '/tmp/network'n ''' %(sys.argv[0],sys.argv[0])
error_info='''
Usage: 33[31m%s [cmd|get|put] host user [key|password] 'PASSWORD|null' [command|filename1 filename2] 33[0m
Detail: 33[31m%s --help 33[0mn ''' %(sys.argv[0],sys.argv[0])
if sys.argv[1] == '--help':
print help_info
sys.exit()
elif len(sys.argv)
print error_info
sys.exit()
host = sys.argv[2]
user = sys.argv[3]
atype = sys.argv[4]
password=sys.argv[5]
cmd=sys.argv[6]
keyfile=os.path.expanduser("~/.ssh/id_rsa")
mykey=PO.RSAKey.from_private_key_file(keyfile)
ssh=PO.SSHClient()
ssh.load_system_host_keys()
ssh.set_missing_host_key_policy(PO.AutoAddPolicy())
def ssh_con():
try:
if atype == 'key':
ssh.connect(host,22,user,pkey=mykey,timeout=3)#authentication of key
elif atype == 'password':
ssh.connect(host,22,user,password=password,timeout=3)#authentication of password
else:
print 'Wrong authentication !n'
except PO.AuthenticationException:
print "Use '%s' connect to '%s' authentication fail !n" %(user,host)
sys.exit()
except:
print "Use '%s' connect to '%s' fail !n" %(user,host)
sys.exit()
try:
stdin,stdout,stderr=ssh.exec_command(cmd,timeout=10)
out=stdout.read()
errout=stderr.read()
print '%s:' %host
if errout:
print errout
elif out:
print out
else:
print 'tCommand exec ok !'%cmd
except socket.timeout:
print "Command '%s' exec in '%s' timeout !n"%(cmd,host)
finally:
ssh.close()
def sftp_get():#get file in remote host
try:
t=PO.Transport((host,22))
getfile=sys.argv[6]#remote host filename
localfile=sys.argv[7]#save in localpath and rename
if atype == 'key':
t.connect(username=user,pkey=mykey)
elif atype == 'password':
t.connect(username=user,password=password)
else:
print 'Wrong authentication !n'
sftp = PO.SFTPClient.from_transport(t)
sftp.get(getfile,localfile)
print 'Host is: %snGetfile_Name is: %snLocalfile_Name is: %sn' %(host,getfile,localfile)
except socket.error:
print "Can not connect to '%s'n " %host
except PO.AuthenticationException:
print "Use '%s' connect to '%s' authentication fail !n" %(user,host)
except IOError:
print "Remote file '%s' does not exist !n"%getfile
finally:
ssh.close()
#t.close()
def sftp_put():#put file to remote host
try:
t=PO.Transport((host,22))
putfile=sys.argv[6]#local file name
remotefile=sys.argv[7]#put in remote host path and rename
if atype == 'key':
t.connect(username=user,pkey=mykey)
elif atype == 'password':
t.connect(username=user,password=password)
else:
print 'Wrong authentication !n'
sftp = PO.SFTPClient.from_transport(t)
sftp.put(putfile,remotefile)
print 'Host is: %snPutfile_Name is: %snRemotefile_Name is: %sn' %(host,putfile,remotefile)
except socket.error:
print "Can not connect to '%s'n" %host
except PO.AuthenticationException:
print "Use '%s' connect to '%s' authentication fail !n" %(user,host)
except OSError:
print "Local file '%s' does not exist !n"%putfile
except IOError:
print "Put file '%s' error,Maybe remote server permission denied !n"%putfile
finally:
ssh.close()
#t.close()
action=sys.argv[1]
if action == 'cmd':
if len(sys.argv) == 7:
ssh_con()
else:
print error_info
elif action == 'get':
if len(sys.argv) == 8:
sftp_get()
else:
print error_info
elif action == 'put':
if len(sys.argv) == 8:
sftp_put()
else:
print error_info
else:
print 'Wrong action !n'
最后
以上就是动人铃铛为你收集整理的python 远程控制,Python——远程管理脚本(一)的全部内容,希望文章能够帮你解决python 远程控制,Python——远程管理脚本(一)所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复