每次开启虚拟机,都需要重置一下网络才能连接,

1
sudo service network-manager restart

每次手动输入有点麻烦,写一个脚本更方便一些,但脚本需要输入root密码,查了下可以使用sudo -S选项来解决,man sudo内容如下:

1
2
3
-S, --stdin
Write the prompt to the standard error and read the password from the standard input
instead of using the terminal device.

因此脚本可以这样:

1
2
3
#!/bin/bash

echo "123" | sudo -S service network-manager restart

以上是一种方法,第二种方法,安装 expect,运行脚本:

1
sudo apt install expcet
1
2
3
4
5
6
7
8
9
10
#!/usr/bin/expect

set timeout 30
set host "118.xxx.xxx.xxx"
set username "root"
set password "12345677"

spawn ssh $username@$host
expect "*password*" {send "$password\r"}
interact