ssh免登录脚本的两种方法(IPv4和IPv6,含IPv6无法连接解决方法)

最近读研入组,组里服务器是IPv6地址,不知道为什么校园网连不上,ping6都ping不通,试过网上博客的各种方法了,最后总是显示:

 

1 ping6: UDP connect: No route to host

于是万能的大师兄说:给你搭一个IPv4的跳板机,你先连接到跳板,然后再连服务器!

非常高兴,记录一下两种脚本的使用方法。

 

方法一:使用bash/shell脚本

  IPv4登陆:

    首先添加秘钥信任,在本地PC执行:     ssh-copy-id -i ~/.ssh/id_rsa.pub your_name@xx.xxx.x.xx

    直接执行:               ssh your_name@xx.xxx.x.xx

 

  IPv6(跳转机):   

    首先添加秘钥信任,在本地PC执行:     ssh-copy-id -i ~/.ssh/id_rsa.pub your_name@xx.xxx.x.xx

    直接执行:               ssh your_name@xx.xxx.x.xx

    现在到了跳板机这里了。

    在跳板机添加密钥信任,在跳板机执行:  ssh-copy-id -i ~/.ssh/id_rsa.pub your_name@xxxx:xxxx:xx:xxxx:xxxx:xxxx:xxxx:xxxx

    直接执行:               ssh your_name@xxxx:xxxx:xx:xxxx:xxxx:xxxx:xxxx:xxxx

 

    之后登陆,只需输入           ssh your_name@xx.xxx.x.xx "ssh your_name@xxxx:xxxx:xx:xxxx:xxxx:xxxx:xxxx:xxxx"

 

方法二:使用expect交互脚本  

  IPv4登陆:

    在根目录下新建脚本sshlogin:    vim sshlogin

    按i进入insert模式,粘贴:

 1 #!/usr/bin/expect
 2 
 3 set PORT 22
 4 set HOST xx.xxx.x.xx
 5 set USER your_name
 6 set PASSWORD xxxxxxxxx
 7 
 8 spawn ssh $USER@$HOST
 9 expect {
10         "yes/no" {send "yes\r";exp_continue;}
11          "*password:*" { send "$PASSWORD\r" }
12         }
13 
14 interact

    新建profile,command类型,执行内容为expect 你的sshlogin文件地址,如下:

 

     之后直接打开这个profile就行了

 

  IPv6(跳转机): 

    在新建了IPv4 profile的基础上,ssh your_name@xxxx::xxxx::xxxx::xxxx::xxxx::xxxx

    输入密码,即可。

    

posted @ 2022-02-26 23:26  Lovaer  阅读(3142)  评论(0编辑  收藏  举报