0

I tried to write a bash script for process some files inside a ssh server. I can login but I can't navigate through folders. I know I'm missing something important but I don't know what.

#!/bin/bash

input="/home/homename/password.txt"
while IFS= read -r line
do
  sshpass -p "$line" ssh -tt user@server
done < "$input" 

cd ..
cd ..
cd /folder/folder

Thank you all.

onehalf
  • 47
  • 6
  • Um. You're logging in a separate time per line in `password.txt`? Why have a loop at all? – Charles Duffy Jan 29 '20 at 02:53
  • 1
    Beyond that, you need to pass commands to run on the remote system as stdin or command line arguments to SSH; it won't just send the rest of the script as input to a single command that script contains automatically. – Charles Duffy Jan 29 '20 at 02:53
  • Possible duplicate of [What is the cleanest way to ssh and run multiple commands in Bash?](https://stackoverflow.com/questions/4412238/what-is-the-cleanest-way-to-ssh-and-run-multiple-commands-in-bash) Warning: if `cd` fails, your script can execute commands in the wrong place, with potentially weird and/or disastrous results. – Gordon Davisson Jan 29 '20 at 03:00
  • @CharlesDuffy no this is a piece of code to read the only line (my password) file.txt that contain my password. I can delete the loop. – onehalf Jan 29 '20 at 03:19
  • @GordonDavisson Thank you. But in which way cd could fail? Anyway i don't think the script ran the cd. Or at least I believe so. – onehalf Jan 29 '20 at 03:22
  • @onehalf, just `IFS= read -r line – Charles Duffy Jan 29 '20 at 03:22
  • @onehalf, and correct, your script currently doesn't run `cd` until after `ssh` exits -- but when it does run it, it runs it *locally*. When you run `command1; command2`, `command2` doesn't run until `command1` exited. That's true even if `command1` is `ssh`. – Charles Duffy Jan 29 '20 at 03:22
  • Anyhow, the answers to the linked duplicate (see the link up at the top of your question!) tells you how to get around that. – Charles Duffy Jan 29 '20 at 03:24
  • 1
    also this is not very secure. see the 'Security Considerations' section of https://linux.die.net/man/1/sshpass – Red Cricket Jan 29 '20 at 03:24
  • Yup. Better to tell sshpass to get the password straight out of a file (that you can set not to be readably by other users) than to put it on a command line (that's readable by everyone, even untrusted users like `nobody`). Or much better than that to not use password authentication *at all*, and switch to public-key auth (ideally, with a key that your computer can't even read, f/e, one escrowed on a smartcard). – Charles Duffy Jan 29 '20 at 03:25
  • @onehalf [Here](https://apple.stackexchange.com/questions/378942/cant-initialize-osx-interrogation-folder) is an example of what can happen when you assume `cd` won't/can't fail (summary: it appears to have deleted the entire volume, or at least enough that it wouldn't boot). – Gordon Davisson Jan 29 '20 at 03:34
  • Ok thank you. I have to learn a lot of basics before get into this stuff. – onehalf Jan 29 '20 at 22:34

0 Answers0