1 Star 0 Fork 1

notanoutman/ssh集群管理

forked from ningbo/ssh群控 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
widgetssh.cpp 3.99 KB
一键复制 编辑 原始数据 按行查看 历史
ningbo 提交于 2021-04-09 21:22 . V1.0
#include "widgetssh.h"
#include "ui_widgetssh.h"
#include <QProcess>
#include <QTimer>
#include<QDebug>
#include<ssherrors.h>
#include<QDebug>
#include<QTextCodec>
WidgetSsh::WidgetSsh(QWidget *parent) :
QWidget(parent),
ui(new Ui::WidgetSsh)
{
ui->setupUi(this);
m_connection = nullptr,
ui->lineEdit_ip->setText("192.168.137.58");
ui->lineEdit_name->setText("guest");
ui->lineEdit_pass->setText("11111111");
}
WidgetSsh::~WidgetSsh()
{
delete ui;
delete m_connection;
}
void WidgetSsh::on_pushButton_clicked()
{
SshConnectionParameters parameters;
parameters.port = 22;
parameters.userName = ui->lineEdit_name->text();
parameters.password = ui->lineEdit_pass->text();
parameters.host = ui->lineEdit_ip->text();
parameters.timeout = 10;
parameters.authenticationType = QSsh::SshConnectionParameters::AuthenticationTypePassword;
if(m_connection == nullptr) {
m_connection = new SshConnection(parameters);
connect(m_connection, SIGNAL(connected()),this,SLOT(handleConnected()));
connect(m_connection,SIGNAL(error(QSsh::SshError)),this,SLOT(slotSshConnectError(QSsh::SshError)));
}
m_connection->connectToHost();
}
void WidgetSsh::slotSshConnectError(QSsh::SshError msg)
{
qDebug()<<msg;
}
void WidgetSsh::handleConnected()
{
//传送文件
m_channel = m_connection->createSftpChannel();
connect(m_channel.data(), SIGNAL(initialized()), this, SLOT(handleChannelInitialized()));
connect(m_channel.data(), SIGNAL(channelError(QString)), this, SLOT(handleChannelInitializationFailure(QString)));
//初始化传送文件
m_channel->initialize();
//执行shell,这里先不执行,所以未start
m_shell = m_connection->createRemoteShell();
connect(m_shell.data(), SIGNAL(started()), SLOT(handleShellStarted()));
connect(m_shell.data(), SIGNAL(readyReadStandardOutput()), SLOT(handleRemoteStdout()));
connect(m_shell.data(), SIGNAL(readyReadStandardError()), SLOT(handleRemoteStderr()));
}
void WidgetSsh::handleRemoteStdout()
{
QString str_out = QString::fromUtf8(m_shell->readAllStandardOutput());
QTimer timer;
qDebug()<< "str out" << str_out;
ui->textBrowser->append(str_out);
timer.start(1000);
if(timer.isActive()) timer.stop();
if(str_out.contains("password for"))
m_shell->write(ui->lineEdit_pass->text().toLatin1().data());
}
void WidgetSsh::handleShellStarted()
{
// m_shell->write(QString("sudo xxxx\n").toLatin1().data());
qDebug()<<"CConnectionForSshClient::slotShellStart Shell已连接:";
}
void WidgetSsh::handleRemoteStderr()
{
qDebug() << "shell standard err"<< QString::fromLocal8Bit(m_shell->readAllStandardError());
}
void WidgetSsh::handleChannelInitialized()
{
//传送到服务器的tmp目录下,可以传送任意类型文件
m_channel->uploadFile(QString("ssh.txt").toLatin1().data(), "/tmp/ssh.txt", SftpOverwriteExisting);
//传送完成,开始执行指令
QTimer timer;
timer.start(5000);
if(timer.isActive()) timer.stop();
m_shell->start();
}
void WidgetSsh::handleChannelInitializationFailure(const QString &reason)
{
qDebug() << "Could not initialize SFTP channel: " << reason;
}
void WidgetSsh::slotDataReceived()
{
QByteArray byteRecv = m_shell->readAllStandardOutput();
QString strRecv = QString::fromUtf8(byteRecv);
qDebug()<<strRecv;
ui->textBrowser->append(strRecv);
// if(strRecv.contains("password for")){
// m_shell->write(m_strPwd.toLatin1().data());
// }
// if(!strRecv.isEmpty()) //过滤空行
// emit sigDataArrived(strRecv, m_strIp, m_nPort);
}
void WidgetSsh::on_pushButton_2_clicked()
{
QByteArray msg=QByteArray::fromStdString(ui->msg->text().toStdString()+"\n");
m_shell->write(msg);
}
void WidgetSsh::on_msg_returnPressed()
{
QByteArray msg=QByteArray::fromStdString(ui->msg->text().toStdString()+"\n");
m_shell->write(msg);
}
void WidgetSsh::on_pushButton_3_clicked()
{
ui->msg->move(ui->msg->pos().x(),ui->msg->pos().y()+5);
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/notanoutman/ssh-cluster-management.git
git@gitee.com:notanoutman/ssh-cluster-management.git
notanoutman
ssh-cluster-management
ssh集群管理
master

搜索帮助