SecureCRT终端执行python脚本简化操作

#$language = "Python"
#$interface = "1.0"


import os
import csv
import re

config_file = "C:\\Users\\username\\Documents\\configuration for 36_60.txt"

###################################################################
from datetime import datetime
t=datetime.now().strftime("%d%m%Y%H%M%S")

crt.Screen.Synchronous = True
SCRIPT_TAB = crt.GetScriptTab()
rowIndex = SCRIPT_TAB.Screen.CurrentRow
colIndex = SCRIPT_TAB.Screen.CurrentColumn - 1

#获取当前行,当前列的数据,即该服务器的前缀,与waitfor配套
prompt = SCRIPT_TAB.Screen.Get(rowIndex, 0, rowIndex, colIndex) 
prompt = prompt.strip()
#crt.Dialog.MessageBox(prompt)

###################################################################

###################################################################
def main():
 
    #with open(config_file, 'r',encoding="UTF-8") as f:
    with open(config_file, 'r') as f:
        cmd = f.readline()
        while cmd:
            #crt.Dialog.MessageBox(cmd)
            send_command(cmd)
            cmd = f.readline()
            #crt.Dialog.MessageBox(cmd)

    f.close()
#crt.Dialog.MessageBox("Got system prompt: %s!"%prompt)
###################################################################

###################################################################
def get_prompt():
    SCRIPT_TAB = crt.GetScriptTab()
    rowIndex = SCRIPT_TAB.Screen.CurrentRow
    colIndex = SCRIPT_TAB.Screen.CurrentColumn - 1
    #获取当前行,当前列的数据,即该服务器的前缀,与waitfor配套
    prompt = SCRIPT_TAB.Screen.Get(rowIndex, 0, rowIndex, colIndex) 
    prompt = prompt.strip()

    #crt.Dialog.MessageBox("Got system prompt: %s!"%prompt)

    return  prompt
###################################################################

###################################################################
def send_command(command, prompt=prompt, timeout=3000):

    
    if not prompt:
        prompt='root@localhost:~# '
    crt.Screen.Send(command + '\r')
    #crt.Sleep(timeout)

    # Wait for the command and the trailing CR to be echoed back from the remote
    # before we start capturing data... Otherwise, we'll capture the command we
    # issued, as well as the results, and in this example, we only want to
    # capture the results.
    #crt.Screen.WaitForString(command) 
    #在15秒内等待屏幕出现命令行提示符下方操作,超过15秒表示超时
    #ret = crt.Screen.WaitForStrings([prompt,'# '],timeout)
    ret = crt.Screen.ReadString([prompt,'# '],timeout)

    #crt.Dialog.MessageBox("Got response:\n%s!"%ret)
    return  ret
###################################################################
 

###################################################################

def check_cpu():
    
    ps_cpu_sort = 'ps aux|sort -k3nr | head -20\r'
    ps_mem_sort = 'ps aux|sort -k4nr | head -20\r'
    

    #send_command('date')
    return send_command(ps_cpu_sort)

###################################################################

###################################################################
def check_mem():
    
    ps_cpu_sort = 'ps aux|sort -k3nr | head -20\r'
    ps_mem_sort = 'ps aux|sort -k4nr | head -20\r'
    
    #send_command('date')
    #send_command(ps_cpu_sort)
    return send_command(ps_mem_sort)
###################################################################    


###################################################################
crt.Screen.Synchronous = True
crt.Screen.Clear()
crt.Screen.Send(chr(3) + '\r\n')
#crt.Screen.WaitForStrings(['# '],5000)
prompt = get_prompt()
###################################################################


###################################################################
main()
crt.Screen.Synchronous = False
 

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值