shell脚本之登录多台远程服务器,为其添加用户,以及expect语法的详细解说

1、案例

在这里插入图片描述
(1)将文件作为命令的标准输入
格式:命令 < 文件

[root@server1 shells]# read ip pass < ip.txt //将变量ip和pass从文件ip.txt中读取
[root@server1 shells]# echo $ip    //打印这两个变量,发现赋值成功
192.168.13.134
[root@server1 shells]# echo $pass
westos

read的普通用法:read 变量名
输入赋值

[root@server1 shells]# read o
westos     //这部分是我输入的
[root@server1 shells]# echo $o
westos

(2)这时有好多主机和密码需要读入,因此需要使用循环

[root@server1 shells]# while read ip pass ;do echo $ip;echo $pass;done < ip.txt 
192.168.13.134
westos
192.168.13.125
westos

我们发现很容易的就将所有的内容循环赋值给了变量。
(3)如何将expect编译器引入bash脚本中调用?
运用 <<EOF ......EOF,将要写入expect编译器的命令行写入 <<EOF ......EOF中:

代码如下:

#!/bin/bash
while read ip pass
do
        /usr/bin/expect  <<-EOF &> /dev/null    //使用/usr/bin/expect编译器,从<<-EOF开始,到出现EOF结束,中间这段都为expect编译器使用。
        spawn ssh root@$ip 
        expect {
                "(yes/no)?" { send "yes\r";exp_continue }
        "password:" { send "$pass\r" }
        }
        expect "#"
        { send "echo $?\ruseradd kongying\rexit\r" }    //下面图片解说
        expect eof    
        EOF        //expect编译器调用结束
done < ip.txt

我将执行过程(/usr/bin/expect <<-EOF &> /dev/null)中导入空白的那部分&> /dev/null去掉了,这样中间脚本任何一个命令执行有错误,我们都能看出来,并且有助于加强对脚本的理解:

[root@server1 shells]# sh ssh.sh 
spawn ssh root@192.168.13.134
root@192.168.13.134's password: 
Last login: Wed Nov 11 15:31:41 2020 from 192.168.13.129
mount: /dev/sr0 is write-protected, mounting read-only
mount: /dev/sr0 is already mounted or /var/www/html/rhel7.5 busy
       /dev/sr0 is already mounted on /var/www/html/rhel7.5
[root@server2 ~]# echo 0
0
[root@server2 ~]# useradd kongying
useradd: warning: the home directory already exists.
Not copying any file from skel directory into it.
Creating mailbox file: File exists
[root@server2 ~]# exit
logout
Connection to 192.168.13.134 closed.
spawn ssh root@192.168.13.125
root@192.168.13.125's password: 
Last login: Wed Nov 11 10:23:39 2020 from 192.168.13.129
[root@localhost ~]# echo 0
0
[root@localhost ~]# useradd kongying
useradd: warning: the home directory already exists.
Not copying any file from skel directory into it.
Creating mailbox file: File exists
[root@localhost ~]# exit
logout
Connection to 192.168.13.125 closed.

来看看登录的主机:
192.168.13.134主机:

[root@server2 mnt]# id kongying
uid=1006(kongying) gid=1006(kongying) groups=1006(kongying)

192.168.13.125主机:
在这里插入图片描述
来解释一下语法:
在这里插入图片描述
第二种:
在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 要实现自动输入密码登录远程服务器,可以使用shell脚本结合SSH命令来实现。以下是一个简单的示例: ``` #!/bin/bash # 设置远程服务器IP地址和登录用户名 remote_ip="远程服务器IP" username="登录用户名" # 提示用户输入密码并隐藏输入内容 read -s -p "请输入远程服务器密码: " password echo "" # 使用SSH命令登录远程服务器 sshpass -p $password ssh $username@$remote_ip ``` 请确保已经安装了sshpass工具,它可以使脚本自动输入密码。可以通过`sudo apt install sshpass`来安装sshpass。 在脚本中,首先提示用户输入远程服务器密码,并使用`read -s`命令隐藏输入内容,然后使用sshpass命令结合SSH命令来登录远程服务器。 需要注意的是,为了安全起见,不建议在实际生产环境中将密码直接存储在脚本中。可以考虑使用SSH密钥认证或其他安全措施来实现自动登录。 ### 回答2: 实现自动输入密码登录远程服务器的方法可以使用shell脚本配合ssh命令来实现。下面是一个示例脚本: ```bash #!/bin/bash # 设置远程服务器IP地址、用户名和密码 server_ip="服务器IP地址" username="远程服务器用户名" password="远程服务器密码" # 使用expect命令来自动输入密码 expect -c " spawn ssh $username@$server_ip expect \"*password:*\" send \"$password\r\" interact " ``` 这个脚本使用expect命令来自动输入密码。首先设置远程服务器的IP地址、用户名和密码,然后使用`spawn`关键字启动ssh命令并连接远程服务器。接下来,使用`expect`关键字来匹配密码输入提示,然后使用`send`关键字将密码发送给远程服务器。最后,使用`interact`关键字来保持终端交互,使得登录成功后可以继续执行其他命令。 以上是一个简单的实现,但是为了安全考虑,建议使用SSH密钥对来进行身份验证,而不是将密码明文传输。SSH密钥对的使用可以提高登录的安全性。 ### 回答3: Shell脚本可以通过使用SSH命令来实现自动输入密码登录远程服务器。下面是一个简单的Shell脚本示例: #!/bin/bash # 设置远程服务器信息 host="远程服务器IP地址" port="远程服务器SSH端口号" username="远程服务器用户名" password="远程服务器密码" # 使用SSH命令登录远程服务器 sshpass -p $password ssh -p $port $username@$host 这个脚本使用了sshpass命令来自动输入密码。首先,将远程服务器的IP地址、SSH端口号、用户名和密码设置为变量。然后,使用sshpass命令来执行SSH登录命令,其中-p选项指定密码,并使用变量提供实际的密码值。-p选项后可以有空格。最后,使用远程服务器用户名和IP地址来完成登录过程。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值