日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python ssh shell交互_使用Paramiko在Python上用ssh实现交互式shell?

發(fā)布時間:2025/3/11 python 51 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python ssh shell交互_使用Paramiko在Python上用ssh实现交互式shell? 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

我想編寫一個程序(在Windows 7上的Python 3.x中),它通過ssh在遠程shell上執(zhí)行多個命令.在查看paramikos的exec_command()函數(shù)之后,我意識到它不適合我的用例(因為在執(zhí)行命令后通道被關閉),因為命令依賴于環(huán)境變量(由先前的命令設置)并且不能連接到一個exec_command()調用,因為它們將在程序中的不同時間執(zhí)行.

因此,我想在同一個通道中執(zhí)行命令.我研究的下一個選項是使用paramikos的invoke_shell()函數(shù)實現(xiàn)交互式shell:

ssh = paramiko.SSHClient()

ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())

ssh.connect(host, username=user, password=psw, port=22)

channel = ssh.invoke_shell()

out = channel.recv(9999)

channel.send('cd mivne_final\n')

channel.send('ls\n')

while not channel.recv_ready():

time.sleep(3)

out = channel.recv(9999)

print(out.decode("ascii"))

channel.send('cd ..\n')

channel.send('cd or_fail\n')

channel.send('ls\n')

while not channel.recv_ready():

time.sleep(3)

out = channel.recv(9999)

print(out.decode("ascii"))

channel.send('cd ..\n')

channel.send('cd simulator\n')

channel.send('ls\n')

while not channel.recv_ready():

time.sleep(3)

out = channel.recv(9999)

print(out.decode("ascii"))

ssh.close()

這段代碼存在一些問題:

>第一次打印并不總是打印ls輸出(有時它只打印在第二次打印時).

>第一個cd和ls命令始終存在于輸出中(我通過recv命令獲取它們,作為輸出的一部分),而有時會打印以下所有cd和ls命令,有時它們不會.

>第二個和第三個cd和ls命令(打印時)總是出現(xiàn)在第一個ls輸出之前.

我對這種“非決定論”感到困惑,非常感謝你的幫助.

最佳答案

import paramiko

import re

class ShellHandler:

def __init__(self, host, user, psw):

self.ssh = paramiko.SSHClient()

self.ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())

self.ssh.connect(host, username=user, password=psw, port=22)

channel = self.ssh.invoke_shell()

self.stdin = channel.makefile('wb')

self.stdout = channel.makefile('r')

def __del__(self):

self.ssh.close()

def execute(self, cmd):

"""

:param cmd: the command to be executed on the remote computer

:examples: execute('ls')

execute('finger')

execute('cd folder_name')

"""

cmd = cmd.strip('\n')

self.stdin.write(cmd + '\n')

finish = 'end of stdOUT buffer. finished with exit status'

echo_cmd = 'echo {} $?'.format(finish)

self.stdin.write(echo_cmd + '\n')

shin = self.stdin

self.stdin.flush()

shout = []

sherr = []

exit_status = 0

for line in self.stdout:

if str(line).startswith(cmd) or str(line).startswith(echo_cmd):

# up for now filled with shell junk from stdin

shout = []

elif str(line).startswith(finish):

# our finish command ends with the exit status

exit_status = int(str(line).rsplit(maxsplit=1)[1])

if exit_status:

# stderr is combined with stdout.

# thus, swap sherr with shout in a case of failure.

sherr = shout

shout = []

break

else:

# get rid of 'coloring and formatting' special characters

shout.append(re.compile(r'(\x9B|\x1B\[)[0-?]*[ -/]*[@-~]').sub('', line).

replace('\b', '').replace('\r', ''))

# first and last lines of shout/sherr contain a prompt

if shout and echo_cmd in shout[-1]:

shout.pop()

if shout and cmd in shout[0]:

shout.pop(0)

if sherr and echo_cmd in sherr[-1]:

sherr.pop()

if sherr and cmd in sherr[0]:

sherr.pop(0)

return shin, shout, sherr

總結

以上是生活随笔為你收集整理的python ssh shell交互_使用Paramiko在Python上用ssh实现交互式shell?的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內容還不錯,歡迎將生活随笔推薦給好友。