GVKun编程网logo

在Python Paramiko中的SSH服务器上的辅助Shell /命令中执行(子)命令(python ssh服务端)

14

本文将介绍在PythonParamiko中的SSH服务器上的辅助Shell/命令中执行的详细情况,特别是关于子命令的相关信息。我们将通过案例分析、数据研究等多种方式,帮助您更全面地了解这个主题,同时也

本文将介绍在Python Paramiko中的SSH服务器上的辅助Shell /命令中执行的详细情况,特别是关于子命令的相关信息。我们将通过案例分析、数据研究等多种方式,帮助您更全面地了解这个主题,同时也将涉及一些关于Paramiko 试图在远程服务器上运行 python 脚本、python paramiko ssh、Python paramiko ssh 在同一个session里run多个命令、python paramiko ssh登录交换机执行命令的知识。

本文目录一览:

在Python Paramiko中的SSH服务器上的辅助Shell /命令中执行(子)命令(python ssh服务端)

在Python Paramiko中的SSH服务器上的辅助Shell /命令中执行(子)命令(python ssh服务端)

我在ShoreTel语音开关上遇到问题,并且尝试使用Paramiko跳入并运行一些命令。我认为问题可能是,ShoreTel
CLI给出的提示与标准Linux不同$。它看起来像这样:

server1$:stcliMitel>gotoshellCLI>  (This is where I need to enter ''hapi_debug=1'')

Python是否仍在期待它$,还是我还缺少其他东西?

我认为这可能是一件时事,所以我将它们放在time.sleep(1)命令之间。似乎仍然没有采取。

import paramikoimport timekeyfile = "****"User = "***"ip = "****"command1 = "stcli"command2 = "gotoshell"command4 = "hapi_debug=1"ssh = paramiko.SSHClient()print(''paramikoing...'')ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())ssh.connect(hostname = ip, username = User, key_filename = keyfile)print(''giving er a go...'')ssh.invoke_shell()stdin, stdout, stderr = ssh.exec_command(command1)time.sleep(1)stdin, stdout, stderr = ssh.exec_command(command2)time.sleep(1)stdin, stdout, stderr = ssh.exec_command(command4)time.sleep(1)print(stdout.read())ssh.close()print("complete")

从成功执行此代码中,我期望的hapi_debug级别是1。这意味着当我通过SSH进入东西时,我会看到那些HAPI调试正在填充。当我这样做时,我看不到那些调试。

答案1

小编典典

我假设gotoshellhapi_debug=1不是顶级命令,而是的子命令stcli。换句话说,stcli是一种外壳。

在这种情况下,您需要将要在子shell中执行的命令写到其stdin

stdin, stdout, stderr = ssh.exec_command(''stcli'')stdin.write(''gotoshell\n'')stdin.write(''hapi_debug=1\n'')stdin.flush()

如果您stdout.read随后调用,它将等到命令stcli完成。它永远不会做。如果您想继续读取输出,则需要发送一个终止子shell的命令(通常是exit\n)。

stdin.write(''exit\n'')stdin.flush()print(stdout.read())

Paramiko 试图在远程服务器上运行 python 脚本

Paramiko 试图在远程服务器上运行 python 脚本

如何解决Paramiko 试图在远程服务器上运行 python 脚本?

