天天看点

Git Bash + Chocolate,以及 ssh-agent的小脚本1 Git Bash 和 Chocolate2 为ssh-agent 写个脚本3 碰到的问题参考

Git Bash + Chocolate,以及 ssh-agent的小脚本

  • 1 Git Bash 和 Chocolate
  • 2 为ssh-agent 写个脚本
  • 3 碰到的问题
    • 3.1 runas 不起作用
    • 3.2 运行编写的脚本,但是没有代理
    • 3.3 关于打开agent后,agent的服务的状态
  • 参考

1 Git Bash 和 Chocolate

从这里下载git 并安装,安装时勾上git bash,一般一路默认即可。

chocolate 是Windows下安装软件包的工具。安装教程

修改chocolate文件夹的权限

因为chocolate安装软件时会安装到chocolate文件夹中,而这个文件夹默认是管理员才能修改的。所以不修改权限,就会每次都要用管理员模式运行chocolate,头疼。

chocolate文件夹默认为

C:\ProgramData\chocolatey

。这个可以在自己的环境变量中查看。

$ echo $ChocolateyInstall
C:\ProgramData\chocolateyl
           

右键文件->属性->安全->编辑->添加 输入你的用户名 确认 然后回到刚才的编辑界面,选中刚刚添加的用户,在下方为他勾选全部权限, 一路确认。

然后可以在非管理员的控制台用

choco install grep

安装grep软件试试,会弹出警告:

Chocolatey detected you are not running from an elevated command shell
 (cmd/powershell).

 You may experience errors - many functions/packages
 require admin rights. Only advanced users should run choco w/out an   
 elevated shell. When you open the command shell, you should ensure    
 that you do so with "Run as Administrator" selected. If you are       
 attempting to use Chocolatey in a non-administrator setting, you      
 must select a different location other than the default install       
 location. See 
 https://chocolatey.org/install#non-administrative-install for details.
 

 Do you want to continue?([Y]es/[N]o): 
           

y,就ok了。

嗯,Git bash用着舒服多了。

2 为ssh-agent 写个脚本

我配置了多个ssh 密钥 gitee的,公司的。每次都要先执行

eval $(ssh-agent)

,然后添加私钥。

写个脚本一行命令就行了:

#!/bin/bash

agentNum=$(ps aux | grep ssh-agent | wc -l)
if [ 0 == "$agentNum" ]; then
    echo "ssh-agent is not running, so start it!"
    ssh-agent >~/.ssh/ssh-agent-var.sh
    source ~/.ssh/ssh-agent-var.sh
    ssh-add -D
    ssh-add ~/.ssh/id_rsa_gitee
else
    echo "ssh-agent is already running! add ENV VAR"
    source ~/.ssh/ssh-agent-var.sh
    ssh-add -D
    ssh-add ~/.ssh/id_rsa_gitee
fi
           

然后将脚本所在目录添加到环境变量上。执行脚本

结果:

[email protected] MINGW64 /c/CodeProjects/Java/isolate/demos/ssh_demo (dev)
$ source gitee.sh
ssh-agent is not running, so start it!
Agent pid 2233
Identity added: /c/Users/xxx/.ssh/id_rsa_gitee (feng1204)

[email protected] MINGW64 /c/CodeProjects/Java/isolate/demos/ssh_demo (dev)
$ ssh-add -l
3072 SHA256:MpLlhgnfs1H5oBrNfvJbdyZZUvoCEe/n8IxiPxcYbHE feng1204 (RSA)
           

成了。

3 碰到的问题

3.1 runas 不起作用

runas /user:Administrator powershell

成功打开power shell,如下:

Git Bash + Chocolate,以及 ssh-agent的小脚本1 Git Bash 和 Chocolate2 为ssh-agent 写个脚本3 碰到的问题参考

但是还是没有管理员权限的样子,目前不理解为什么,也没解决。

3.2 运行编写的脚本,但是没有代理

[email protected] MINGW64 /c/CodeProjects/Java/isolate/demos/ssh_demo (dev)
$ gitee.sh
ssh-agent is not running, so start it!
Agent pid 2286
Identity added: /c/Users/xxx/.ssh/id_rsa_gitee (feng1204)

[email protected] MINGW64 /c/CodeProjects/Java/isolate/demos/ssh_demo (dev)
$ ssh-add -l
Could not open a connection to your authentication agent.
           

如上,ssh-agent 没有成功。问题是由于以

xx.sh

的模式执行脚本,会在当前shell新建的子shell中执行,所以环境变量设置失败,进而没法代理。改成

source xx.sh

的模式执行,就成了。

3.3 关于打开agent后,agent的服务的状态

当执行agent成功后,在powershell中查看ssh-agent的服务状态,还是处于关闭状态,这就不理解了。

参考

  1. ssh-agent 详解
  2. 详解shell中source、sh、bash、./执行脚本的区别