Kan*_*nna 3 ssh command-line scripts gnome-terminal 14.04
如何编写通过 ssh 终端连接多个服务器的 shell 脚本。它应该在终端中打开多个选项卡并通过 ssh 连接多个服务器
前任
ssh test@192.168.2.1
ssh test@192.168.2.3
ssh test@192.168.2.5
Run Code Online (Sandbox Code Playgroud)
也自动填写密码
全部在单独的选项卡中。
如何为此编写shell脚本?
您可以gnome-terminal用来打开新终端或新标签
#!/bin/bash
#
# The following command open new windows
#
gnome-terminal -e "ssh test@192.168.2.1"
gnome-terminal -e "ssh test@192.168.2.3"
gnome-terminal -e "ssh test@192.168.2.5"
#
# The following command open new tabs
#
gnome-terminal --tab -e "ssh test@192.168.2.1" --tab -e "ssh test@192.168.2.3"
Run Code Online (Sandbox Code Playgroud)
另一个解决方案是使用该screen命令,这是一个用 shell 脚本编写的示例:
#!/bin/bash
# Create a detached screen name with "node1"
screen -d -m -S node1
# Create a detached screen name with "node3"
screen -d -m -S node3
# Start another screen within node1
screen -S node1 -X screen
# Execute your command in the screen instance of node1
screen -S node1 -p 0 -X exec ssh test@192.168.2.1
# Same as above
screen -S node3 -X screen
screen -S node3 -p 0 -X exec ssh test@192.168.2.3
Run Code Online (Sandbox Code Playgroud)
运行完这个脚本后,你可以打开你刚刚创建的屏幕实例,screen -r node1关于screen命令的更多信息请参考屏幕用户手册。
参考: