184 ssh连接实现域名alias

7 篇文章 0 订阅

ssh连接实现域名alias


1.(windows)修改host文件

C:\Windows\System32\drivers\etc\host

# For example:

     102.54.94.97     rhino.acme.com          # source server
       38.25.63.10     x.acme.com              # x client host

# localhost name resolution is handled within DNS itself.
    127.0.0.1       localhost
    ::1             localhost
    102.54.94.97    rhino.acme.com

于是ssh连接
ssh root@102.54.94.97 可以替换为ssh root@rhino.acme.com

2.修改.ssh/config

~/.ssh/config

Host HOST_ALIAS                       # 用于 SSH 连接的别名,最好与 HostName 保持一致
  HostName SERVER_DOMAIN              # 服务器的域名或 IP 地址
  Port SERVER_PORT                    # 服务器的端口号,默认为 22,可选
  User SERVER_USER                    # 服务器的用户名
  PreferredAuthentications publickey
  IdentityFile ~/.ssh/PRIVATE_KEY     # 本机上存放的私钥路径

host j
        hostname www.baidu.cn
        User root
        PreferredAuthentications publickey
        ServerAliveInterval 60
        IdentityFile ~/.ssh/id_rsa

于是
ssh root@www.juhe.cn可替换为ssh j

添加到ssh-agent

有时候ssh会报错

 sign_and_send_pubkey: signing failed: agent refused operation

那么需要

ssh-agent -s
ssh-add
  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在C语言中实现SSH远程连接,你可以使用开源的libssh库。下面是一个简单的示例代码,演示如何连接到远程SSH服务器并执行命令: ```c #include <stdio.h> #include <stdlib.h> #include <libssh/libssh.h> int main() { ssh_session my_ssh_session; int rc; // 创建一个新的SSH会话对象 my_ssh_session = ssh_new(); if (my_ssh_session == NULL) { fprintf(stderr, "Failed to create SSH session\n"); exit(1); } // 设置SSH会话的选项 ssh_options_set(my_ssh_session, SSH_OPTIONS_HOST, "remote_host"); ssh_options_set(my_ssh_session, SSH_OPTIONS_USER, "username"); // 连接到远程SSH服务器 rc = ssh_connect(my_ssh_session); if (rc != SSH_OK) { fprintf(stderr, "Error connecting to remote host: %s\n", ssh_get_error(my_ssh_session)); ssh_free(my_ssh_session); exit(1); } // 进行用户身份验证 rc = ssh_userauth_password(my_ssh_session, NULL, "password"); 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); } // 在远程服务器上执行命令 ssh_channel channel; char buffer[256]; int nbytes; channel = ssh_channel_new(my_ssh_session); if (channel == NULL) { fprintf(stderr, "Failed to create SSH channel\n"); ssh_disconnect(my_ssh_session); ssh_free(my_ssh_session); exit(1); } rc = ssh_channel_open_session(channel); if (rc != SSH_OK) { fprintf(stderr, "Error opening SSH channel: %s\n", ssh_get_error(my_ssh_session)); ssh_channel_free(channel); ssh_disconnect(my_ssh_session); ssh_free(my_ssh_session); exit(1); } rc = ssh_channel_request_exec(channel, "command_to_execute"); if (rc != SSH_OK) { fprintf(stderr, "Error executing command: %s\n", ssh_get_error(my_ssh_session)); ssh_channel_close(channel); ssh_channel_free(channel); ssh_disconnect(my_ssh_session); ssh_free(my_ssh_session); exit(1); } nbytes = ssh_channel_read(channel, buffer, sizeof(buffer), 0); while (nbytes > 0) { if (fwrite(buffer, 1, nbytes, stdout) != nbytes) { fprintf(stderr, "Error writing to stdout\n"); ssh_channel_close(channel); ssh_channel_free(channel); ssh_disconnect(my_ssh_session); ssh_free(my_ssh_session); exit(1); } nbytes = ssh_channel_read(channel, buffer, sizeof(buffer), 0); } if (nbytes < 0) { fprintf(stderr, "Error reading from SSH channel: %s\n", ssh_get_error(my_ssh_session)); ssh_channel_close(channel); ssh_channel_free(channel); ssh_disconnect(my_ssh_session); ssh_free(my_ssh_session); exit(1); } // 关闭SSH通道和会话 ssh_channel_send_eof(channel); ssh_channel_close(channel); ssh_channel_free(channel); ssh_disconnect(my_ssh_session); ssh_free(my_ssh_session); return 0; } ``` 请确保将 "remote_host" 替换为远程SSH服务器的主机名或IP地址,"username" 替换为您的SSH用户名,"password" 替换为您的SSH密码,"command_to_execute" 替换为要在远程服务器上执行的命令。 这只是一个基本示例,libssh库还提供了许多其他功能和选项,你可以根据需要进行进一步的定制和扩展。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值