Letho123
Letho123

Reputation: 129

how to send the output of a command that runs on the connected machine, to a file in the connecting machine

I am making a script to access a bunch of machines through ssh and run the df -h command to check if their disk usage is getting near 100% , my problem is that I can't run the command on the machine that when I run the command, i cant send it to a file in the machine that I am using to connect. I have tried this

df -h > /dir/file

but that just creates a file in the machine I am connected to. How would I go about doing what I am trying to do?

the full code that im using to do this is

expect <<-EOF
spawn ssh -oPort=23 x.x.x.x df -h > /home/file
expect "*password: " {send "password\r"}
expect "*#" {send "exit\r"}
EOF

Any help is appreciated.

Upvotes: 2

Views: 101

Answers (2)

Dennis Williamson
Dennis Williamson

Reputation: 359955

Move the redirection to the beginning of the heredoc:

expect <<-EOF > /home/file
spawn ssh -oPort=23 x.x.x.x df -h
expect "*password: " {send "root\r"}
expect "*#" {send "exit\r"}
EOF

(untested)

Upvotes: 1

Mark Setchell
Mark Setchell

Reputation: 207405

Like this:

ssh remoteMachine 'df -h' > path/to/local/file

Upvotes: 1

Related Questions