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 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
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]
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')
#!/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')
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有