我打算使用paramiko在远程主机上运行几个命令,但运行命令后ssh会话关闭.
下面列出的代码:
from paramiko import SSHClient
import paramiko
ssh = SSHClient()
ssh.load_system_host_keys()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(host, 22, user, passwd, timeout=3)
stdin, stdout, stderr = ssh.exec_command('uname -a')
Run Code Online (Sandbox Code Playgroud)
那么有什么方法可以阻止ssh会话结束?或帕拉米科的任何替代品?
更新:当连接到Linux服务器时,
我能够继续调用exec_command我的Macbook,但是exec_command当连接到交换机并升级时,ssh会话在Linux服务器上自动关闭
SSHException: paramiko.ssh_exception.SSHException: SSH session not active
>>> print ssh.get_transport()
>>> <paramiko.Transport at 0xf00216d0L (unconnected)>
>>> print ssh.get_transport().is_active()
>>> False
>>> print ssh.get_transport().is_authenticated()
>>> False
Run Code Online (Sandbox Code Playgroud)
是否有任何方法可以让paramiko ssh会话始终保持活动状态?
paramiko调试模式信息返回如下:
启动线程(客户端模式):0x2657e10L
连接(版本1.99,客户端Comware-5.20)
kex algos:[u'diffie-hellman-group-exchange-sha1',u'diffie-hellman-group14-sha1',u'diffie- hellman-group1-sha1']服务器密钥:[u'ssh-rsa']客户端加密:[u'aes128-cbc',u'3des-cbc',u'des-cbc']服务器加密:[u'aes128 -cbc',u'3des-cbc',u'des-cbc']客户端mac:[u'hmac-sha1',u'hmac-sha1-96',u'hmac-md5',u'hmac-md5 -96']服务器mac:[u'hmac-sha1',u'hmac-sha1-96',u'hmac-md5',u'hmac-md5-96']客户端压缩:[u'none']服务器压缩:[u'none']客户端lang:[u'']服务器lang:[u''] kex跟随?False
Ciphers同意:local = aes128-cbc,remote = aes128-cbc
using kex diffie-hellman-group14- SHA1; 服务器密钥类型ssh-rsa; 密码:本地aes128-cbc,远程aes128-cbc; …
我打算并行运行多个作业,并按照此处的'NoneType' object is not callable示例使用作业队列,但它执行一次并在我尝试传递参数时引发异常。下面列出了代码:
import Queue
import schedule
import threading
import time
def job(arg):
print 'Argument is %s' % arg
def worker_main():
while True:
try:
job_func = jobqueue.get()
job_func()
except Exception as e:
print e
jobqueue = Queue.Queue()
schedule.every(2).seconds.do(jobqueue.put, job(1))
schedule.every(2).seconds.do(jobqueue.put, job(2))
worker_thread = threading.Thread(target=worker_main)
worker_thread.start()
while True:
try:
schedule.run_pending()
time.sleep(1)
except Exception as e:
print e
sys.exit()
Run Code Online (Sandbox Code Playgroud)
输出是:
Arg is 1
Arg is 2
'NoneType' object is not callable
'NoneType' object is not callable
'NoneType' …Run Code Online (Sandbox Code Playgroud) 在CLI中运行时,我的hava可执行文件a.py文件运行正常.但是在我写了一个a.sh脚本/root/a.py >> /root/a.log并启动一个crontab之后* * * * * /bin/sh /root/a.sh,除了在日志文件中没有输出外,它工作得很好.
a.py的日志记录部分配置如下:
DATE_FORMAT = '%a, %d %b %Y %H:%M:%S'
LOG_FILE = 'log'
RESULT_LOG_FILE = 'result.log'
LOG_FORMAT = '[%(asctime)s] [%(filename)s:%(lineno)d] [%(levelname)s] [%(threadName)s] [%(process)d] %(message)s'
logging.basicConfig(format=LOG_FORMAT, datefmt=DATE_FORMAT, level=logging.INFO, filename=LOG_FILE)
logging.error('ERROR')
Run Code Online (Sandbox Code Playgroud)
我试图在a.sh /usr/local/bin/python前添加 ,/root/a.py但它没有用.我不知道为什么会这样.
python ×3
python-2.7 ×2
adapter ×1
centos6 ×1
crontab ×1
linux ×1
networking ×1
paramiko ×1
queue ×1
scheduler ×1
shell ×1
ssh ×1
switching ×1
windows ×1
windows-10 ×1