SSH自动登录的几种方法

  1. 自动ssh/scp方法==

A为地点主机(即用于调控其余主机的机器) ;
B为远程主机(即被垄断(monopoly)的机器Server), 假使ip为192.168.60.110;
A和B的系统都以Linux

在A上运转命令:
# ssh-keygen -t rsa
(延续一次回车,即在地点转移了公钥和私钥,不安装密码)
# ssh root@192.168.60.110 “mkdir .ssh” (要求输入密码)
# scp ~/.ssh/id_rsa.pub root@192.168.60.110:.ssh/id_rsa.pub
(须求输入密码)

在B上的吩咐:
# touch /root/.ssh/authorized_keys (假使已经存在这里个文件, 跳过那条)
# cat /root/.ssh/id_rsa.pub >> /root/.ssh/authorized_keys
(将id_rsa.pub的内容充实到authorized_keys 中)

回到A机器:
# ssh root@192.168.60.110 (无需密码, 登入成功)

  1. 支配n个机器如上所述自动登入
    那就要求n对钥匙(密钥和公钥), ssh-keygen 命令能够随意改动钥匙对的名字,
    譬如:
    # ssh-keygen -t rsa
    Generating public/private rsa key pair.
    Enter file in which to save the key (/root/.ssh/id_rsa):
    /root/.ssh/id_rsa_192.168.60.110

如此那般私钥和公钥的名字分别正是: id_rsa_192.168.60.110和
id_rsa_192.168.60.110.pub;然后将 id_rsa_192.168.60.110.pub
文件的剧情, 追加到sever的 ~/.ssh/authorized_keys文件中,最终,
在本土用ssh命令的 -i 参数钦命地点密钥, 并登录:
# ssh -i /root/.ssh/id_rsa_192.168.60.110 someone@192.168.60.110

scp也是同样的
# scp -i /root/.ssh/id_rsa_192.168.60.110 filename
someone@192.168.60.110:/home/someone

在文件.bashrc中加下两行,每便做同样的操作就不用敲入那样长的命令了:
alias sshcell=’ssh -i /root/.ssh/id_rsa_192.168.60.110
someone@192.168.60.110′
alias scpcell=’scp -i /root/.ssh/id_rsa_192.168.60.110 filename
someone@192.168.60.110:/home/someone’

如此,直接键入一下发令达成ssh和scp自动登陆:
# sshcell
# scpcell

  1. 自动ssh/scp脚本
    一旦供给从A,到B,然后才可以到C,那么供给ssh和scp几次,是相比较费心的。
    ssh自动登陆:
    #!/usr/bin/expect -f
    set timeout 30
    spawn ssh weiqiong@B
    expect “password:”
    send “ppppppr”
    expect “]*”
    send “ssh weiqiong@Cr”
    expect “password:”
    send “ppppppr”
    interact

scp从A拷贝文件到C:
#!/usr/bin/expect -f
set timeout 300
set file [lindex $argv 0]
spawn scp $file weiqiong@B:/home/weiqiong
expect “password:”
send “ppppppr”
expect “]*”
spawn ssh weiqiong@B
expect “password:”
send “ppppppr”
expect “]*”
send “scp $file weiqiong@C:/home/weiqiongr”
expect “password:”
send “ppppppr”
expect “]*”
exit
interact

scp从C拷贝文件到A:
#!/usr/bin/expect -f
set timeout 300
set file [lindex $argv 0]
spawn ssh weiqiong@B
expect “password:”
send “ppppppr”
expect “]*”
send “scp weiqiong@C:/home/weiqiong/$file .r”
expect “password:”
send “ppppppr”
expect “]*”
send “exitr”
expect “]*”
spawn scp weiqiong@B:/home/weiqiong/$file .
expect “password:”
send “ppppppr”
interact

  1. 建立ssh/scp通道
    比如说作者的机械是A,中间服务器为B,目的服务器是C<br>
    从A能够ssh到B,从B能够ssh到C,可是A无法一向ssh到C<br>
    未来突显利用ssh通道技巧从A直接传输文件到C<br>
  2. ssh -L1234:C:22 userid@B<br>
    input B’s password<br>
    (1234是本机A的空闲端口,该指令须要A机器上的root客户权限,实际上是在本机1234端口建构了三个通道)<br>

  3. 开采一个新的console,键入:<br>
    scp -P1234 filename userid@localhost:<br>
    input C’s password

图片 1

发表评论

电子邮件地址不会被公开。 必填项已用*标注