Python3使用paramiko模块远程登录关机,制作成为EXE执行程序。

需求:单位每次停电,需要关闭许多太业务服务器,一台台登录关闭比较麻烦,使用Python3语法加paramiko,time模块进行一键关闭,避免因电源断电引起服务器宕机。

paramiko模块的了解

praramiko简单来说是ssh连接模块,可以使用在python2、python3的环境中,远程SSH连接服务器的方式,使用指定用户登录操作
官方操作文档:http://docs.paramiko.org/en/2.4/api/client.html?highlight=connect
推荐网友操作:https://www.cnblogs.com/xiao-apple36/p/9144092.html

(module1)编写代码部分

#输入变量
import time
import paramiko

#定义接收函数
def channel_exe_cmd(ChannelSSH0b,cmd,t=0.1):
    ChannelSSH0b.send(cmd)
    ChannelSSH0b.send("\n")
    time.sleep(t)
    resp = ChannelSSH0b.recv(9999).decode("utf-8")# 定义变量用于接收信息
    print("Exec sshCmd: %s"%(cmd))
    print("-----------------")
    print("Exce Result: %s"%(resp))
    print("****************\n")
    return resp

#定义远程连接函数
def CreatSSHConnect0b(ip_remote,port_remote,username,password):
    print('--------start to create SSH object')
    print('Remote SSH Info:\n\'ip:%s port:%d username:%s portword:%s\'\n'%(ip_remote,port_remote,username,password))
    ssh =  paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    try:
        ssh.connect(ip_remote,port_remote,username=username,password=password,timeout=66)
        return ssh
    except:
        print('Warning:\nFist connect the ABC failed, now will retry!')
        ssh.connect(ip_remote,port_remote,username=username,password=password,timeout=60)
        print('Error:\nAttempt to connect ABC failed!!! Please check the Ip /port/ account / password.')
    else:
        print('Info:\nConnect remote ABC success.')

#初始化方法,传入参数。
if __name__=='__main__':
    ssh = CreatSSHConnect0b('192.168.123.178',50000, 'ufttt', '863975@ufttt')#本地用户账号密码,这里涉及生产无服务,密码进行串改。
    ChannelSSH0b = ssh.invoke_shell()
    
    sshCmd = 'whoami'#ssh的指令
    channel_exe_cmd(ChannelSSH0b, sshCmd)#向函数传参数
    
    sshCmd = 'ls -R -l'
    channel_exe_cmd(ChannelSSH0b, sshCmd)
    
    sshCmd = 'su - '
    if channel_exe_cmd(ChannelSSH0b, sshCmd).endswith("Password: "):
        sshCmd = 'rootroot@root'#root密码
        channel_exe_cmd(ChannelSSH0b, sshCmd)
        
    sshCmd = 'whoami'
    channel_exe_cmd(ChannelSSH0b, sshCmd)
    
    sshCmd = 'ifconfig'
    channel_exe_cmd(ChannelSSH0b, sshCmd)
    
    sshCmd = 'uname -a' 
    channel_exe_cmd(ChannelSSH0b, sshCmd)

验证实验(执行工具:spyder3)

以下是代码在spyder3中执行的结果。
结果分析,代码中函数成功切换成为root用户,在root用户上面执行关机指令,解决每次登录关机的繁琐。

runfile('C:/Users/Administrator/.spyder-py3/练习题目/ssh-su-root.py', wdir='C:/Users/Administrator/.spyder-py3/练习题目')
--------start to create SSH object
Remote SSH Info:
'ip:192.168.123.178 port:22000 username:ufttt portword:86397576@ufttt'

Exec sshCmd: whoami
-----------------
Exce Result: Last login: Mon Jul 13 15:50:42 2020 from 192.168.123.182
whoami
[ufttt@localhost ~]$ whoami  
ufttt#第一次登录用户
[ufttt@localhost ~]$ 
****************

