r/bash • u/ireg_god • Mar 23 '22
help Save Output into a file (expect)
Hi All,
I am new to the scripting world and was assigned a task to create a script at my workplace which basically log's in to a remote server, executes a command and needs to save the output of the command into a file.
Script below.
#!/usr/bin/expect
#!/bin/bash
spawn ssh xxx.xxx.xxx.xxx
expect "login:"
send "username\r"
expect "Password:"
send "password\r"
sleep 5
send -- "command to check some statistics\r"
sleep 5
send -- "exit\r"
interact
The issue im having now is how to save the output of "send -- "command to check some statistics\r"" into a file ?
TIA
2
u/ohsmaltz Mar 24 '22
3
u/marauderingman Mar 23 '22
Two things:
sshcommand enables you to provide authentication credentials, and a command to run on the remote host. There is no need to useexpectto walk through these operations pseudo-interactively. See the ssh manpage for details on these options.To answer your question, I suggest you read the section on Redirection in the bash manpage, as it will provide you with much needed background information in addition to the answer you seek.
The only real question is whether you want the command output file to be stored on the remote host, or on the host where your script is launched. The answer will determine where you must place the redirection.
1
u/cirosantilli Mar 18 '25
Here's a useful pattern:
myscript.tcl
This outputs only the stdout of one command to stdout, e.g. here we ran
printfinsidebashto obtain:and then when calling you just:
Related: