服务器在线 - 服务器软件 - 网站地图 服务器在线,专注于服务器技术!

当前位置:主页 > 编程脚本 > Python > 正文

flask制作一个批量连接ssh的页面

时间:2023-05-19    来源:未知    投稿:admin    点击:

python文件:

from flask import Flask, render_template, request
import paramiko

app = Flask(__name__)

def test_ssh_connection(ip, port, username, password):
    try:
        ssh = paramiko.SSHClient()
        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        ssh.connect(ip, port=port, username=username, password=password, timeout=5)
        ssh.close()
        return {"ip": ip, "port": port, "result": "连接成功"}
    except paramiko.AuthenticationException:
        return {"ip": ip, "port": port, "result": "身份验证失败"}
    except paramiko.SSHException as e:
        return {"ip": ip, "port": port, "result": f"SSH connection error: {str(e)}"}
    except Exception as e:
        return {"ip": ip, "port": port, "result": f"Error connecting: {str(e)}"}

def parse_host_info(host_info):
    hosts = []
    lines = host_info.splitlines()
    for line in lines:
        line = line.strip()
        if line:
            ip, port, username, password = line.split(',')
            hosts.append((ip, int(port), username, password))
    return hosts

@app.route('/', methods=['GET', 'POST'])
def index():
    if request.method == 'POST':
        host_info = request.form['host_info']
        hosts = parse_host_info(host_info)

        results = []
        for host in hosts:
            ip, port, username, password = host
            result = test_ssh_connection(ip, port, username, password)
            results.append(result)

        return render_template('index.html', results=results)
    return render_template('index.html')

if __name__ == '__main__':
    app.run(debug=True,host="0.0.0.0",port=801)



templates 模板index.html文件
<!DOCTYPE html>
<html>
<head>
    <title>SSH Connection Test</title>
</head>
<body>
    <h1>SSH Connection Test</h1>
    <form method="post">
        <label for="host_info">Host Information:</label><br>
        <textarea id="host_info" name="host_info" rows="10" cols="50" required></textarea><br>
<br>
        <input type="submit" value="Test Connection">
<br>
<br>
        <small>格式样例:</small>
        <ul>
            <li>IP地址,端口,用户名,密码</li>
            <li>192.168.0.2,22,root,123456</li>
            <li>192.168.0.3,22,root,456789</li>
            <li>...</li>
        </ul>
<br>
    </form>

    {% if results %}
    <h2>Connection Results:</h2>
    <ul>
        {% for result in results %}
        <li>{{ result }}</li>
        {% endfor %}
    </ul>
    {% endif %}
</body>
</html>

如果您的问题仍未解决,还可以加入服务器在线技术交流QQ群:8017413寻求帮助。


相关内容
最新热点内容