python中的paramiko模塊是用來實(shí)現(xiàn)ssh連接到遠(yuǎn)程服務(wù)器上的庫,在進(jìn)行連接的時(shí)候,可以用來執(zhí)行命令,也可以用來上傳文件。
1、得到一個(gè)連接的對(duì)象
在進(jìn)行連接的時(shí)候,可以使用如下的代碼:
def connect(host): 'this is use the paramiko connect the host,return conn' ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) try: # ssh.connect(host,username='root',allow_agent=True,look_for_keys=True) ssh.connect(host,username='root',password='root',allow_agent=True) return ssh except: return None
在connect函數(shù)中,參數(shù)是一個(gè)主機(jī)的IP地址或者是主機(jī)名稱,在執(zhí)行這個(gè)方法之后,如果成功的連接到服務(wù)器,那么就會(huì)返回一個(gè)sshclient對(duì)象。
第一步是建立一個(gè)SSHClient的對(duì)象,然后設(shè)置ssh客戶端允許連接不在know_host文件中的機(jī)器,然后就嘗試連接服務(wù)器,在連接服務(wù)器的時(shí)候,可以使用兩種方式:一種方式是使用秘鑰的方式,也就是參數(shù)look_for_keys,這里用設(shè)置密碼尋找,也可以直接使用密碼的方式,也就是直接使用參數(shù)password,從而最后返回一個(gè)連接的對(duì)象。
2、 獲取設(shè)置的命令
在進(jìn)行paramiko連接之后,那么必須要得到需要執(zhí)行的命令,如下代碼所示:
def command(args,outpath): 'this is get the command the args to return the command' cmd = '%s %s' % (outpath,args) return cmd
在參數(shù)中,一個(gè)是args,一個(gè)outpath,args表示命令的參數(shù),而outpath表示為可執(zhí)行文件的路徑,例如/usr/bin/ls -l。在其中outpath也就是/usr/bin/ls ,而參數(shù)為-l
這個(gè)方法主要是用來組合命令,將分開的參數(shù)作為命令的一部分進(jìn)行組裝。
3、 執(zhí)行命令
在連接過后,可以進(jìn)行直接執(zhí)行命令,那么就有了如下的函數(shù):
def exec_commands(conn,cmd): 'this is use the conn to excute the cmd and return the results of excute the command' stdin,stdout,stderr = conn.exec_command(cmd) results=stdout.read() return results
在此函數(shù)中,傳入的參數(shù)一個(gè)為連接的對(duì)象conn,一個(gè)為需要執(zhí)行的命令cmd,最后得到執(zhí)行的結(jié)果,也就是stdout.read(),最后返回得到的結(jié)果
4、 上傳文件
在使用連接對(duì)象的時(shí)候,也可以直接進(jìn)行上傳相關(guān)的文件,如下函數(shù):
def copy_moddule(conn,inpath,outpath): 'this is copy the module to the remote server' ftp = conn.open_sftp() ftp.put(inpath,outpath) ftp.close() return outpath
此函數(shù)的主要參數(shù)為,一個(gè)是連接對(duì)象conn,一個(gè)是上傳的文件名稱,一個(gè)上傳之后的文件名稱,在此必須寫入完整的文件名稱包括路徑。
做法主要是打開一個(gè)sftp對(duì)象,然后使用put方法進(jìn)行上傳文件,最后關(guān)閉sftp連接,最后返回一個(gè)上傳的文件名稱的完整路徑
5、 執(zhí)行命令得到結(jié)果
最后就是,執(zhí)行命令,得到返回的結(jié)果,如下代碼:
def excutor(host,outpath,args): conn = connect(host) if not conn: return [host,None] exec_commands(conn,'chmod +x %s' % outpath) cmd =command(args,outpath) result = exec_commands(conn,cmd) print '%r' % result result = json.loads(result) return [host,result]
首先,進(jìn)行連接服務(wù)器,得到一個(gè)連接對(duì)象,如果連接不成功,那么返回主機(jī)名和None,表示沒有連接成功,如果連接成功,那么修改文件的執(zhí)行權(quán)限,從而可以執(zhí)行文件,然后得到執(zhí)行的命令,最后,進(jìn)行執(zhí)行命令,得到結(jié)果,將結(jié)果用json格式表示返回,從而結(jié)果能得到一個(gè)美觀的json格式,最后和主機(jī)名一起返回相關(guān)的信息
6、 測(cè)試代碼
測(cè)試代碼如下:
if __name__ == '__main__': print json.dumps(excutor('192.168.1.165','ls',' -l'),indent=4,sort_keys=True) print copy_module(connect('192.168.1.165'),'kel.txt','/root/kel.1.txt') exec_commands(connect('192.168.1.165'),'chmod +x %s' % '/root/kel.1.txt')
第一步測(cè)試命令執(zhí)行,第二步測(cè)試上傳文件,第三部測(cè)試修改上傳文件的權(quán)限。
完整代碼如下:
#!/usr/bin/env python import json import paramiko def connect(host): 'this is use the paramiko connect the host,return conn' ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) try: # ssh.connect(host,username='root',allow_agent=True,look_for_keys=True) ssh.connect(host,username='root',password='root',allow_agent=True) return ssh except: return None def command(args,outpath): 'this is get the command the args to return the command' cmd = '%s %s' % (outpath,args) return cmd def exec_commands(conn,cmd): 'this is use the conn to excute the cmd and return the results of excute the command' stdin,stdout,stderr = conn.exec_command(cmd) results=stdout.read() return results def excutor(host,outpath,args): conn = connect(host) if not conn: return [host,None] #exec_commands(conn,'chmod +x %s' % outpath) cmd =command(args,outpath) result = exec_commands(conn,cmd) result = json.dumps(result) return [host,result]
def copy_module(conn,inpath,outpath): 'this is copy the module to the remote server' ftp = conn.open_sftp() ftp.put(inpath,outpath) ftp.close() return outpath if __name__ == '__main__': print json.dumps(excutor('192.168.1.165','ls',' -l'),indent=4,sort_keys=True) print copy_module(connect('192.168.1.165'),'kel.txt','/root/kel.1.txt') exec_commands(connect('192.168.1.165'),'chmod +x %s' % '/root/kel.1.txt')
主要就是使用python中的paramiko模塊通過ssh連接linux服務(wù)器,然后執(zhí)行相關(guān)的命令,并且將文件上傳到服務(wù)器。
聲明:本網(wǎng)頁內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com