python远程执行命令、上传文件

2015年8月27日 07:20 by wst

python小技巧

由于公司的服务器只能通过publickey登录,本地只有一个dsa文件,登录服务器时必须通过此文件,下面具体给出示例;

1,打开ssh客户端,然后执行命令

import paramiko

def ssh2(ip,username,passwd,cmd):
    try:
        paramiko.util.log_to_file('paramiko.log')
        ssh  = paramiko.SSHClient()
        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        keyFile = r'E:\workfile\mylib\id_dsa_2048'
        ssh.connect(ip,22,username,passwd,timeout=5,key_filename=keyFile)
        stdout,stdin,stderr = ssh.exec_command(cmd)
        print stdout._read(1024)
    except Exception,e :
        print "Error.",e 


if __name__ == "__main__":
    cmd = "mkdir -p ./aa/bb/cc"
    ssh2('192.168.0.3','root','*********','ls')

2,打开sftp客户端,然后上传文件

#!/usr/bin/env python

import socket
import paramiko
import os

username='root'
hostname='192.168.0.3'
port = 22

sock=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((hostname, port))

t=paramiko.Transport(sock)

t.start_client()

key=t.get_remote_server_key()

privatekeyfile = os.path.expanduser(r'E:\workfile\mylib\id_dsa_2048')

mykey = paramiko.DSSKey.from_private_key_file(privatekeyfile,password='********')

t.auth_publickey(username,mykey)

sftp=paramiko.SFTPClient.from_transport(t)

sftp.put(r"E:\workfile\test\crm.rar","/root/crm.rar")

sftp.close()

t.close()

3,综合使用

import paramiko

def ssh2(ip,username,passwd,cmd):
    try:
        paramiko.util.log_to_file('paramiko.log')
        ssh  = paramiko.SSHClient()
        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        keyFile = r'E:\workfile\mylib\id_dsa_2048'
        ssh.connect(ip,22,username,passwd,timeout=5,key_filename=keyFile)
        stdout,stdin,stderr = ssh.exec_command(cmd)
        print stdout._read(1024)

        sftp = ssh.open_sftp()
        sftp.put(r"E:\workfile\test\crm.rar","/root/crm.rar")

        ssh.close()

    except Exception,e :
        print "Error.",e 


if __name__ == "__main__":
    cmd = "mkdir -p ./aa/bb/cc"
    ssh2('192.168.0.3','root','********','ls')

 


Comments(127) Add Your Comment

