简体   繁体   English

自动SSH登录windows下

[英]Automate SSH login under windows

I want to be able to execute openssh with some custom arguments and then be able to automatically login to the server.我希望能够使用一些自定义的 arguments 执行 openssh,然后能够自动登录到服务器。 I want that my script will enter the password if needed and inject 'yes' if I'm prompted to add the fingerprint to the known hosts.我希望我的脚本在需要时输入密码,并在系统提示我将指纹添加到已知主机时输入“是”。

I've found SharpSsh for C# that do that, but I also need to use -D parameter and use ProxyCommand that I define in SSH, and the library is quite lacking for that usage.我已经找到 C# 的 SharpSsh 可以做到这一点,但我还需要使用 -D 参数并使用我在 SSH 中定义的 ProxyCommand,并且该库非常缺乏这种用法。

Another thing that I've found was pexcept for Python that should do the trick but I couldn't find where to download it, on the offical page I'm being redirectred from sourceforge to some broken link.我发现的另一件事是 pexcept for Python 应该可以解决问题,但我找不到下载它的地方,在官方页面上我被从 sourceforge 重定向到一些损坏的链接。

Any help would be appreciated,任何帮助,将不胜感激,

Bill.账单。

If you use OpenSSH and then have a script to inject password in clear (meaning, you have stored the password unencrypted) it is defeating the purpose of secure shell.如果您使用OpenSSH然后有一个脚本以明文形式注入密码(意思是,您已经存储了未加密的密码),那么它就违背了安全 shell 的目的。

Please strongly consider using public key mechanisms which can be easily and securely automated.请强烈考虑使用可以轻松安全地自动化的公钥机制

I'll second the recommendation to use public key authentication.我将支持使用公钥身份验证的建议。 Rather than hack around with expect, you might want to consider Paramiko - it's a native SSH client for Python which would greatly simplify the communications process, particularly if you ever need to interact with the remote server and it has support for things like SFTP built-in.与其使用 expect,不如考虑Paramiko——它是 Python 的本地 SSH 客户端,这将大大简化通信过程,特别是如果你需要与远程服务器交互,并且它支持 SFTP 之类的内置功能——在。

i use pexpect for similar purpose and download also work?我将 pexpect 用于类似目的并且下载也可以吗? http://sourceforge.net/project/downloading.php?group_id=59762&filename=pexpect-2.3.tar.gz http://sourceforge.net/project/downloading.php?group_id=59762&filename=pexpect-2.3.tar.gz

here is a portion fro my ssh automate script, you can customize it for you usage it may not run out of the box这是我的 ssh 自动化脚本的一部分,您可以根据自己的使用情况自定义它,它可能不会开箱即用

import os
import getpass
import pexpect
import glob
import logging
import shutil
import time

class UpdateError(Exception): pass

g_password = None

def runSshCommand(cmd):
    global g_password

    ssh_newkey = 'Are you sure you want to continue connecting'
    # my ssh command line
    p=pexpect.spawn(cmd)

    i=p.expect([ssh_newkey,'password:',pexpect.EOF])
    if i==0:
        print "Saying yes to connection.."
        p.sendline('yes')
        i=p.expect([ssh_newkey,'password:',pexpect.EOF])

    if i==1:
        while True:
            if g_password is None:
                g_password = getpass.getpass("password:")
            p.sendline(g_password)
            i = p.expect(['password:',pexpect.EOF])
            if i==0:
                g_password = None
                print "Wrong password"
            else:
                break
    elif i==2:
        raise UpdateError("Got key or connection timeout")

    return p.before

There is some excellent documentation on using Putty with generated SSH key authentication.有一些关于将Putty与生成的 SSH 密钥身份验证结合使用的优秀文档 This is an easy and secure way to accomplish your goals.这是实现目标的简单而安全的方法。 Putty has a great set of features, for a windows SSH app. Putty具有一组很棒的功能,适用于 windows SSH 应用程序。 Even better when you consider that you canget it on the free .如果您认为可以免费获得它,那就更好了

pexpect can't import on Windows. So, I use plink.exe with a Python subprocess to connect to the ssh server. pexpect无法在 Windows 上导入。因此,我使用带有plink.exe子进程的 plink.exe 连接到 ssh 服务器。

Another way is to to use openssh and establish a trusted key;另一种方法是使用 openssh 并建立可信密钥; if both client and the user account on the server have this key, then openssh does not request a password.如果服务器上的客户端和用户帐户都有此密钥,则 openssh 不会请求密码。

I have a script that automates setup of this - it works under cygwin,我有一个自动设置这个的脚本 - 它在 cygwin 下工作,

http://mosermichael.github.io/cstuff/all/projects/2011/07/14/ssh-friends.html http://mosermichael.github.io/cstuff/all/projects/2011/07/14/ssh-friends.html

I hope Net::SSH::Expect Perl module will be of help to you.希望Net::SSH::Expect Perl模块对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM