代码拉取完成,页面将自动刷新
同步操作将从 ningbo/ssh群控 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
#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);
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。