- dockerfile
# 【生产构建】开始
FROM docker.io/centos
RUN useradd -d /home/workspace lee
# 【生产构建】构建sshd服务 openssh-server*
RUN yum install glibc-headers cpp libgomp glibc-devel gcc zlib zlib-devel make openssl openssl-devel libssl-dev libffi libffi-devel openssh-server* pcre pcre-devel -y
# 【开发构建】关于sshd_config
RUN sed -i 's/#Port 22/Port 22/g' /etc/ssh/sshd_config
RUN sed -i 's/#ListenAddress 0.0.0.0/ListenAddress 0.0.0.0/g' /etc/ssh/sshd_config
RUN sed -i 's/#HostKey \/etc\/ssh\/ssh_host_rsa_key/HostKey \/etc\/ssh\/ssh_host_rsa_key/g' /etc/ssh/sshd_config
RUN sed -i 's/HostKey \/etc\/ssh\/ssh_host_dsa_key/#HostKey \/etc\/ssh\/ssh_host_dsa_key/g' /etc/ssh/sshd_config
RUN sed -i 's/HostKey \/etc\/ssh\/ssh_host_ecdsa_key/#HostKey \/etc\/ssh\/ssh_host_ecdsa_key/g' /etc/ssh/sshd_config
RUN sed -i 's/HostKey \/etc\/ssh\/ssh_host_ed25519_key/#HostKey \/etc\/ssh\/ssh_host_ed25519_key/g' /etc/ssh/sshd_config
# 【生产构建】下载Python3.7.0
ADD Python-3.7.0.tgz /srv
RUN cd /srv/Python-3.7.0 && ./configure --prefix=/usr/local/python3 && make && make install && mv /usr/bin/python /usr/bin/python.bak && ln -s /usr/local/python3/bin/python3 /usr/bin/python
# 【生产构建】下载所需 python 包 Eviews
RUN /usr/local/python3/bin/pip3 install Tornado
RUN /usr/local/python3/bin/pip3 install pymysql
# 【开发构建】生成 ssh rsa 密钥与公钥
RUN echo 123456 | passwd --stdin root
RUN /usr/local/python3/bin/pip3 install pexpect
ADD ssh_keygen_auto.py /srv
RUN python /srv/ssh_keygen_auto.py
RUN mkdir /home/workspace/bin/
RUN echo -e "#!/bin/bash\n/usr/sbin/sshd -D\n/bin/bash" > /home/workspace/bin/docker_init
# 【生产构建】删除多余包
#【生产构建】暴露端口
EXPOSE 22
#设置默认启动的命令
ENV PATH /home/workspace/bin/:$PATH
CMD ["docker_init"]
- ssh_keygen_auto.py
# -*- coding:utf-8 -*-
#/usr/local/python3/bin/python3
import pexpect,sys
if __name__ == '__main__':
command = 'ssh-keygen -t rsa -b 2048'
child = pexpect.spawn(command)
ret = child.expect([pexpect.TIMEOUT,'save the key', pexpect.EOF])
if ret in [0]:
print('error connecting')
child.close(force=True)
if ret == 1:
print('------save the key------')
#child.logfile = sys.stdout
#print(type('/etc/ssh/ssh_host_rsa_key'))
child.sendline('/etc/ssh/ssh_host_rsa_key')
#child.logfile = sys.stdout
ret = child.expect([pexpect.TIMEOUT,'Enter passphrase', pexpect.EOF])
if ret == 1:
print('------Enter passphrase------')
child.sendline('\n\r')
elif ret in [0]:
print('error connecting')
child.close(force=True)
ret = child.expect([pexpect.TIMEOUT,'Enter same passphrase', pexpect.EOF])
if ret == 1:
print('------Enter same passphrase again------')
child.sendline('\n\r')
elif ret in [0]:
print('error connecting')
child.close(force=True)
elif ret in [2]:
print('------ssh key gen ok------')
child.close(force=True)
版权声明:本文为ChangerJJLee原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。