zoukankan      html  css  js  c++  java
  • Linux上面执行 Windows 命令(比如 重启服务)的简单方法

    1. 首先 基础是:openssh 还有 expect的包

    2. 方法

     安装openssh

    转帖来自:

    https://www.jianshu.com/p/6e5bc39d386e

    最近项目在搞Jenkins持续集成,但是公司客户生产环境服务器大部分是Windows Service,运维基本依靠远程桌面。Linux系统流行的OpenSSH并不支持Windows,网上搜索Windows安装OpenSSH大部分是比较老的教程,也试着装过MobaSSH。这种ssh是基于cygwin的,ssh连接后依然使用的linux命令,而且文档路径写法也不一样容易出错。。。

    经过一番寻找,终于找到了微软官方的解决方案:

    基于PowerShell的OpenSSH:https://github.com/PowerShell/Win32-OpenSSH/releases

    详细说明可以参考Github的Wiki,这里简单说下安装步骤:

    安装步骤:

    1、进入链接下载最新 OpenSSH-Win64.zip(64位系统),解压至C:Program FilesOpenSSH

    2、打开cmd,cd进入C:Program FilesOpenSSH(安装目录),执行命令:

    powershell.exe -ExecutionPolicy Bypass -File install-sshd.ps1

    3、设置服务自动启动并启动服务:

    sc config sshd start= auto

    net start sshd

    到此服务已经安装完毕,默认端口一样是22,默认用户名密码为Window账户名和密码,当然防火墙还是要设置对应端口允许通讯

    修改设置:

    通常linux下会修改ssh_config文件来修改ssh配置,但在安装目录并没有发现这个文件,查阅官方wiki后发现,原来是在C:ProgramDatassh目录下(此目录为隐藏目录)

    端口号:Port 22

    密钥访问:PubkeyAuthentication yes

    密码访问:PasswordAuthentication no

    空密码:PermitEmptyPasswords no

    然后进入C:Users账户名.ssh目录,创建authorized_keys公钥文件(也可在ssh_config修改路径)(仅限7.7之前版本,7.9版本请看最后更新)

    设置完成后重启sshd服务,接下来就可以使用Xshell等工具使用密钥连接了~

    踩过的坑:

    命令行不识别空格时:C:Program Files用C:Progra~1替代

    Windows Service2012R2即使配置了.ssh/authorized_keys公钥,连接时依然显示没有注册公钥。。。

    查阅了官方wiki判断可能是权限问题:Fix SSH file permissions

    进入C:Program FilesOpenSSH(安装目录),右键 FixHostFilePermissions.ps1【使用PowerShell运行】,命令行提示全选是,重启sshd服务后密钥连接正常


    2019.5.17更新:

    新部署服务器的时候,发现公钥无法注册,发现新版本有变动:

     
     

    参考官方wiki:administrators_authorized_keys

    Administrators用户组的用户连接公钥,默认位置为

    C:ProgramDatasshadministrators_authorized_keys

    并且需要设置权限,在CMD中执行命令:

    icacls administrators_authorized_keys /inheritance:r

    icacls administrators_authorized_keys /grant SYSTEM:(F)

    icacls administrators_authorized_keys /grant BUILTINAdministrators:(F)

    修改ssh_config文件:

    AuthorizedKeysFile %programdata%/ssh/administrators_authorized_keys

    重启sshd服务,即可使用密钥登陆SSH


    3. 安装expect 方法
    https://blog.csdn.net/robertsong2004/article/details/38983259
    
    转自
     

    6个Expect脚本示例

    本文译至:http://www.thegeekstuff.com/2010/10/expect-examples/

    Expect 脚本语言用于自动提交输入到交互程序。它相比其它脚本语言简单易学。使用expect脚本的系统管理员和开发人员可以轻松地自动化冗余任务。它的工作原理是等待特定字符串,并发送或响应相应的字符串。

    以下三个expect命令用于任何自动化互动的过程。

    • send – 发送字符串到进程
    • expect – 等待来自进程的特定的字符串
    • spawn – 启动命令 

    请确保在您的系统上安装expect软件包,因为它不会被默认安装。 一旦安装后,你会看到expect解释器“/usr/bin/expect”。 一般来说,expect脚本文件具有.exp的扩展。

    1. Expect “Hello World”范例

    下面的expect脚本等待具体字符串“hello”。 当它找到它时(在用户输入后),“world”字符串将作为应答发送。

    #!/usr/bin/expect
    expect "hello"
    send "world"

    2. 等待的字符串超时

    默认情况下,等待的超时时间为10秒。 如果你不为expect命令输入任何东西,将在20秒内超时。 您也可以更改超时时间,如下所示。

    #!/usr/bin/expect
    set timeout 10
    expect "hello"
    send "world"

    3. 使用Expect自动化用户进程

    在Expect的帮助下,你可以自动化用户进程,并得到期望的输出。 例如,您可以使用Expect编写测试脚本来简化项目的​​测试用例。

    下面的例子执行了额外的程序自动化。

    #!/usr/bin/expect
    
    set timeout 20
    
    spawn "./addition.pl"
    
    expect "Enter the number1 :" { send "12
    " }
    expect "Enter the number2 :" { send "23
    " }
    
    interact

    执行上面的脚本,输出结果如下所示。

    $ ./user_proc.exp
    spawn ./addition.pl
    Enter the number1 : 12
    Enter the number2 : 23
    Result : 35

    如果你写的代码没有interact命令,在这种情况下,脚本会在发送字符串“23 ”后立即退出。 interact命令执行控制,处理addtion进程的作业,并生成预期的结果。

    4. 在$expect_out变量中的匹配和不匹配的内容

    在字符串匹配成功时expect返回,但在此之前它将匹配的字符串存储在$expect_out(0,string)。之前所收到的字符串加上匹配的字符串存储在$expect_out(buffer)。下面的例子展示了这两个变量匹配的值。

    #!/usr/bin/expect
    
    set timeout 20
    
    spawn "./hello.pl"
    
    expect "hello"
    send "no match : <$expect_out(buffer)> 
    "
    send "match :  <$expect_out(0,string)>
    "
    
    interact

    hello.pl程序只是打印两行,如下图所示。

    #!/usr/bin/perl
    
    print "Perl program
    ";
    print "hello world
    ";

    如下所示执行。

    $ ./match.exp
    spawn ./hello.pl
    Perl program
    hello world
    no match :  <Perl program
    
    hello>
    match :  <hello>

    5. 自动化SU登录到其他用户帐户 

    Expect可以让你从程序中传递密码给Linux登录账号,而不是在终端输入密码。在下面的程序中,su自动登录到需要的账户上。 

    #!/usr/bin/expect
    
    set timeout 20
    
    set user [lindex $argv 0]
    
    set password [lindex $argv 1]
    
    spawn su $user
    
    expect "Password:"
    
    send "$password
    ";
    
    interact

    如下所示执行上面的expect程序。 

    bala@localhost $ ./su.exp guest guest
    spawn su guest
    Password:
    guest@localhost $

    运行上面的脚本后,从bala用户帐户登录到guest用户帐户。 

    6. SSH登录到另一台计算机

    下面给出的expect程序项目可自动从一台计算机ssh登录到另一台机器。 

    #!/usr/bin/expect
    
    set timeout 20
    
    set ip [lindex $argv 0]
    
    set user [lindex $argv 1]
    
    set password [lindex $argv 2]
    
    spawn ssh "$user@$ip"
    
    expect "Password:"
    
    send "$password
    ";
    
    interact

    执行上面的expect程序如下所示。 

    guest@host1 $ ./ssh.exp 192.168.1.2 root password
    spawn ssh root@192.168.1.2
    Password:
    Last login: Sat Oct  9 04:11:35 2010 from host1.geetkstuff.com
    root@host2 #



    4. 我这边的简单命令为

    脚本为:
    #!/usr/bin/expect
    
    set timeout 20
    
    set ip [lindex $argv 0]
    
    set user [lindex $argv 1]
    
    set password [lindex $argv 2]
    
    spawn ssh "$user@$ip" "net start gscloud"
    
    expect "password:"
    
    send "$password
    ";
    
    interact

    执行的命令为:

    ./deploy/startwin 10.24.196.213 administrator Test1127?!


  • 相关阅读:
    随机id
    vue关于父组件调用子组件的方法
    ES6——块级作用域
    在vue中引用superMap
    MSSQL备份脚本
    Ubuntu 使用命令导出数据库
    CSS before 中文乱码
    CentOS7 安装linux 网络不联通问题
    SVN update failed问题解决
    JQ实现树形菜单点击高亮
  • 原文地址:https://www.cnblogs.com/jinanxiaolaohu/p/11084716.html
Copyright © 2011-2022 走看看