Exec sshCmd: ls -R -l
-----------------#ufttt登录用户操作文件查看指令
Exce Result: ls -R -l
.:
total 32
drwxr-xr-x 2 ufttt ufttt 4096 Jun 22  2017 Desktop
drwxr-xr-x 2 ufttt ufttt 4096 Jun 22  2017 Documents
drwxr-xr-x 2 ufttt ufttt 4096 Jun 22  2017 Downloads
drwxr-xr-x 2 ufttt ufttt 4096 Jun 22  2017 Music
drwxr-xr-x 2 ufttt ufttt 4096 Jun 22  2017 Pictures
drwxr-xr-x 2 ufttt ufttt 4096 Jun 22  2017 Public
drwxr-xr-x 2 ufttt ufttt 4096 Jun 22  2017 Templates
drwxr-xr-x 2 ufttt ufttt 4096 Jun 22  2017 Videos

./Desktop:
total 0

./Documents:
total 0

./Downloads:
total 0

./Music:
total 0

./Pictures:
total 0

./Public:
total 0

./Templates:
total 0

./Videos:
total 0
[ufttt@localhost ~]$ 
****************

Exec sshCmd: su - 
-----------------
Exce Result: su - 
Password: 
****************

Exec sshCmd: rootroot@root
-----------------
Exce Result: 
[root@localhost ~]# 
****************

Exec sshCmd: whoami
-----------------
Exce Result: whoami#主要验证这步,如果root用户出现,就可以执行Init指令。
root
[root@localhost ~]# 
****************

Exec sshCmd: ifconfig
-----------------
Exce Result: ifconfig
eth0      Link encap:Ethernet  HWaddr F4:4D:30:5A:74:62  
          inet addr:192.168.123.178  Bcast:192.168.123.255  Mask:255.255.255.0
          inet6 addr: fe80::f64d:30ff:fe5a:7462/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:1656418 errors:0 dropped:0 overruns:0 frame:0
          TX packets:434981 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:168478337 (160.6 MiB)  TX bytes:73130863 (69.7 MiB)

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:3143426 errors:0 dropped:0 overruns:0 frame:0
          TX packets:3143426 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:590380336 (563.0 MiB)  TX bytes:590380336 (563.0 MiB)

[root@localhost ~]# 
****************

Exec sshCmd: uname -a
-----------------
Exce Result: uname -a
Linux localhost.localdomain 2.6.32-431.el6.x86_64 #1 SMP Fri Nov 22 03:15:09 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux
[root@localhost ~]# 
****************

使用pyinstaller模块打包成为可执行文件。

pyinstaller:可用于代码文件打包成为exe执行文件,给用户直接执行,不需要再进入IDE中编译。
推荐网友操作手册:http://c.biancheng.net/view/2690.html
操作步骤:
1、在win10主机上安装Anaconda3,并在环境中安装pyinstaller包。
在这里插入图片描述
2、点击进行Python安装的环境变量路径
在这里插入图片描述
3、进入脚本所在的环境.(cd路径)
在这里插入图片描述
4、进入脚本所在的环境.(cd路径),执行一下脚本
-F是产生单个可执行文件,-i是引用ico格式图片给程序上图标。
ico图片在线转化:http://www.bitbug.net/
Pyinstaller参考文档:
http://c.biancheng.net/view/2690.html
https://segmentfault.com/a/1190000016087451
在这里插入图片描述
生成了EXE程序在这里插入图片描述

附加:如果想要exe执行程序执行后,画面停止,在后台代码中加入两行代码
import os
os.system(")
在这里插入图片描述

(module2)执行失败代码部分总结

总结:借鉴其他网友的版本使用,后发现代码每次执行后禁止不前,怀疑是python2中设置的‘’空字符串不适用于python3中。知道方法网友请留言告诉我。

代码部分:

在这里插入图片描述
执行结果,卡死在后面,也没有报错,头疼。
在这里插入图片描述
鸣谢:
谢谢网友提供方法和解决思路,因为涉及的连接太多,无法一一呈现,IT道路无止境,用于突破赢自我。喜欢点个赞。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值