Charlesnag
“I haven’t seen you in these parts,” the barkeep said, sidling settled to where I sat. “Personage’s Bao.” He stated it exuberantly, as if low-down of his exploits were shared by way of settlers around many a fire in Aeternum. He waved to a unanimated tun hard by us, and I returned his token with a nod. He filled a eyeglasses and slid it to me across the stained red wood of the bench prior to continuing. “As a betting fellow, I’d be assenting to wager a adequate portion of silver you’re in Ebonscale Reach for the purpose more than the carouse and sights,” he said, eyes glancing from the sword sheathed on my hip to the bow slung across my back. https://images.google.bf/url?q=https://renewworld.ru/sistemnye-trebovaniya-new-world/
Kevingaw
https://www.google.so/url?q=http://1xbitapp.org/index.html http://tanzschule-schrittfest.de/wp-content/pages/welche_online_casinos_sind_in_deutschland_legal__2.html http://maps.google.com.eg/url?q=http://cloudbetwelcomebonus.com/it/ http://maps.google.vg/url?q=http://pinup-fashion.de/wp-content/pages/welche_online_casinos_sind_in_deutschland_legal_.html http://images.google.fr/url?q=https://1xbitapk.com/hi/
Pckrzv
lipitor 40mg generic <a href="https://lipiws.top/">order lipitor 10mg online</a> buy lipitor pills
Qzycvz
buy ciprofloxacin 1000mg pill - <a href="https://cipropro.top/bactrimos/">bactrim brand</a> augmentin 625mg over the counter
Tkbpbt
order ciprofloxacin 1000mg online cheap - <a href="https://metroagyl.top/cefaclor/">cephalexin online</a> oral augmentin 375mg
Ydimxi
ciprofloxacin 500mg price - <a href="https://septrim.top/tinidazole/">tinidazole for sale</a> order erythromycin pills
Lycleo
order metronidazole 200mg without prescription - <a href="https://metroagyl.top/azithromycin/">azithromycin 500mg canada</a> azithromycin buy online
Txhiov
ivermectin 3mg for humans - <a href="https://keflexin.top/ceftin/">where to buy axetil without a prescription</a> order tetracycline pills
Tlpkrd
valtrex 500mg brand - <a href="https://gnantiviralp.com/nemasole/">purchase nemasole pill</a> acyclovir pills
Jqywwp
purchase ampicillin generic <a href="https://ampiacil.top/amoxil500/">amoxil us</a> buy amoxil pill
Aemhvh
flagyl 400mg brand - <a href="https://metroagyl.top/">buy generic flagyl</a> purchase zithromax pills
Ixilgi
furosemide 100mg sale - <a href="https://antipathogenc.com/prograf/">order prograf</a> order capoten 25mg sale
Jprztl
glucophage where to buy - <a href="https://gnantiacidity.com/duricef/">buy duricef no prescription</a> generic lincocin
Mlsufj
cost retrovir - <a href="https://canadiangnp.com/">glycomet oral</a> order zyloprim 100mg pills
Zuhmlg
order clozapine generic - <a href="https://genonlinep.com/coversyl/">aceon price</a> pepcid canada
Hslzfa
quetiapine for sale - <a href="https://gnkantdepres.com/bupron/">generic bupropion</a> buy eskalith without prescription
Iaqksw
buy anafranil 25mg generic - <a href="https://antdeponline.com/cymbalta/">buy cymbalta 20mg online cheap</a> purchase doxepin generic
Pnfxhv
atarax 25mg without prescription - <a href="https://antdepls.com/pamelor/">order nortriptyline sale</a> amitriptyline online
Bnlojk
amoxiclav brand - <a href="https://atbioinfo.com/ampicillin/">ampicillin cost</a> purchase ciprofloxacin
Pvnxee
buy amoxil generic - <a href="https://atbioxotc.com/">buy amoxil pill</a> order cipro 500mg without prescription
Ualgea
azithromycin 500mg usa - <a href="https://gncatbp.com/dofloxacin/">buy ofloxacin for sale</a> brand ciplox 500 mg
Fumzls
buy cleocin 150mg without prescription - <a href="https://cadbiot.com/cefpodoxime/">vantin medication</a> chloramphenicol cheap
Kofbbk
buy ivermectin 6 mg - <a href="https://antibpl.com/rferyc/">eryc 500mg ca</a> cefaclor 250mg without prescription
Sonpuq
ventolin 4mg pill - <a href="https://antxallergic.com/dftheo-24/">theophylline 400 mg canada</a> buy theophylline without a prescription
Nscmnm
buy methylprednisolone 8 mg - <a href="https://ntallegpl.com/fluorometholone/">order fluorometholone sale</a> order azelastine 10 ml sprayer
Vsrnqs
buy generic desloratadine - <a href="https://rxtallerg.com/beclomethasone/">buy beclomethasone online cheap</a> ventolin generic
Umvjpv
glyburide online order - <a href="https://prodeprpl.com/pioglitazone30/">order actos 30mg online</a> order dapagliflozin generic
Mrtmho
metformin 500mg tablet - <a href="https://arxdepress.com/">glucophage online</a> order acarbose 50mg pills
Zwxnjm
order prandin 2mg without prescription - <a href="https://depressinfo.com/jardiance25/">empagliflozin 25mg sale</a> order jardiance 25mg without prescription
Ipvkem
buy rybelsus 14mg - <a href="https://infodeppl.com/">buy rybelsus</a> desmopressin
Qiolho
buy generic lamisil 250mg - <a href="https://treatfungusx.com/fulvicin250mg/">where to buy fulvicin without a prescription</a> order griseofulvin sale
Cmwojb
nizoral 200 mg sale - <a href="https://antifungusrp.com/clotrithasone/">lotrisone</a> order sporanox generic
Bijiym
famciclovir 250mg oral - <a href="https://amvinherpes.com/acyclovircream/">cheap zovirax 800mg</a> order valaciclovir 500mg online
Tozhhy
buy lanoxin 250mg generic - <a href="https://blpressureok.com/furosemidediuret/">furosemide for sale</a> buy generic furosemide diuretic
Ujysua
order lopressor 50mg online - <a href="https://bloodpresspl.com/">lopressor 50mg tablet</a> brand nifedipine 10mg
Azzxcy
order microzide 25 mg pills - <a href="https://norvapril.com/bisoprolol/">order bisoprolol 10mg online</a> bisoprolol 5mg ca
Bsqbks
nitroglycerin for sale online - <a href="https://nitroproxl.com/catapres/">order clonidine generic</a> purchase valsartan without prescription
Toqyhh
zocor sway - <a href="https://canescholest.com/atorvastatin/">lipitor disguise</a> atorvastatin kindness
Kyqzrr
crestor together - <a href="https://antcholesterol.com/ezetimibe/">zetia limit</a> caduet suggest
Upoqjy
viagra professional northern - <a href="https://edsildps.com/eriacta/">eriacta tend</a> levitra oral jelly online perish
Gubbna
dapoxetine aid - <a href="https://promedprili.com/zudena/">zudena deep</a> cialis with dapoxetine worth
Bslvdp
cenforce online scent - <a href="https://xcenforcem.com/zenegrasildenafil/">zenegra pills moody</a> brand viagra pills waist
Qdkdpv
brand cialis couple - <a href="https://probrandtad.com/zhewitra/">zhewitra beautiful</a> penisole government
Ytmqvd
brand cialis proud - <a href="https://probrandtad.com/apcalissx/">apcalis draught</a> penisole pursue
Jydczu
cialis soft tabs pills petunia - <a href="https://supervalip.com/valifonline/">valif pills crash</a> viagra oral jelly class
Jpofvi
cialis soft tabs fog - <a href="https://supervalip.com/viagrasuperactive/">viagra super active monstrous</a> viagra oral jelly online print
Tgwgqa
dapoxetine tomorrow - <a href="https://promedprili.com/zudena/">udenafil tin</a> cialis with dapoxetine question
Pbcavb
cenforce online terrify - <a href="https://xcenforcem.com/tadaciptadalafil/">tadacip pills mankind</a> brand viagra mix
Bndmbj
asthma treatment observe - <a href="https://bsasthmaps.com/">asthma treatment passion</a> asthma treatment helmet
Llpkij
acne medication stamp - <a href="https://placnemedx.com/">acne medication curve</a> acne treatment badge
Zpkhsq
prostatitis medications swell - <a href="https://xprosttreat.com/">prostatitis medications underground</a> prostatitis pills loose
Celrop
uti medication glitter - <a href="https://amenahealthp.com/">treatment for uti island</a> uti antibiotics objection
Qlnhfm
loratadine medication yourself - <a href="https://clatadine.top/">claritin collapse</a> claritin protest
Qezksm
valacyclovir modern - <a href="https://gnantiviralp.com/">valtrex drag</a> valacyclovir online title
Gimotw
dapoxetine apprehension - <a href="https://prilixgn.top/">priligy watcher</a> dapoxetine method
Aoywkq
loratadine medication law - <a href="https://clatadine.top/">loratadine medication capital</a> loratadine find
Xuxytv
ascorbic acid may - <a href="https://ascxacid.com/">ascorbic acid plastic</a> ascorbic acid generous
Hvtvhn
promethazine trifle - <a href="https://prohnrg.com/">promethazine clumsy</a> promethazine wife
Ysjlio
clarithromycin pills sacrifice - <a href="https://gastropls.com/albendazole400/">albenza million</a> cytotec pills crumble
Isofrw
fludrocortisone pills date - <a href="https://gastroplusp.com/omepprilosec/">omeprazole pills arrive</a> prevacid venture
Iplfoz
rabeprazole brand - <a href="https://gastrointesl.com/metoclopramide/">cost metoclopramide 20mg</a> where to buy domperidone without a prescription
Oqsons
bisacodyl 5mg over the counter - <a href="https://gastroinfop.com/">pill bisacodyl 5 mg</a> order liv52 10mg without prescription
Nzmewg
purchase eukroma for sale - <a href="https://danaterone.shop/">hydroquinone cheap</a> dydrogesterone 10mg over the counter
Dtkybv
order generic bactrim - <a href="https://ketiracet.com/">order levetiracetam 1000mg without prescription</a> buy tobra 5mg generic
Inajxm
griseofulvin 250 mg sale - <a href="https://gemfilopi.com/">gemfibrozil 300mg generic</a> buy gemfibrozil sale
Hjskbo
forxiga brand - <a href="https://sineqpin.com/">sinequan 25mg generic</a> order acarbose without prescription
Ixtklr
order dramamine 50mg for sale - <a href="https://prasilx.com/">buy cheap generic prasugrel</a> risedronate 35 mg cost
Hskauu
order vasotec 5mg online - <a href="https://doxapisin.com/">doxazosin order</a> xalatan without prescription
Sardjl
monograph 600mg price - <a href="https://cilosetal.com/">pletal 100 mg usa</a> cilostazol buy online
Zorflw
piroxicam 20mg tablet - <a href="https://feldexicam.com/">order piroxicam 20mg online cheap</a> exelon buy online
Kzjsmg
buy hydroxyurea paypal - <a href="https://hydroydrinfo.com/trecatorsc/">ethionamide pills</a> buy robaxin 500mg sale
Rjiupl
order divalproex 250mg sale - <a href="https://adepamox.com/amiodarone/">buy amiodarone 200mg pill</a> cost topamax
Ggbgqh
purchase disopyramide phosphate pill - <a href="https://anorpica.com/chlorpromazine/">chlorpromazine generic</a> purchase thorazine sale
Cdcllv
oral spironolactone - <a href="https://aldantinep.com/phenytoin/">order dilantin 100mg online</a> buy revia cheap
Uxkkgl
cyclophosphamide ca - <a href="https://cycloxalp.com/trimetazidine/">how to get trimetazidine without a prescription</a> purchase vastarel generic
Ygxouu
how to buy cyclobenzaprine - <a href="https://abflequine.com/donepezil/">donepezil brand</a> buy enalapril online
Wbcmml
buy zofran no prescription - <a href="https://azofarininfo.com/procyclidine/">cheap kemadrin online</a> order requip
Mqnlmm
buy generic ascorbic acid online - <a href="https://mdacidinfo.com/ferroussulfate/">ferrous sulfate 100 mg brand</a> compro drug
Pfszqx
durex gel where to purchase - <a href="https://xalaplinfo.com/">how to buy durex gel</a> where can i buy zovirax
Tmtghs
purchase minoxidil generic - <a href="https://hairlossmedinfo.com/finasteridefinp/">buy finpecia online cheap</a> order proscar 5mg generic
Bjnccf
arava 20mg sale - <a href="https://infohealthybones.com/alfacalcidol/">alfacip sale</a> purchase cartidin online
Wlufbg
buy calan sale - <a href="https://infoheartdisea.com/atenolol/">tenoretic order online</a> buy generic tenoretic
Psbwcr
tenormin for sale - <a href="https://heartmedinfox.com/sotalol/">order sotalol 40 mg without prescription</a> buy coreg 6.25mg generic
Htuvjc
where can i buy atorlip - <a href="https://infoxheartmed.com/lisinopril/">order zestril online</a> buy nebivolol no prescription
Babjfk
gasex drug - <a href="https://herbalinfomez.com/">buy gasex sale</a> buy diabecon pill
Xslvub
buy generic norfloxacin for sale - <a href="https://gmenshth.com/">cheap noroxin for sale</a> order confido for sale
Fkucfn
buy speman pills - <a href="https://spmensht.com/">speman buy online</a> cheap generic finasteride
Azcoid
finax order - <a href="https://finmenura.com/doxazosin/">doxazosin online order</a> order uroxatral 10mg online cheap
Rzocjm
buy cheap generic hytrin - <a href="https://hymenmax.com/tamsulosin/">order flomax 0.2mg online cheap</a> dapoxetine 60mg brand
Tzbqdd
order trileptal 600mg - <a href="https://trileoxine.com/">oxcarbazepine usa</a> levoxyl drug
Lmrbpt
cyclosporine over the counter - <a href="https://asimusxate.com/methotrexate/">methotrexate 2.5mg drug</a> colchicine 0.5mg without prescription
Jwpdeg
purchase duphalac online cheap - <a href="https://duphalinfo.com/betahistine/">betahistine 16mg price</a> brand betahistine 16 mg
Zjtqaj
deflazacort over the counter - <a href="https://lazacort.com/brimonidine/">buy generic brimonidine for sale</a> how to get alphagan without a prescription
Iidmik
order besivance online cheap - <a href="https://besifxcist.com/carbocysteine/">buy carbocisteine pills for sale</a> buy sildamax medication
Bvhtsd
order gabapentin online cheap - <a href="https://aneutrin.com/sulfasalazine/">azulfidine 500 mg generic</a> azulfidine for sale online
Aufxpo
probenecid 500mg canada - <a href="https://bendoltol.com/">buy probalan without a prescription</a> carbamazepine 400mg uk
Fjqwyx
order celebrex 200mg for sale - <a href="https://celespas.com/">buy celecoxib 200mg</a> purchase indocin sale
Vribpp
purchase mebeverine pills - <a href="https://coloxia.com/etoricoxib/">buy arcoxia 120mg generic</a> where can i buy pletal
Ucjxff
order diclofenac 50mg generic - <a href="https://dicloltarin.com/">buy generic voltaren 50mg</a> aspirin price
Xbnkrz
order generic rumalaya - <a href="https://rumaxtol.com/">cheap rumalaya online</a> buy elavil online cheap
Utdetf
mestinon pills - <a href="https://mestonsx.com/azathioprine/">how to get imuran without a prescription</a> buy imuran 25mg online cheap
Afyqpx
buy diclofenac tablets - <a href="https://vovetosa.com/tisosorbide/">imdur 20mg cost</a> nimodipine drug
Sojvtg
ozobax pills - <a href="https://baclion.com/nspiroxicam/">piroxicam 20mg uk</a> how to get feldene without a prescription
Uvvute
order cyproheptadine - <a href="https://periheptadn.com/aszanaflex/">tizanidine 2mg ca</a> buy generic zanaflex
Glnfmw
order meloxicam 15mg generic - <a href="https://meloxiptan.com/asrizatriptan/">rizatriptan 10mg ca</a> ketorolac price
Lctvah
order trihexyphenidyl online - <a href="https://voltapll.com/diclofenacgel/">buy diclofenac gel sale</a> how to order emulgel
Xickuu
omnicef 300 mg canada - <a href="https://omnixcin.com/aminocycline/"></a> cleocin online
Pnszgw
accutane 40mg without prescription - <a href="https://aisotane.com/">order isotretinoin without prescription</a> deltasone usa
Zfgmoe
buy deltasone pills - <a href="https://apreplson.com/prednisolone/">buy omnacortil 5mg pill</a> order permethrin online cheap
Tpmeis
permethrin over the counter - <a href="https://actizacs.com/asbenzac/">benzoyl peroxide oral</a> retin cream for sale
Ywvnno
betnovate online - <a href="https://betnoson.com/benoquincre/">benoquin for sale online</a> purchase monobenzone without prescription
Fbkpni
oral metronidazole 400mg - <a href="https://ametronles.com/">buy metronidazole 200mg without prescription</a> generic cenforce
Uqsqmt
cleocin 150mg tablet - <a href="https://indometnp.com/">indocin 75mg drug</a> purchase indomethacin for sale
Hkegro
losartan 25mg for sale - <a href="https://acephacrsa.com/">buy cephalexin 500mg for sale</a> buy generic keflex 125mg
Ogbsat
eurax usa - <a href="https://aeuracream.com/abactroban/">buy bactroban ointment sale</a> buy aczone paypal
Gcipey
buy provigil 100mg pill - <a href="https://sleepagil.com/melatonin/">melatonin medication</a> buy melatonin 3mg generic
Gcrpgb
zyban pills - <a href="https://bupropsl.com/shuddhaguggulu/">brand shuddha guggulu</a> shuddha guggulu drug
Bozwuf
order capecitabine 500mg generic - <a href="https://xelocap.com/">order xeloda 500 mg online</a> order danazol 100 mg
Doibuh
buy prometrium 200mg pills - <a href="https://apromid.com/">where can i buy progesterone</a> purchase clomiphene
Quqmpd
buy fosamax 70mg generic - <a href="https://pilaxmax.com/tamoxifen/">tamoxifen 20mg over the counter</a> how to buy provera
Kmnbip
order generic aygestin 5 mg - <a href="https://norethgep.com/">aygestin 5 mg for sale</a> buy cheap generic yasmin
Djbdmd
order yasmin without prescription - <a href="https://festrolp.com/">where can i buy estrace</a> order arimidex 1mg without prescription
Dqrmrm
dostinex medication - <a href="https://adostilin.com/premarin/">buy premarin 0.625mg online cheap</a> buy alesse without prescription
Blefqw
プレドニン 飲み方 - <a href="https://jpaonlinep.com/jazithromycin/">ジスロマック 海外通販</a> アジスロマイシン の購入
Nlajyz
г‚·гѓ«гѓ‡гѓЉгѓ•г‚Јгѓ« гЃ©гЃ“гЃ§иІ·гЃ€г‚‹ - <a href="https://jpedpharm.com/">г‚·гѓ«гѓ‡гѓЉгѓ•г‚Јгѓ« и–¬е±ЂгЃ§иІ·гЃ€г‚‹</a> г‚їгѓЂгѓ©гѓ•г‚Јгѓ« - 50mg/100mg
Duyzst
プレドニン 薬局で買える - <a href="https://jpanfarap.com/jpadoxycycline/">ドキシサイクリン 市販 おすすめ</a> アキュテイン 個人輸入 おすすめ
Swsycy
eriacta wrap - <a href="https://eriagra.com/dapcalisb/">apcalis chapter</a> forzest flat