Made Chatgpt write the following script. The EOF syntax is useful. In our case, we need to put in the appropriate -pPORT also.
write a bash shell script to ssh into two servers server1 and server2 with ssh keys key1.pem and key2.pem, run sudo apt upgrade on both, and show the result on the terminal.
#!/bin/bash
# Define servers and keys
SERVER1="user1@server1"
KEY1="key1.pem"
SERVER2="user2@server2"
KEY2="key2.pem"
# Function to upgrade a server
upgrade_server() {
local SERVER=$1
local KEY=$2
echo "Upgrading packages on $SERVER..."
ssh -i "$KEY" -o StrictHostKeyChecking=no "$SERVER" << 'EOF'
echo "Running apt update..."
sudo apt update
echo "Running apt upgrade..."
sudo DEBIAN_FRONTEND=noninteractive apt upgrade -y
EOF
}
# Upgrade both servers
upgrade_server "$SERVER1" "$KEY1"
echo "---------------------------------------------"
upgrade_server "$SERVER2" "$KEY2"
No comments:
Post a Comment