我有一个名为 run.py 的脚本,位于远程服务器的暂存区。我已成功连接到主机 IP,但在运行“run.py”代码后,它没有按预期执行(当我在本地运行它时它运行良好。我没有看到任何可见的错误。我已经尝试过 stdout.readLines() 但它似乎冻结了。 有人对我可能做错了什么有任何想法吗?

import paramiko
from scp import SCPClient

# declare credentials   
host = ''HOST''
username = ''USER''
password = ''PASSWORD''

# connect to server   
con = paramiko.SSHClient()
con.load_system_host_keys()
con.connect(host,username=username,password=password)


stdin,stdout,stderr = con.exec_command(''cd /staging'')
stdin,stderr = con.exec_command(''python -m run.py'')


# con.close()

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

python paramiko ssh

python paramiko ssh

我是python新手。我写了一个脚本来连接到主机并执行一个命令

ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(host,username=user,password=pw)

print 'running remote command'

stdin,stdout,stderr = ssh.exec_command(command)
stdin.close()

for line in stdout.read().splitlines():
    print '%s$: %s' % (host,line)
    if outfile != None:
        f_outfile.write("%s\n" %line)

for line in stderr.read().splitlines():
    print '%s$: %s' % (host,line + "\n")
    if outfile != None:
        f_outfile.write("%s\n" %line)

ssh.close()

if outfile != None:
    f_outfile.close()

print 'connection to %s closed' %host

except:
   e = sys.exc_info()[1]
   print '%s' %e

当远程命令不需要tty时,可以正常工作。我找到了一个与Paramiko的invoke_shell示例嵌套SSH会话。我对这种解决方案不满意,因为如果服务器上的提示未在我的脚本中指定->无限循环,或者脚本中指定的提示是返回文本中的字符串->不会接收到所有数据。有没有更好的解决方案,也许将stdout和stderr像我的脚本一样发送回去?

Python paramiko ssh 在同一个session里run多个命令

Python paramiko ssh 在同一个session里run多个命令

import threading, paramiko

strdata=''''
fulldata=''''

class ssh:
    shell = None
    client = None
    transport = None

    def __init__(self, address, username, password):
        print("Connecting to server on ip", str(address) + ".")
        self.client = paramiko.client.SSHClient()
        self.client.set_missing_host_key_policy(paramiko.client.AutoAddPolicy())
        self.client.connect(address, username=username, password=password, look_for_keys=False)
        self.transport = paramiko.Transport((address, 22))
        self.transport.connect(username=username, password=password)

        thread = threading.Thread(target=self.process)
        thread.daemon = True
        thread.start()

    def close_connection(self):
        if(self.client != None):
            self.client.close()
            self.transport.close()

    def open_shell(self):
        self.shell = self.client.invoke_shell()

    def send_shell(self, command):
        if(self.shell):
            self.shell.send(command + "\n")
        else:
            print("Shell not opened.")

    def process(self):
        global strdata, fulldata
        while True:
            # Print data when available
            if self.shell is not None and self.shell.recv_ready():
                alldata = self.shell.recv(1024)
                while self.shell.recv_ready():
                    alldata += self.shell.recv(1024)
                strdata = strdata + str(alldata)
                fulldata = fulldata + str(alldata)
                strdata = self.print_lines(strdata) # print all received data except last line

    def print_lines(self, data):
        last_line = data
        if ''\n'' in data:
            lines = data.splitlines()
            for i in range(0, len(lines)-1):
                print(lines[i])
            last_line = lines[len(lines) - 1]
            if data.endswith(''\n''):
                print(last_line)
                last_line = ''''
        return last_line


sshUsername = "SSH USERNAME"
sshPassword = "SSH PASSWORD"
sshServer = "SSH SERVER ADDRESS"


connection = ssh(sshServer, sshUsername, sshPassword)
connection.open_shell()
connection.send_shell(''cmd1'')
connection.send_shell(''cmd2'')
connection.send_shell(''cmd3'')
time.sleep(10)
print(strdata)    # print the last line of received data
print(''=========================='')
print(fulldata)   # This contains the complete data received.
print(''=========================='')
connection.close_connection()

 

python paramiko ssh登录交换机执行命令

python paramiko ssh登录交换机执行命令

 

# encoding=utf-8
import paramiko
import time
client = paramiko.SSHClient()
client.load_system_host_keys()
 
# connect to client
client.connect(''192.168.254.141'',22,''test'',''test'',allow_agent=False,look_for_keys=False)
 
# get shell
ssh_shell = client.invoke_shell()
# ready when line endswith ''>'' or other character
while True:
    line = ssh_shell.recv(1024)
    #print line
    if line and line.endswith(''>''):
        break;
 
# send command
ssh_shell.sendall( ''ping 192.168.254.142'' + ''\n'')
 
# get result lines
lines = []
while True:
    line = ssh_shell.recv(1024)
    if line and line.endswith(''>''):
        break;
    lines.append(line)
result = ''''.join(lines)
 
# print result
print result

 

关于在Python Paramiko中的SSH服务器上的辅助Shell /命令中执行子命令的介绍已经告一段落,感谢您的耐心阅读,如果想了解更多关于Paramiko 试图在远程服务器上运行 python 脚本、python paramiko ssh、Python paramiko ssh 在同一个session里run多个命令、python paramiko ssh登录交换机执行命令的相关信息,请在本站寻找。

本文标签: