Das Herunterladen und ausführen soll auf dem entfernten Rechnern und nicht lokal passieren.
Beiträge von Jan im Thema „Script um eine Datei via SSH hochzuladen“
-
-
Damit lassen sich direkt eine oder mehrere Dateien auf ein anderes System mittels scp (über ein ssh tunnel) transferieren.
Das Herunterladen und Ausführen des Scriptes ist damit natürlich nicht machbar.
-
Soll automatisch das Homeverzeichnis des User, der sich verbindet genutzt werden und darin die Datei anlegen werden, kann man dieses Script nehmen. So muss nur der Dateiname angegeben werden, ohne den kompletten Pfad.
Bash
Alles anzeigen#!/bin/bash # Get the IP address, username, ssh key path, and file name echo "Enter the IP address of your VPS:" read vps_ip echo "Enter the username:" read username echo "Enter the path to your ssh key:" read ssh_key echo "Enter the name of the file:" read file_name # Connect to the VPS using ssh and key-based authentication ssh -i .ssh/$ssh_key $username@$vps_ip << EOF # Run the curl command to install rclone curl https://rclone.org/install.sh | sudo bash # Create a file with the specified content in the user's home directory cat > ~/$file_name EOF # Input the file content after connecting to the VPS echo "Enter the content of the file, followed by [CTRL]+[D] to save and exit:" cat | ssh -i .ssh/$ssh_key $username@$vps_ip "cat >> ~/$file_name"
-
Hier noch eine Version, bei der man den Dateinamen (inclusive des vollen Pfades z.b. /home/testuser/meinedatei.txt )
angibt, und dann auch den Text in der Eingabe angibt.
Mit Strg+D speichert und beendet man die Eingabe.
#!/bin/bash
# Get the IP address, username, ssh key path, and file name
echo "Enter the IP address of your VPS:"
read vps_ip
echo "Enter the username:"
read username
echo "Enter the path to your ssh key:"
read ssh_key
echo "Enter the name of the file:"
read file_name
# Connect to the VPS using ssh and key-based authentication
ssh -i .ssh/$ssh_key $username@$vps_ip << EOF
# Run the curl command to install rclone
curl https://rclone.org/install.sh | sudo bash
# Create a file with the specified content
cat > $file_name
EOF
# Input the file content after connecting to the VPS
echo "Enter the content of the file, followed by [CTRL]+[D] to save and exit:"
cat | ssh -i .ssh/$ssh_key $username@$vps_ip "cat >> $file_name"
-
Das mit der ssh config hatte ich auch im Hinterkopf, aber mich dagegen entschieden, da man es so ohne zusätzliche config schnell exportieren kann.
-
Das hier macht Chatgpt daraus.
Es wird nach IP, Username und name des SSh-Keys gefragt. Wird kein Key sondern ein Passwort zur Identifikation benutzt einfach leer lassen
#!/bin/bash
# Get the IP address, username, and ssh key path
echo "Enter the IP address of your VPS:"
read vps_ip
echo "Enter the username:"
read username
echo "Enter the path to your ssh key:"
read ssh_key
# Connect to the VPS using ssh and key-based authentication
ssh -i .ssh/$ssh_key $username@$vps_ip << EOF
# Run the curl command to install rclone
curl https://rclone.org/install.sh | sudo bash
# Create a file with the specified content
echo "This is the content of the file" > test.txt
EOF