python 实现脚本上传远程服务并执行脚本

#!/usr/bin/env python 
#coding:utf-8
import paramiko,os,commands
from scp import SCPClient
def sshclient_execmd(hostname, port, username, password, execmd):
        paramiko.util.log_to_file("paramiko.log")
        global s 
        s = paramiko.SSHClient()
        s.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        s.connect(hostname=hostname, port=port, username=username, password=password)
        stdin, stdout, stderr = s.exec_command (execmd)
        stdin.write("Y")  
        print stdout.read()
def main():
        global hostname,port,username, password
        hostname = "192.168.0.200"
        port = 22
        username = 'root'
        password = '123456'
        execmd = "whoami"
        sshclient_execmd(hostname, port, username, password, execmd)   
        upload("bash -x /data/scrips/linux_system_optimization.sh")  
def upload(cmd):
       scpclient = SCPClient(s.get_transport(),socket_timeout=15.0)
       remotepath='/opt/linux_system_optimization.sh'
       localpath='/data/scrips/linux_system_optimization.sh'
       scpclient.put(localpath, remotepath) 
       os.system(cmd)
       s.close()

if __name__=='__main__':    
       main()
[root@scrips]# cat /data/scrips/linux_system_optimization.sh 
#!/bin/bash
#this is yum and python 
function yum_source_edit()
{
if [  -f /etc/yum.repos.d/CentOS-Base.repo ]
   then 
       mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup 
      wget -t 3 http://mirrors.163.com/.help/CentOS7-Base-163.repo -O  /etc/yum.repos.d/CentOS-163.rep
    yum clean all && yum makecache
else
      echo -e "\033[42;37m yum 文件不存在,替换文件失败 \033[0m"
fi
}


function command_mode(){
  /usr/bin/yum -y install lrzsz zip ntpdate unzip net-tools g++ gcc gcc-c++ epel-release lsof make cmake  make cmake telnet ntp wget git tree nload namp iftop sysstat iotop bind-utils fuse fuse-libs

}

function kernel_optimization(){

sysctl_file="/etc/sysctl.conf"    
touch $sysctl_file
if [ -f $sysctl_file ];then
cat >> $sysctl_file <> /var/log/init.log 
fi
}
kernel_optimization


function file_limit(){
    echo  '*        soft    nproc 6553600' >>/etc/security/limits.conf
    echo  '*        soft    nproc 6553600' >> /etc/security/limits.conf
    echo  '*        hard    nproc 6553600' >> /etc/security/limits.conf
    echo  '*        soft    nofile 6553600' >> /etc/security/limits.conf
    echo  '*        hard    nofile 6553600' >> /etc/security/limits.conf
    echo  '*        soft    memlock unlimited' >> /etc/security/limits.conf
    echo  '*        hard    memlock unlimited' >> /etc/security/limits.conf
}

function ntp_server(){
/usr/bin/echo "*/5 * * * * /usr/sbin/ntpdate ntp1.aliyun.com >/dev/null 2>&1" > /var/spool/cron/root
}


function kernel_upgrade_4x(){
nuber=$(rpm -qa |grep git |wc -l)
if [ $nuber -ge 2  ] 
   then
      rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org 
      rpm -Uvh http://www.elrepo.org/elrepo-release-7.0-3.el7.elrepo.noarch.rpm 
      yum --enablerepo=elrepo-kernel install kernel-ml -y
      sed -i "s#GRUB_DEFAULT.*#GRUB_DEFAULT=0#g" /etc/default/grub
      grub2-mkconfig -o /boot/grub2/grub.cfg 
      grub2-mkconfig -o /etc/grub2.cfg
      grub2-set-default 0
else
      yum -y install git 
      rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org 
      rpm -Uvh http://www.elrepo.org/elrepo-release-7.0-3.el7.elrepo.noarch.rpm  
          yum --enablerepo=elrepo-kernel install kernel-ml -y
      sed -i "s#GRUB_DEFAULT.*#GRUB_DEFAULT=0#g" /etc/default/grub
      grub2-mkconfig -o /boot/grub2/grub.cfg
      grub2-mkconfig -o /etc/grub2.cfg
      grub2-set-default 0
fi 

}

main(){
echo -e "\033[32m 开始升级系统内核4x  \033[0m"
#kernel_upgrade_4x
echo -e "\033[32m 开始安装 替换默认yum 源  \033[0m"
yum_source_edit
echo -e "\033[32m 开始安装系统常用命令  \033[0m"
command_mode
echo -e "\033[32m 开始优化系统内核参数  \033[0m"
kernel_optimization
echo -e "\033[32m 开始新增文件句柄数  \033[0m"
file_limit
echo -e "\033[32m 同步系统时间 \033[0m"
ntp_server
}

if [[ $1 == "" ]]
  then
      main  
else
      echo -e "\033[42;37m 不需要携带参数执行,请再试一次 \033[0m"
fi