我想 ssh 到远程服务器并运行一些 shell 脚本(如 scp 或 yum 等)。
一切都很顺利,除了我无法连续获得诸如 scp 进度或 yum 下载进度之类的标准输出。将f.read被阻止并且不会返回任何内容。
我猜 paramiko 可能会逐行传输假的标准输出流,但这些渐进输出不会输出行分隔符,而是输出“\r”。
有办法解决这个问题吗?
这就是我现在正在做的事情,在ssh哪里paramiko.SSHClient()
def read_buffer_line(f):
line = ""
while not f.channel.exit_status_ready():
c = f.read(1)
if c == '\n':
yield line
line = ''
else:
line += c
yield line + f.read()
def ssh_run(ssh, cmd):
stdin, stdout, sterr = ssh.exec_command(cmd, get_pty=True, bufsize=1)
for l in read_buffer_line(stdout):
print l
Run Code Online (Sandbox Code Playgroud) 我有一个在 AWS 中运行的 EC2 实例,这是我想要实现的场景。我有一个包含 3 个子网的 VPC 设置。其中 2 个是私有子网,无法访问互联网(即使使用 NAT 网关/NAT 实例),另一个是公共子网。
如果我按照此处讨论的方式设置 SSH 连接,那么它对于在我的堡垒主机上设置的 Web 浏览器页面来说效果非常好。但是,对于我的用例,我需要像上面那样实现另一个级别的 SSH 转发,因为我的应用程序位于私有子网中,以便可以从我的本地计算机访问该应用程序。这有可能吗?我还需要确保 DNS 没有问题。
我知道私钥身份验证有效,但是我正在寻找公钥身份验证。
我正在尝试使用 SSH.NET 和公钥建立连接。
在 Git Bash 终端中,我可以使用公钥很好地连接并运行命令,但是当我尝试使用 SSH.NET 连接到同一主机时,出现异常。
这是我尝试连接的方式:
using (var client = new SshClient("host.name.com", port, "username"))
{
client.Connect(); // exception thrown here
}
Run Code Online (Sandbox Code Playgroud)
我也尝试过这条路线:
var authMethod = new PrivateKeyAuthenticationMethod("username");
var info = new ConnectionInfo("host.name.com", port, "username", authMethod);
using (var client = new SshClient(info))
Run Code Online (Sandbox Code Playgroud)
两者都给出相同的例外:Permission denied (publickey).
似乎我需要指定我的公钥在哪里,或者我需要将我的公钥放在某个位置,但我找不到任何文档告诉我将其放在哪里。
我有一个主存储库,其中包含一个 NPM 包,该包加载另一个私有 NPM 包,这两个包位于 Gitlab 中的同一组织中。
我已经研究了几个小时,发现很多方法都行不通。首先是 Dockerfile,我认为它包含添加 SSH 密钥的最常见方法。
FROM node:10.15.1-alpine as image
WORKDIR /usr/src/app
RUN apk add --update --no-cache openssh git
COPY package.json ./
ARG SSH_PRIVATE_KEY
RUN mkdir /root/.ssh/ && \
echo "$SSH_PRIVATE_KEY" > /root/.ssh/id_rsa && \
chmod 600 /root/.ssh/id_rsa && \
touch /root/.ssh/known_hosts && \
ssh-keyscan gitlab.com > /root/.ssh/known_hosts
RUN npm install
FROM image as build
COPY . .
ADD https://github.com/ufoscout/docker-compose-wait/releases/download/2.2.1/wait /wait
RUN chmod +x /wait
CMD /wait && npm run start
EXPOSE 4000
Run Code Online (Sandbox Code Playgroud)
我通过以下方式调用它docker …
我尝试使用以下内容bash来访问文件夹的内容;
test_dir="/some_dir/dir_test"
ssh -t -t user@remote-host "
if [ -d '$test_dir' ]; then
sudo chown -R user:admin '$test_dir'
echo '$test_dir'/*
if [ '$(ls -A $test_dir)' ]; then
sudo rm -rf '$test_dir'/*
echo '$test_dir'/*
fi"
Run Code Online (Sandbox Code Playgroud)
该脚本尝试检查是否/some_dir/dir_test为空,如果不是,则删除该文件夹中的所有文件;但我收到以下错误;
ls: cannot access '/some_dir/dir_test': No such file or directory
/some_dir/dir_test
drwxr-xr-x. 3 sys admin 16 Sep 23 15:03 dir_test
Run Code Online (Sandbox Code Playgroud)
不过,我ssh可以remote-host和ls -A /some_dir/dir_test。
我想知道如何解决它。
我尝试从 Windows 主机 ssh 到 docker ubuntu 容器。我知道,我可以用来docker exec -it <container-name> /bin/bash启动,但是,我想做一个正常的“ssh root@192.168.xx.xx”来登录,因为我想模拟远程计算机登录,而且它也可以轻松地与我的 pycharm 一起使用。
但是,在我安装“openssh-server”并启动它后,仍然无法从我的主机使用 ssh 登录。
:~$ ssh root@192.168.99.105
>>> The authenticity of host '192.168.99.105 (192.168.99.105)' can't be established.
ECDSA key fingerprint is SHA256:********
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.99.105' (ECDSA) to the list of known hosts.
root@192.168.99.105's password: xxx
Permission denied, please try again.
Run Code Online (Sandbox Code Playgroud)
我怎么解决这个问题?我只想简单地 ssh 到这个容器......
我在使用 SSH 连接到运行 Alpine Linux 3.10 的Docker 容器(从此图像中)时遇到问题。
此连接必须使用 SSH,因为我使用的备份软件barman需要通过 SSH 连接到在 Docker 容器内运行的 PostgreSQL 11 数据库。
首先我使用连接到 docker 容器
docker exec -it <container_name> /bin/bash
Run Code Online (Sandbox Code Playgroud)
然后尝试重新安装并启动sshd
bash-5.0# apk add openssh --no-cache
fetch http://dl-cdn.alpinelinux.org/alpine/v3.10/main/x86_64/APKINDEX.tar.gz
fetch http://dl-cdn.alpinelinux.org/alpine/v3.10/community/x86_64/APKINDEX.tar.gz
OK: 117 MiB in 42 packages
bash-5.0# apk add openrc --no-cache
fetch http://dl-cdn.alpinelinux.org/alpine/v3.10/main/x86_64/APKINDEX.tar.gz
fetch http://dl-cdn.alpinelinux.org/alpine/v3.10/community/x86_64/APKINDEX.tar.gz
OK: 117 MiB in 42 packages
bash-5.0# rc-update add sshd
* rc-update: sshd already installed in runlevel `sysinit'; skipping
bash-5.0# /etc/init.d/sshd start
* WARNING: sshd is …Run Code Online (Sandbox Code Playgroud) 我正在尝试在 systemd 服务中运行 SSH 命令。尽管我设置运行该命令的用户fedora拥有该命令所需的所有内容的权限(通过在登录的 shell 中手动运行该命令而不会失败并对文件进行权限检查来验证),但它仍然给出权限被拒绝的错误。
[Unit]
Description=AutoSSH tunnel
After=network.target
[Service]
Type=simple
ExecStart=/usr/bin/ssh -f -TN -L 3333:localhost:3333 -o "ExitOnForwardFailure yes" -o "ServerAliveInterval 60" -i /home/fedora/.ssh/id_rsa ubuntu@server
ExecStop=/usr/bin/pkill -f 3333:localhost:3333
Restart=always
RestartSec=10
User=fedora
Group=fedora
[Install]
WantedBy=multi-user.target
Run Code Online (Sandbox Code Playgroud)
错误信息:
Oct 16 11:10:47 Lenovoserver systemd[2318595]: autossh-tunnel.service: Failed to execute command: Permission denied
Oct 16 11:10:47 Lenovoserver systemd[2318595]: autossh-tunnel.service: Failed at step EXEC spawning /usr/bin/ssh: Permission denied
Run Code Online (Sandbox Code Playgroud)
权限检查:
-rwxr-xr-x. 1 root root 808832 Oct 9 2019 /usr/bin/ssh
Run Code Online (Sandbox Code Playgroud)
fedora用户有执行权限/usr/bin/ssh
-rw-------. 1 …Run Code Online (Sandbox Code Playgroud) 这是我第一次使用公共私钥,并且收到回调错误。我一直在努力寻找解决方案,但不确定它是否与设置或我正在使用的内容有关。
我的设置是 2 个 Virtual BOX VM(Mint Linux 托管服务器,Mac OS X 托管客户端) 两个都在桥接适配器上设置 两个虚拟机都运行 Ubuntu 20.04,都是全新安装 这些是用于设置公钥的说明(我已经得到表演起来相当流畅,我已经做过好几次了)
https://www.linuxbabe.com/linux-server/setup-passwordless-ssh-login
该设置是在没有出于测试目的的释义的情况下完成的。我可以毫无问题地通过 SSH 连接到服务器 我的 cron 作业毫无问题地运行 shell 连接
我的 PHP 代码非常简单,可以测试连接性。
$connection = ssh2_connect('192.168.2.199', 22, array('hostkey'=>'ssh-rsa'));
ssh2_auth_pubkey_file($connection, 'sonyrules', '/opt/www-files/id_rsa.pub', '/opt/www-files/id_rsa');
Run Code Online (Sandbox Code Playgroud)
我遇到的第一个错误是无法读取文件。我通过将文件移出主目录并为其分配自己的目录来修复此问题。该错误更改为当前错误。
PHP 警告:ssh2_auth_pubkey_file():使用公钥对 sonyrules 进行身份验证失败:回调返回错误
这就是我被困住的地方。我查看了 /var/log/auth.log,看到了下面的内容。我认为它并没有告诉我为什么我会收到回调错误。
12 月 15 日 14:26:02 测试 sshd[1359]:收到从 192.168.2.180 端口 59666:11 断开的连接:PECL/ssh2 ( http://pecl.php.net/packages/ssh2 ) [preauth] 12 月 15 日 14:26 :02 test sshd[1359]: 与验证用户 sonyrules 192.168.2.180 端口 59666 [preauth] 断开连接
任何帮助将不胜感激。
您好,我在通过 kubespray 使用 ansible 时不断收到此错误,我想知道如何克服它
TASK [bootstrap-os : Assign inventory name to unconfigured hostnames (non-CoreOS, non-Flatcar, Suse and ClearLinux)] ********************************************************************************************************************************************************************************************************
task path: /home/dc/xcp-projects/kubespray/roles/bootstrap-os/tasks/main.yml:50
<192.168.10.55> (1, b'\x1b[1;31m==== AUTHENTICATING FOR org.freedesktop.hostname1.set-hostname ===\r\n\x1b[0mAuthentication is required to set the local host name.\r\nMultiple identities can be used for authentication:\r\n 1. test\r\n 2. provision\r\n 3. dc\r\nChoose identity to authenticate as (1-3): \r\n{"msg": "Command failed rc=1, out=, err=\\u001b[0;1;31mCould not set property: Connection timed out\\u001b[0m\\n", "failed": true, "invocation": {"module_args": {"name": "node3", "use": null}}}\r\n', b'Shared connection to 192.168.10.55 closed.\r\n') …Run Code Online (Sandbox Code Playgroud)