python交互界面 linux_Python_基于Python同Linux进行交互式操作实现通过堡垒机访问目标机...

end_flag1

= ']$'end_flag2

= ']#'stdout

= ''#

存放每次执行读取的内容for

item in prompt_input_list:prompt

= list(item.keys())[0]input

= item[prompt]flag

= Falsewhile

stdout.find(prompt) == -1:try:stdout

+= channel.recv(65535).decode('utf-8')#.decode('utf-8')flag

= Trueexcept

Exception as e:print('通过堡垒机:%s

访问目标机:%s

失败:%s' % (hostname, host_via_by_bastion, e))flag

= Falsebreakif

flag:channel.send(input)else:

# 未找到了对应提示return

[False,

'通过堡垒机:%s

访问目标机:%s

失败,可能是读取命令返回结果超时,或者没找到对应输入提示'%

(hostname, host_via_by_bastion)]flag

= Falsewhile

not (stdout.rstrip().endswith(end_flag1) or stdout.rstrip().endswith(end_flag2)):try:stdout

= channel.recv(2048).decode('utf-8')flag

= Trueexcept

Exception as e:print('通过堡垒机:%s

访问目标机:%s

失败:%s' % (hostname, host_via_by_bastion, e))flag

= Falsebreakif

flag:return

[True,

'']else:

# 未找到了对应提示return

[False,

'没出现成功登录提示符 ]$

或 ]#

']channel.close()return[True,

'']except

Exception as e:return

[False,

'%s' % e]#

远程执行命令def

exec_command(self,

command, target_host, bastion_host):try:channel

= self.ssh_client.invoke_shell()channel.settimeout(30)

# 读、写操作超时时间,30秒end_flag1

= ']$'end_flag2

= ']#'################

定制化开发 ##############################if

bastion_host != '':print('正在通过堡垒机:%s

访问目标机:%s' % (bastion_host, target_host))target_host_input

= target_host + '\n'prompt_input_list

= [{'Please

enter your ID':'01367599\n'},

{'Please

enter your password':

'Huozhe2020\n'},

{'Please

select your app ip':target_host_input},

{'select

your user for login':'1\n'}]stdout

= ''#

存放每次执行读取的内容for

item in prompt_input_list:prompt

= list(item.keys())[0]input

= item[prompt]flag

= Falsewhile

stdout.find(prompt) == -1:try:stdout

+= channel.recv(65535).decode('utf-8')#.decode('utf-8')flag

= Trueexcept

Exception as e:print('通过堡垒机:%s

访问目标机:%s

失败:%s' %  (bastion_host, target_host,

e))flag

= Falsebreakif

flag:channel.send(input)else:

# 未找到了对应提示print('通过堡垒机:%s

访问目标机:%s

失败,可能是读取命令返回结果超时,或者没找到对应输入提示'%

(bastion_host, target_host))#

return [False, '通过堡垒机:%s 访问目标机:%s 失败,可能是读取命令返回结果超时,或者没找到对应输入提示'

%  (bastion_host,

target_host)]returnwhile

not (stdout.rstrip().endswith(end_flag1) or stdout.rstrip().endswith(end_flag2)):try:stdout

= channel.recv(2048).decode('utf-8')except

Exception as e:print('通过堡垒机:%s

访问目标机:%s

失败:没出现成功登录提示符 ]$ 或 ]#' % (bastion_host, target_host))returnchannel.send(command+'\n')command_res

= ''#

存放每次执行读取的内容while

not (command_res.endswith(end_flag1) or command_res.endswith(end_flag2)):try:command_res

= channel.recv(2048).decode('utf-8').strip()except

Exception as e:print('在目标机(IP:

%s)上进行读取操作超时' % target_host)breakchannel.close()except

Exception as e:print('针对目标机:%s

执行命令: %s

出错 %s' % (target_host, command, e))

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,您的问题是如何用Python实现一个交互式界面的扑克牌发牌程序。下面是一个简单的实现示例,您可以参考一下: ```python import random import tkinter as tk # 创建扑克牌的列表 suits = ['♠', '♥', '♦', '♣'] ranks = ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K'] deck = [rank + suit for suit in suits for rank in ranks] # 创建交互式界面 root = tk.Tk() root.title('扑克牌发牌程序') canvas = tk.Canvas(root, width=800, height=600) canvas.pack() # 创建玩家和底牌的标签 player_label = tk.Label(root, text='玩家:') player_label.place(x=50, y=50) deck_label = tk.Label(root, text='底牌:') deck_label.place(x=50, y=100) # 创建发牌按钮,点击后随发一张牌给玩家或底牌 def deal_card(): card = random.choice(deck) deck.remove(card) if len(player_cards) < 5: player_cards.append(card) player_card_label.config(text=' '.join(player_cards)) else: deck_cards.append(card) deck_card_label.config(text=' '.join(deck_cards)) deal_button = tk.Button(root, text='发一张牌', command=deal_card) deal_button.place(x=50, y=150) # 创建玩家和底牌的显示标签 player_card_label = tk.Label(root, text='', font=('Arial', 20)) player_card_label.place(x=150, y=50) deck_card_label = tk.Label(root, text='', font=('Arial', 20)) deck_card_label.place(x=150, y=100) # 初始化玩家和底牌的牌组 player_cards = [] deck_cards = [] root.mainloop() ``` 这个程序使用了Tkinter库创建了一个交互式界面,包括一个发牌按钮和两个标签用于显示玩家和底牌的牌组。点击发牌按钮后,程序会随从扑克牌列表中选取一张牌,并将其从列表中删除。如果玩家的牌组还没有满5张,则将这张牌加入玩家的牌组中,并更新玩家牌组的显示标签;否则,将这张牌加入底牌的牌组中,并更新底牌牌组的显示标签。 您可以根据自己的需求进行修改和扩展,比如增加计分功能、添加更多的按钮和标签等等。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值