Python Remote execute command, publish file

Source: Internet
Author: User

Recently there is a demand, want to get some of the server running on those application services, a set to see, too much effort, refer to the Ox people wrote this script, and later plus a front page to do some simple maintenance work, make up the first use, to be perfected,

Note: This script is lazy to install fabric, the installation process reference: http://5973819.blog.51cto.com/5963819/1532334


#!/usr/bin/env python

#coding: UTF8

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

# author:wangganyu188 [email protected]

# Last Modified:2014-09-05

# Filename:sys_ops.py

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




From FABRIC.API import Env,run,put,get

From OS import path

from re import findall

From sys import ARGV

From fabric.context_managers Import Hide

From time import sleep




User= ' Root '

Host,ip_list=[],[]

Port= ' 22 '

Timeout = 1

CMD,GETSRC,GETDST,PUTSRC,PUTDST = ', ', ', ', ', '


For I in range (1,len (argv) +1):

#print I

If argv[i-1] = = '-h ' or len (argv) = = 1:

Print "" "

USAGE:

-C [CMD] the command you want the host (s) to run

-F [File] The file content multiple IP address want to connect

-P [put] The local file that is want to upload to the remote host (s)

-G [Get] The remote file that is want to download to the local host

-h [Help] Print

"""

If argv[i-1]== '-f ':

If Path.isfile ('%s '% (argv[i]) = = True:

file_list = open ('%s '% (Argv[i]), ' R '). ReadLines ()

For line in File_list:

#print Line

HostIP = Line.split () [0]

HOSTPW = Line.split () [1]

#print hostip, ' \ n ', HOSTPW

Ip_list.append (HostIP)

Env.password = '%s '%HOSTPW

#print ip_list, ' \ n ', Env.password

If argv[i-1] = = '-C ':

CMD = Argv[i]


If argv[i-1] = = '-P ':

p = src = argv[i].split (', ')

PUTSRC = p[0]

PUTDST = p[1]



If argv[i-1] = = '-G ':

G = src = Argv[i].split (', ')

GETSRC = g[0]

GETDST = g[1]


Else

Ip_port = []

If Len (ip_list)! = 0:

For IP in ip_list:

Ip_port.append (IP + ': ' + PORT)




If CMD! = ':

def command ():

With hide (' running '):

Run ("%s"%cmd)


For Ipport in Ip_port:

env.host_string = Ipport

Print "Execute Command: \033[1;33;40m%s\033[0m at Host: \033[1;33;40m%s \033[0m"% (Cmd,ipport.split (': ') [0])

Print "***************************************************************"

Command ()

print ' *************************************************************** '


If PUTSRC and putdst! = ':

Def putupload ():

With hide (' running '):

Put ("%s"% (PUTSRC), "%s"% (PUTDST))

For Ipport in Ip_port:

env.host_string = Ipport

Print "PUT local file:\033[1;33;40m%s \033[0m to remote host:\033[1;33;40m%s\033[0m: \033[1;33;40m%s\033[0m"% (PUTSRC, Ipport.split (': ') [0],PUTDST]

Print "*****************************************************************"

Putupload ()

Print "*****************************************************************"


If GETSRC and getdst! = ':

Def getdown ():

With hide (' running '):

Get ("%s"% (GETSRC), "%s"% (GETDST))

For Ipport in Ip_port:

env.host_string = Ipport

Print "Get remote file:\033[1;33;40m%s \033[0m from host: \033[1;33;40m%s\033[0m to local \033[1;33;40m%s\033[0m"% (get Src,ipport.split (': ') [0],GETDST]

Print "*****************************************************************"

Getdown ()

Print "*****************************************************************"


Examples of use are:

Help:

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/48/86/wKioL1QJPDzx_ooIAAHHs09G_kM956.jpg "title=" Help.png "alt=" Wkiol1qjpdzx_ooiaahhs09g_km956.jpg "/>

Remote execution commands

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/48/84/wKiom1QJO7HzxjA-AAKuG5BNpJY393.jpg "title=" Cmd.png "alt=" Wkiom1qjo7hzxja-aakug5bnpjy393.jpg "/>

Upload file:

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/48/86/wKioL1QJPJCDQoYkAAHunkuIrK0963.jpg "title=" up.png "alt=" Wkiol1qjpjcdqoykaahunkuirk0963.jpg "/>

This article from "Easier Avenue to Jane" blog, declined reprint!

Python Remote execute command, publish file

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.