2022-05-07 13:18:59 +08:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <pthread.h>
|
|
|
|
|
|
|
|
#define LIBSSH_STATIC 1
|
|
|
|
#include <libssh/libssh.h>
|
|
|
|
|
|
|
|
#define BUFFER_SIZE 1024
|
|
|
|
|
|
|
|
int verify_knownhost(ssh_session session)
|
|
|
|
{
|
|
|
|
enum ssh_known_hosts_e state;
|
|
|
|
unsigned char *hash = NULL;
|
|
|
|
ssh_key srv_pubkey = NULL;
|
|
|
|
size_t hlen;
|
|
|
|
char *hexa;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
rc = ssh_get_server_publickey(session, &srv_pubkey);
|
|
|
|
if (rc < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = ssh_get_publickey_hash(srv_pubkey, SSH_PUBLICKEY_HASH_SHA1, &hash, &hlen);
|
|
|
|
ssh_key_free(srv_pubkey);
|
|
|
|
if (rc < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
state = ssh_session_is_known_server(session);
|
|
|
|
switch (state) {
|
|
|
|
case SSH_KNOWN_HOSTS_OK:
|
|
|
|
/* OK */
|
|
|
|
|
|
|
|
break;
|
|
|
|
case SSH_KNOWN_HOSTS_CHANGED:
|
|
|
|
fprintf(stderr, "Host key for server changed: it is now:\n");
|
|
|
|
ssh_print_hash(SSH_PUBLICKEY_HASH_SHA256, hash, hlen);
|
|
|
|
fprintf(stderr, "For security reasons, connection will be stopped\n");
|
|
|
|
ssh_clean_pubkey_hash(&hash);
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
case SSH_KNOWN_HOSTS_OTHER:
|
|
|
|
fprintf(stderr, "The host key for this server was not found but an other" "type of key exists.\n");
|
|
|
|
fprintf(stderr, "An attacker might change the default server key to" "confuse your client into thinking the key does not exist\n");
|
|
|
|
ssh_clean_pubkey_hash(&hash);
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
case SSH_KNOWN_HOSTS_NOT_FOUND:
|
|
|
|
fprintf(stderr, "Could not find known host file.\n");
|
|
|
|
fprintf(stderr, "If you accept the host key here, the file will be" "automatically created.\n");
|
|
|
|
|
|
|
|
/* FALL THROUGH to SSH_SERVER_NOT_KNOWN behavior */
|
|
|
|
|
|
|
|
case SSH_KNOWN_HOSTS_UNKNOWN:
|
|
|
|
hexa = ssh_get_hexa(hash, hlen);
|
|
|
|
fprintf(stderr, "The server is unknown. Do you trust the host key?\n");
|
|
|
|
fprintf(stderr, "Public key hash: %s\n", hexa);
|
|
|
|
ssh_string_free_char(hexa);
|
|
|
|
ssh_clean_pubkey_hash(&hash);
|
|
|
|
|
|
|
|
rc = ssh_session_update_known_hosts(session);
|
|
|
|
if (rc < 0) {
|
|
|
|
fprintf(stderr, "Error %s\n", strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
case SSH_KNOWN_HOSTS_ERROR:
|
|
|
|
fprintf(stderr, "Error %s", ssh_get_error(session));
|
|
|
|
ssh_clean_pubkey_hash(&hash);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
ssh_clean_pubkey_hash(&hash);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-05-14 16:27:28 +08:00
|
|
|
// 交互式外壳会话
|
|
|
|
int interactive_shell_session(ssh_session session, ssh_channel channel)
|
2022-05-07 13:18:59 +08:00
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
|
2022-05-14 16:27:28 +08:00
|
|
|
rc = ssh_channel_request_pty(channel);
|
|
|
|
if (rc != SSH_OK)
|
|
|
|
return rc;
|
2022-05-07 13:18:59 +08:00
|
|
|
|
2022-05-14 16:27:28 +08:00
|
|
|
rc = ssh_channel_change_pty_size(channel, 80, 24);
|
|
|
|
if (rc != SSH_OK)
|
2022-05-07 13:18:59 +08:00
|
|
|
return rc;
|
2022-05-14 16:27:28 +08:00
|
|
|
|
|
|
|
rc = ssh_channel_request_shell(channel);
|
|
|
|
if (rc != SSH_OK)
|
|
|
|
return rc;
|
|
|
|
|
|
|
|
char buffer[256];
|
|
|
|
int nbytes, nwritten;
|
|
|
|
|
|
|
|
while (ssh_channel_is_open(channel) && !ssh_channel_is_eof(channel)) {
|
|
|
|
struct timeval timeout;
|
|
|
|
ssh_channel in_channels[2], out_channels[2];
|
|
|
|
fd_set fds;
|
|
|
|
int maxfd;
|
|
|
|
|
|
|
|
timeout.tv_sec = 30;
|
|
|
|
timeout.tv_usec = 0;
|
|
|
|
in_channels[0] = channel;
|
|
|
|
in_channels[1] = NULL;
|
|
|
|
FD_ZERO(&fds);
|
|
|
|
FD_SET(0, &fds);
|
|
|
|
FD_SET(ssh_get_fd(session), &fds);
|
|
|
|
maxfd = ssh_get_fd(session) + 1;
|
|
|
|
|
|
|
|
ssh_select(in_channels, out_channels, maxfd, &fds, &timeout);
|
|
|
|
|
|
|
|
if (out_channels[0] != NULL) {
|
|
|
|
nbytes = ssh_channel_read(channel, buffer, sizeof(buffer), 0);
|
|
|
|
if (nbytes < 0)
|
|
|
|
return SSH_ERROR;
|
|
|
|
if (nbytes > 0) {
|
|
|
|
nwritten = write(1, buffer, nbytes);
|
|
|
|
if (nwritten != nbytes)
|
|
|
|
return SSH_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (FD_ISSET(0, &fds)) {
|
|
|
|
nbytes = read(0, buffer, sizeof(buffer));
|
|
|
|
if (nbytes < 0)
|
|
|
|
return SSH_ERROR;
|
|
|
|
if (nbytes > 0) {
|
|
|
|
nwritten = ssh_channel_write(channel, buffer, nbytes);
|
|
|
|
if (nbytes != nwritten)
|
|
|
|
return SSH_ERROR;
|
|
|
|
}
|
|
|
|
}
|
2022-05-07 13:18:59 +08:00
|
|
|
}
|
|
|
|
|
2022-05-14 16:27:28 +08:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 执行远程命令
|
|
|
|
int execute_remote_command(ssh_channel channel, char *command)
|
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
char buffer[256];
|
|
|
|
int nbytes;
|
|
|
|
|
2022-05-07 13:18:59 +08:00
|
|
|
rc = ssh_channel_request_exec(channel, command);
|
|
|
|
if (rc != SSH_OK) {
|
|
|
|
ssh_channel_close(channel);
|
|
|
|
ssh_channel_free(channel);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
nbytes = ssh_channel_read(channel, buffer, sizeof(buffer), 0);
|
|
|
|
while (nbytes > 0) {
|
|
|
|
if (write(1, buffer, nbytes) != (unsigned int)nbytes) {
|
|
|
|
ssh_channel_close(channel);
|
|
|
|
ssh_channel_free(channel);
|
|
|
|
return SSH_ERROR;
|
|
|
|
}
|
|
|
|
nbytes = ssh_channel_read(channel, buffer, sizeof(buffer), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nbytes < 0) {
|
|
|
|
ssh_channel_close(channel);
|
|
|
|
ssh_channel_free(channel);
|
|
|
|
return SSH_ERROR;
|
|
|
|
}
|
|
|
|
|
2022-05-14 16:27:28 +08:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
int show_remote_processes(ssh_session session, char *command, int is)
|
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
ssh_channel channel;
|
|
|
|
|
|
|
|
// 分配一个新的信道
|
|
|
|
channel = ssh_channel_new(session);
|
|
|
|
if (channel == NULL)
|
|
|
|
return SSH_ERROR;
|
|
|
|
|
|
|
|
// 分配好的信道
|
|
|
|
rc = ssh_channel_open_session(channel);
|
|
|
|
if (rc != SSH_OK) {
|
|
|
|
ssh_channel_free(channel);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is)
|
|
|
|
{
|
|
|
|
interactive_shell_session(session, channel);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
execute_remote_command(channel, command);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-05-07 13:18:59 +08:00
|
|
|
ssh_channel_send_eof(channel);
|
|
|
|
ssh_channel_close(channel);
|
|
|
|
ssh_channel_free(channel);
|
|
|
|
|
|
|
|
return SSH_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
int authenticate_pubkey(ssh_session session, char *priv_name, char *passphrase)
|
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
ssh_key pkey = NULL;
|
|
|
|
|
|
|
|
//从文件中个导入密钥
|
|
|
|
rc = ssh_pki_import_privkey_file(priv_name, passphrase, NULL, NULL, &pkey);
|
|
|
|
if (rc != SSH_OK) {
|
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
ssh_userauth_publickey(session, NULL, pkey);
|
|
|
|
|
|
|
|
ssh_key_free(pkey);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
int rsa_file()
|
|
|
|
{
|
2022-05-14 16:27:28 +08:00
|
|
|
char rsa[512] = { 0 };
|
|
|
|
|
|
|
|
char temp[] = "./aes/aes -d \"FE67236A61D8D67FD4DEBFD46C9E409B94326781DAE97A7DCD84426711D4D0E078EBA98886342F1E8568828843886DCC38487ACDC9C413D73A4E0C80E6070D054FD5E0DCC0771452FC5E31FCF715EFC62177EABE9014E7F9623B4EA843A93A9020A268F9E8A300BB5D30AD22D52F6EA441222205DAF5560A82F18F28075C035205BF4A47E8E350A957B1F7E90AB63AC2FCCCAC23E1C9C3B50F34B32CDBC7BC8B2155475FBC0B794089C60BFA0EFDD2E096D65DAF31A40E31A5D2C1547A4EA622241EEDD9307CB74BFB3C82E5F48FB1A2CA6D5B223BBB9807B6E54487F4F116D7D9EA2320BFE025ED8B88DBF13F79884C8ACB9161793D59D527579319173EC5A0784398C81435FB0536C048AE656F54422BC41207E0DD3E0B66DB70D725B644915D967D9BA56FC9FE1BF8D628D00D5F6835E48CF9C2B9BCB995EC84D881444F0269272FEE8D4FD667A6C909ADB500FB83E25DB45C9CE75044D2C052E72271665BC37AF036D4A81A889BB8A211B4B9730C0ECC9C70B8B4A07BC4D5A2C274361517DE7C8E87E1AC0A66F3D99F593BE77F1B1343323C92F85B4801957E0F32CAB604252028ACB779E1DCA318A8A9F367F7BF9B4D7CDFEC06D878AF6D4EB4BD7F564BA7134479721CECA232CB0C0FD0F63C6FCC63EA393BA06E13FCF691FC40939B6B332842D8046EBC8049C0971FADACBBE3D28C80FB2F59FFCE1482D050FF3BDB22EF9E67295EB3C1F045EF3CB0F638CE5437E20F8126725E5DF1A867F59407795288B5E69385BDED43E1484886B08BCA06A84938F6BED0CF1326BA149FD3E45E533E3AC48E222025CBD27A33DD6A9BF46F35F601CE8E54DBD6E0ECECE1ECC8D6091F86836C781AC5D21E390FFF2CAAD3FA72497548365E77981C24707B3206E679035D668AE66245EA900FE533E8B935C84D5755ECA88AF396B52C7CBEDB448860D826DD0BEE671F07DE374AE31E71883AF95A45F38066C5BC78C49C783B85395A23627BF202397143F556DE4FB0C39977824834C4716C44435FC5B6286BC14E5D3DFE4C7133A88772509A54C80CCCE45433F7271E6434E37BB20754CA8C9462499BC4DA5E2010B06B05514F5FE7532E873D8918C594D28E2446D074B205589F1A50933EAE1165C48632CC110193D3CA6C6CE001E137A218AA9D09EA918881B5635BCADE6670CD061D7D9302561A5B7D4D752A67FAE30C30FD9D1C7CF83D7700ABD455CA2E190183361F9CF25D963E51CBC601E0FC9D752D45E0A72ACBDEE319927F72D023DB94BEB5DFF2153CC4D6187406A44FF8DEFD7045B3E5DF08A06851CF9B462804F339EB96F7D68BCEE2A3860E5DFD26C2884C121DCF3168185CB261FFE3C6F15343CC4769D0632214CC77EF6313D290D7F42CF317DD90FA6CC98983318863FB2A4C43E231F043CDEEF59C305F06B6AF08683B681CD6C7D7CB6B56EE778ED9FD2790C3D34C3754BD7E8A243D4813542E1DF34D127754F93A205F7889E14D9B21E1760318152E6E67FA608D1610CC1894A127595AF80A939A0FD1A18B5662F79AF3CB0EF0276767DFB062A1BD35CA95912F0B31D5951E0A4245856DDE9F11A782148ACC56F38D866B5505E5DBC2FA1204469AB5D44F996DFEE88F35E47ED8206EA5B537A77C175FAB0A392E6FA8C2BF69D63593D7F78CE72145228BD1CDC1ECC051869DF09CEC01DA31F997250D953745A28C61EB7EB379010518D83F8E1A27A81EA78C84DC7D12F64593D3051A5DA89CCF88580E4CE752F6A528041ADF62268F84F09A5CA7E66F6C4AA3E1FC481813C614BF4F783F5E9AC0846178DA1A7EB24BE1B0620826E34D4AC1C72F820DACBDA40294272093FCA2765DC7C052CCDA64A3C7998E0ADBD118FECC3260529D44D4B10BDA8CE9C9AAD5D15D799B0EB2B4976CBE99B78E4068C1105924D2CCC08FCE95B5963ABDF316D47417D7C73510450386565BDED107F4F278B6CA29D5109C5324CEEFA28A5B84F843932991DE2D1DF828916A097A53B233DF1B82DC084D624BF7F35BF0E2A551EFBCDD0A0B8324A68636B9B72BA68D1A1C6A89B7A736BC82381AE3CD6B4A5CCE0ED35C2B17ADD63BA8C908256AE5297AEC9D9ABD2EBE95A43617383F420AF893752B4B03463AAB3B88E3F835FD19504A615AFC739995D93960987235CA0FBAE7D9626045B2E6382521C4CF01C07AC26E9E1C5CFEAF782AD97E983F3A43F8E90314AC32C60D44AF7CBB1EC74C8BB5139526B2A577BCC2558503842F3AD9A2026D2ED104D462D035AB2CC3802D7136DFC938067F4EFD38ECFDBAE8FE30BC3342D3354628F0F87EDCD5F6A005F69A32A30DA6C6AC2188C3A11790867797D2D03D836B9F6FFD55C590F1AC8DF4A0F63B5201987C9AA05B4EAA91466E4D5454549E184306EB59381CAA20A3B1EFE7EFFC97A98A48E8712A0A98B4E39F050BFF5EFB72917E60DD1992DFC77840F00862FA81D34FBF774AB060B80E0805E63C65CE0B5651256D42A8376FD734BEECE02A58651CC8F567EAD91C0C60690D8C9104205A338B3DAB0F75221BE9AE505CFCB2343F9FD68CC79F96795C8ED06866B32CF12456C30232CDF5D1F8EB253777264EF7D3706E875DF54FC216B2DC90C6653D813E9135288730FC6A9178079D6E89694D9CC4CF828F06AE954224402B1FE2BBBA5A6DECA5344DD11DB77AE49D0BB3DA38FCACC15F8AD1BC5A6212F93F2AD285F5915A4AD642239CDB3240CD622E4DBEE18366BB2CB4604F92D8EFC693C3AB2F90AD84FCFD88CB461C3DF89043E94EFBF08FED2B98C5835CBFC3EDDBA653A33D35BA2E2D08F7ADA87A12FAF76DF84D170BD92A56E62DE8B8B0D1CCAEB4DB4BF7D2FA9F8F23A8443638D721BCCDD31D5396EE2BD5FB3F28BC8
|
2022-05-07 13:18:59 +08:00
|
|
|
|
2022-05-09 10:00:07 +08:00
|
|
|
FILE *pfd = popen(temp, "r");
|
|
|
|
if (pfd == NULL)
|
2022-05-07 13:18:59 +08:00
|
|
|
perror("popen");
|
|
|
|
|
|
|
|
FILE *rfd = fopen("aixiao.rsa", "w+");
|
|
|
|
fseek(rfd, 0, SEEK_SET);
|
2022-05-14 16:27:28 +08:00
|
|
|
|
|
|
|
while (fgets(rsa, 512, pfd) != NULL) {
|
2022-05-09 10:00:07 +08:00
|
|
|
//printf("%s", rsa);
|
|
|
|
fwrite(rsa, strlen(rsa), 1, rfd);
|
|
|
|
}
|
2022-05-14 16:27:28 +08:00
|
|
|
|
2022-05-09 10:00:07 +08:00
|
|
|
fclose(pfd);
|
2022-05-07 13:18:59 +08:00
|
|
|
fclose(rfd);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void help_information() {
|
|
|
|
puts("\n"
|
|
|
|
"remote command\n"
|
|
|
|
"Author: aixiao@aixiao.me\n"
|
|
|
|
"Usage: [-?hlfpkib]\n"
|
|
|
|
"\n"
|
|
|
|
" Options:\n"
|
2022-05-09 10:00:07 +08:00
|
|
|
" -l : host name\n"
|
2022-05-14 16:27:28 +08:00
|
|
|
" -p : host port\n"
|
2022-05-09 10:00:07 +08:00
|
|
|
" -u : user name(default: root)\n"
|
2022-05-14 16:27:28 +08:00
|
|
|
" -e : user passwd\n"
|
2022-05-07 13:18:59 +08:00
|
|
|
" -k : private key passwd\n"
|
2022-05-14 16:27:28 +08:00
|
|
|
" -f : private key file\n"
|
|
|
|
" -i : interactive shell session\n"
|
|
|
|
" -c : command\n"
|
2022-05-07 13:18:59 +08:00
|
|
|
" -? -h : help information\n");
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char *argv[], char **env)
|
|
|
|
{
|
|
|
|
|
|
|
|
int rc;
|
2022-05-14 16:27:28 +08:00
|
|
|
int is=0;
|
2022-05-07 13:18:59 +08:00
|
|
|
int verbosity = SSH_LOG_PROTOCOL;
|
|
|
|
ssh_session my_ssh_session;
|
|
|
|
|
|
|
|
char host_ip[BUFFER_SIZE];
|
|
|
|
char password[BUFFER_SIZE];
|
2022-05-09 10:00:07 +08:00
|
|
|
char user[BUFFER_SIZE];
|
2022-05-14 16:27:28 +08:00
|
|
|
char *priv_name = "aixiao.rsa"; // 私钥文件名称
|
|
|
|
char priv_passwd[BUFFER_SIZE]; // 私钥的解密密钥
|
2022-05-07 13:18:59 +08:00
|
|
|
char command[BUFFER_SIZE];
|
|
|
|
int host_port = 22;
|
|
|
|
|
|
|
|
memset(host_ip, 0, BUFFER_SIZE);
|
|
|
|
memset(password, 0, BUFFER_SIZE);
|
2022-05-09 10:00:07 +08:00
|
|
|
memset(user, 0, BUFFER_SIZE);
|
2022-05-07 13:18:59 +08:00
|
|
|
memset(priv_passwd, 0, BUFFER_SIZE);
|
|
|
|
memset(command, 0, BUFFER_SIZE);
|
2022-05-14 16:27:28 +08:00
|
|
|
|
2022-05-09 10:00:07 +08:00
|
|
|
strcpy(user, "root");
|
2022-05-07 13:18:59 +08:00
|
|
|
|
|
|
|
int opt;
|
2022-05-14 16:27:28 +08:00
|
|
|
char optstrs[] = "-:l:p:u:e:k:f:ic:h?";
|
2022-05-07 13:18:59 +08:00
|
|
|
while (-1 != (opt = getopt(argc, argv, optstrs))) {
|
|
|
|
switch (opt) {
|
|
|
|
case 'l':
|
|
|
|
strcpy(host_ip, optarg);
|
|
|
|
break;
|
2022-05-14 16:27:28 +08:00
|
|
|
case 'p':
|
2022-05-07 13:18:59 +08:00
|
|
|
host_port = atoi(optarg);
|
|
|
|
break;
|
2022-05-09 10:00:07 +08:00
|
|
|
case 'u':
|
|
|
|
strcpy(user, optarg);
|
|
|
|
break;
|
2022-05-14 16:27:28 +08:00
|
|
|
case 'e':
|
2022-05-07 13:18:59 +08:00
|
|
|
strcpy(password, optarg);
|
|
|
|
break;
|
|
|
|
case 'k':
|
|
|
|
memset(password, 0, BUFFER_SIZE);
|
|
|
|
strcpy(priv_passwd, optarg);
|
|
|
|
rsa_file();
|
|
|
|
break;
|
2022-05-14 16:27:28 +08:00
|
|
|
case 'f':
|
2022-05-07 13:18:59 +08:00
|
|
|
priv_name = optarg;
|
|
|
|
break;
|
2022-05-14 16:27:28 +08:00
|
|
|
case 'i':
|
|
|
|
is = 1;
|
|
|
|
break;
|
|
|
|
case 'c':
|
2022-05-07 13:18:59 +08:00
|
|
|
strcpy(command, optarg);
|
|
|
|
break;
|
|
|
|
case ':':
|
|
|
|
printf("\nMissing argument after: -%c\n", optopt);
|
2022-05-09 10:00:07 +08:00
|
|
|
exit(0);
|
2022-05-07 13:18:59 +08:00
|
|
|
case 'h':
|
|
|
|
case '?':
|
|
|
|
help_information();
|
|
|
|
default:
|
|
|
|
help_information();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
my_ssh_session = ssh_new();
|
2022-05-14 16:27:28 +08:00
|
|
|
if (my_ssh_session == NULL)
|
|
|
|
{
|
2022-05-07 13:18:59 +08:00
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
ssh_options_set(my_ssh_session, SSH_OPTIONS_HOST, host_ip);
|
|
|
|
ssh_options_set(my_ssh_session, SSH_OPTIONS_LOG_VERBOSITY, &verbosity);
|
|
|
|
ssh_options_set(my_ssh_session, SSH_OPTIONS_PORT, &host_port);
|
|
|
|
|
|
|
|
rc = ssh_connect(my_ssh_session);
|
|
|
|
|
|
|
|
// Verify the server's identity
|
2022-05-14 16:27:28 +08:00
|
|
|
if (verify_knownhost(my_ssh_session) < 0)
|
|
|
|
{
|
2022-05-07 13:18:59 +08:00
|
|
|
ssh_disconnect(my_ssh_session);
|
|
|
|
ssh_free(my_ssh_session);
|
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
|
2022-05-14 16:27:28 +08:00
|
|
|
if (strlen(password) > 1)
|
|
|
|
{
|
2022-05-07 13:18:59 +08:00
|
|
|
// Authenticate ourselves
|
2022-05-09 10:00:07 +08:00
|
|
|
rc = ssh_userauth_password(my_ssh_session, user, password);
|
2022-05-07 13:18:59 +08:00
|
|
|
if (rc != SSH_AUTH_SUCCESS) {
|
|
|
|
fprintf(stderr, "Error authenticating with password: %s\n", ssh_get_error(my_ssh_session));
|
|
|
|
ssh_disconnect(my_ssh_session);
|
|
|
|
ssh_free(my_ssh_session);
|
|
|
|
exit(-1);
|
|
|
|
}
|
2022-05-14 16:27:28 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-05-07 13:18:59 +08:00
|
|
|
// 私钥认证
|
|
|
|
authenticate_pubkey(my_ssh_session, priv_name, priv_passwd);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 传递远程命令
|
2022-05-14 16:27:28 +08:00
|
|
|
show_remote_processes(my_ssh_session, command, is);
|
2022-05-07 13:18:59 +08:00
|
|
|
|
|
|
|
// 从一个会话断开连接(服务器或客户端)
|
|
|
|
ssh_disconnect(my_ssh_session);
|
|
|
|
// 释放已分配的SSH会话句柄
|
|
|
|
ssh_free(my_ssh_session);
|
2022-05-14 16:27:28 +08:00
|
|
|
|
2022-05-07 13:18:59 +08:00
|
|
|
system("test -f aixiao.rsa && rm aixiao.rsa");
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|