python implement scripts to upload remote services and execute scripts #!/usr/bin/ env python #coding: Utf-8import paramiko,os,commandsfrom scp import scpclientdef 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 ()
[[email protected]]# 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 makecacheelse echo -e "\033[42;37m yum file does not exist, Replace file failed \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 _fileif [ -f $sysctl _file ];thencat >> $sysctl _file <<eoffs.nr_open = 6553600fs.file-max = 6553600net.ipv4.ip_forward = 1net.ipv4.conf.default.rp_filter = 1net.ipv4.conf.default.accept_source_route = 0kernel.sysrq = 0kernel.core_uses_pid = 1net.ipv4.tcp_syncookies = 1kernel.msgmnb = 655360kernel.msgmax = 655360kernel.shmmax = 68719476736kernel.shmall = 4294967296vm.max_map_count = 262144net.ipv4.tcp_keepalive_probes = 5net.ipv4.tcp_keepalive_time = 30net.ipv4.tcp_max_ orphans = 3276800net.ipv4.tcp_max_syn_backlog = 1048576net.ipv4.tcp_max_tw_buckets = 50000net.ipv4.tcp_mem = 94500000 915000000 927000000net.ipv4.tcp_orphan_retries = 3net.ipv4.tcp_reordering = 5net.ipv4.tcp_retrans_collapse = 0net.ipv4.tcp_retries2 = 5net.ipv4.tcp_rmem = 4096 87380 4194304net.ipv4.tcp_sack = 1net.ipv4.tcp_synack_retries = 1net.ipv4.tcp_syncookies = 0net.ipv4.tcp_syn_retries = 1net.ipv4.tcp_timestamps = 0net.ipv4.tcp_tw_recycle = 0net.ipv4.tcp_tw_reuse = 1net.ipv4.tcp_window_scaling = 1net.ipv4.tcp_wmem = 4096 16384 4194304net.ipv6.conf.all.disable_ipv6 = 1net.ipv6.conf.default.disable_ipv6 = 1eofelse echo "sysctl.conf File does not exist, "&NBSP;>>&NBSP;/VAR/LOG/INIT.LOG&NBsp;fi}kernel_optimizationfunction 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 0else 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 0fi }main () {echo -e "\033[32m Start upgrading the system kernel 4x \033[0m "#kernel_upgrade_4xecho -e " \033[32m start installation Replace the default yum source \033[0m "yum_source_editecho -e " \033[32m start installing the system common commands \033[0m "Command_ modeecho -e "\033[32m start optimizing system kernel Parameters \033[0m" kernel_optimizationecho -e "\033[ 32m start new file handle number \033[0m "file_limitecho -e " \033[32m synchronization system Time \033[0m "Ntp_ server}if [[ $1 == "" ]] then main else echo -e "\033[42;37m do not need to carry parameters, please try again \ 033[0m "fi
Python implements scripts to upload remote services and execute scripts