MrPickles
MrPickles

Reputation: 1445

How to pass local text file to ssh command?

I am writing a script in Unix that needs to:

  1. Create a local text file
  2. Log in via ssh (e.g. user@servername)
  3. Do stuff with local text file
  4. End script

Unfortunately, I can't do this because once the script logs in via ssh, it can't see the text file. So I am trying to pass a local text to the ssh command, so it will see the text file.

Text file is resembles this

user@servername1
user@servername2
user@servername3
... etc

I looked on a previous answer from SuperUser, but their solution just returns errors for me.

ssh user@servername < text.txt
#stty: : Invalid argument
#sh: user@servername1: not found
#sh[2]: user@servername2: not found
#sh[3]: user@servername3: not found
#...etc

Upvotes: 3

Views: 9982

Answers (3)

jozxyqk
jozxyqk

Reputation: 17304

As an alternative to @glennjackman's two connections to copy and then execute...

Since you can pipe commands to ssh, which is quite similar to your example,

echo "ls" | ssh localhost

All you need is something in the middle to interpret the text file. For example...

cat text.txt | sed 's/^/finger /' | ssh user@servername
#or
cat text.txt | ssh user@servername xargs finger

In your example, ssh user@servername and user@servername1 are pretty similar. Just to be clear you didn't want to use those in the ssh command itself?:

while read user
do
ssh $user echo hi #ssh to each user/server in turn
done <text.txt

Upvotes: 0

Ruslan Gerasimov
Ruslan Gerasimov

Reputation: 1782

if you need to work with local file, not with remote copy, you can try to do next: call ssh command in a different terminal, then you can continue to work locally and process you local file

xterm -e "bash -c \"ssh user@servername; exec bash\"" &

Upvotes: 0

glenn jackman
glenn jackman

Reputation: 247042

Try:

  1. create a local text file
  2. transfer the file to the remote host: scp local_file user@servername:./remote_file
  3. login to the remote host and do stuff: ssh user@servername cat -n remote_file

Upvotes: 2

Related Questions