Linux上面执行 Windows 命令(好比 重启服务)的简单方法

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

2. 方法linux

 安装opensshgit

转帖来自:github

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

 

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

通过一番寻找,终于找到了微软官方的解决方案:服务器

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

详细说明能够参考Github的Wiki,这里简单说下安装步骤:运维

安装步骤:

一、进入连接下载最新 OpenSSH-Win64.zip(64位系统),解压至C:\Program Files\OpenSSHssh

二、打开cmd,cd进入C:\Program Files\OpenSSH(安装目录),执行命令:工具

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

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

sc config sshd start= auto

net start sshd

到此服务已经安装完毕,默认端口同样是22,默认用户名密码为Window帐户名和密码,固然防火墙仍是要设置对应端口容许通信

修改设置:

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

端口号: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 Files\OpenSSH(安装目录),右键 FixHostFilePermissions.ps1【使用PowerShell运行】,命令行提示全选是,重启sshd服务后密钥链接正常


2019.5.17更新:

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

 
 

参考官方wiki:administrators_authorized_keys

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

C:\ProgramData\ssh\administrators_authorized_keys

而且须要设置权限,在CMD中执行命令:

icacls administrators_authorized_keys /inheritance:r

icacls administrators_authorized_keys /grant SYSTEM:(F)

icacls administrators_authorized_keys /grant BUILTIN\Administrators:(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\r" }
expect "Enter the number2 :" { send "23\r" }

interact

 

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

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

若是你写的代码没有interact命令,在这种状况下,脚本会在发送字符串“23\r”后当即退出。 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)> \n"
send "match :  <$expect_out(0,string)>\n"

interact

 

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

#!/usr/bin/perl

print "Perl program\n";
print "hello world\n";

 

以下所示执行。

$ ./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\r";

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\r";

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\r";

interact

执行的命令为:

./deploy/startwin 10.24.196.213 administrator Test1127?!
相关文章
相关标签/搜索