Paramiko 列出 SAS 作业(PYTHON)
Paramiko to list SAS Jobs(PYTHON)
早上好。所以我在工作中遇到了这个问题。使用 Paramiko 库,我试图将许多人在工作中为 DB2 实例所做的简单任务自动化。我已经设置了一项工作来重置一堆密码,所以我知道连接到服务器的基础知识是正确的,只是这些命令没有按照我的意愿行事。我想要做的是在 "bjobs" 的第二个命令之后,我希望能够查看输出。我试过使用 stdout.read(),但到目前为止它除了 b'' 什么都没给我。非常需要任何帮助。
from paramiko import client
from os import getlogin
class ssh:
client = None
def __init__(self, address, username, password):
print("Connecting to server")
self.client = client.SSHClient()
self.client.set_missing_host_key_policy(client.AutoAddPolicy())
self.client.connect(address, username=username, password=password)
print("Connected to " + address)
def sendCommand(self, command):
if(self.client):
stdin, stdout, stderr = self.client.exec_command(command)
x= stdout.read()
print(x)
while not stdout.channel.exit_status_ready():
if stdout.channel.recv_ready():
alldata = stdout.channel.recv(1024)
while stdout.channel.recv_ready():
alldata+=stdout.channel.recv(1024)
print(str(alldata, 'utf8'))
else:
print("connection not opened")
serverCon = "My Server"
plist = []
currPass = 'MyPassword!'
#get user information
userName = getlogin()
#Connect to server, insert and chnage passwords
connection = ssh(serverCon, userName, currPass)
connection.sendCommand(r'. /opt/sas/lsf/conf/profile.lsf')
connection.sendCommand('bjobs')
Every exec_command()
将使用 shell 的新实例执行命令(用户的 login shell 在 ssh 服务器上)所以你的第一个 . /opt/sas/lsf/conf/profile.lsf
不会影响后面的 bjobs
。你应该写
exec_command('. /opt/sas/lsf/conf/profile.lsf; bjobs')
与
基本相同
ssh user@host '. /opt/sas/lsf/conf/profile.lsf; bjobs'
根据手册:
class paramiko.client.SSHClient
exec_command(command, bufsize=-1, timeout=None, get_pty=False)
Execute a command on the SSH server. A new Channel is opened and the requested command is executed. The command’s input and output streams are returned as Python file-like objects representing
stdin, stdout, and stderr.
class paramiko.channel.Channel
exec_command(*args, **kwds)
Execute a command on the server. If the server allows it, the channel will then be directly connected to the
stdin, stdout, and stderr of the command being executed.
When the command finishes executing, the channel will be closed and can't be reused. You must open a new channel if you wish to execute another command.
早上好。所以我在工作中遇到了这个问题。使用 Paramiko 库,我试图将许多人在工作中为 DB2 实例所做的简单任务自动化。我已经设置了一项工作来重置一堆密码,所以我知道连接到服务器的基础知识是正确的,只是这些命令没有按照我的意愿行事。我想要做的是在 "bjobs" 的第二个命令之后,我希望能够查看输出。我试过使用 stdout.read(),但到目前为止它除了 b'' 什么都没给我。非常需要任何帮助。
from paramiko import client
from os import getlogin
class ssh:
client = None
def __init__(self, address, username, password):
print("Connecting to server")
self.client = client.SSHClient()
self.client.set_missing_host_key_policy(client.AutoAddPolicy())
self.client.connect(address, username=username, password=password)
print("Connected to " + address)
def sendCommand(self, command):
if(self.client):
stdin, stdout, stderr = self.client.exec_command(command)
x= stdout.read()
print(x)
while not stdout.channel.exit_status_ready():
if stdout.channel.recv_ready():
alldata = stdout.channel.recv(1024)
while stdout.channel.recv_ready():
alldata+=stdout.channel.recv(1024)
print(str(alldata, 'utf8'))
else:
print("connection not opened")
serverCon = "My Server"
plist = []
currPass = 'MyPassword!'
#get user information
userName = getlogin()
#Connect to server, insert and chnage passwords
connection = ssh(serverCon, userName, currPass)
connection.sendCommand(r'. /opt/sas/lsf/conf/profile.lsf')
connection.sendCommand('bjobs')
Every exec_command()
将使用 shell 的新实例执行命令(用户的 login shell 在 ssh 服务器上)所以你的第一个 . /opt/sas/lsf/conf/profile.lsf
不会影响后面的 bjobs
。你应该写
exec_command('. /opt/sas/lsf/conf/profile.lsf; bjobs')
与
基本相同ssh user@host '. /opt/sas/lsf/conf/profile.lsf; bjobs'
根据手册:
class paramiko.client.SSHClient
exec_command(command, bufsize=-1, timeout=None, get_pty=False)
Execute a command on the SSH server. A new Channel is opened and the requested command is executed. The command’s input and output streams are returned as Python file-like objects representing stdin, stdout, and stderr.
class paramiko.channel.Channel
exec_command(*args, **kwds)
Execute a command on the server. If the server allows it, the channel will then be directly connected to the stdin, stdout, and stderr of the command being executed. When the command finishes executing, the channel will be closed and can't be reused. You must open a new channel if you wish to execute another command.