sftp batch file not able to execute - ftp

I have a windows batch file to connect from Server A (Windows) to
Server B (UNIX) via sftp to get a file. The script is as below:
sftpg3 -oStrictHostKeyChecking=no -oIdentityFile=EAPIINSTADM_hostnameA ftpeapsg#hostnameB
lcd D:\APPBASE\EAPSG\GEMSSG
get GENUOBGW1 /sftp/ftphrssg/HRSSG/EAPSG
exit
When I run the script it stops after running the first line i.e sftpg3 -oStrictHostKeyChecking=no...
D:\APPBASE\EAPSG\GEMSSG>sftpg3 -oStrictHostKeyChecking=no -oIdentityFile=EAPIINSTADM_hostnameA ftpeapsg#hostnameB
Warning: ignoring unsupported option -o
Warning: ignoring unsupported option -o
Remote system type is POSIX.
sftp>
It didn't execute below lines:
lcd D:\APPBASE\EAPSG\GEMSSG
get GENUOBGW1 /sftp/ftphrssg/HRSSG/EAPSG
If I run the command manually one line at a time it works.
Any idea why the script does not run completely?
Thank you.

That's because sftpg3 is a sftp like client which prompt an interactive session with the server waiting for input. so your first line in the script just opens the session and wait until that command (which is the prompt opened) to exit. you need to redirect the input into that prompted session (in unix like using <<) I don't know how it is done in windows. but if the aim from writing that script is to copy a file from the server you can just use scpg3

Related

Error when run simple shell script on opensuse15.1: SubEntry: number of args (2) is invalid

The following error occurred every time I tried to run a simple shell script(test.sh):
SubEntry: number of args (2) is invalid
This single line code is in the test.sh:echo -e 'open 192.168.1.123 \nuser root pass \nput test.csv \nquit'|ftp -inv
If I run the code line directly in the command line works OK: the file test.csv is transferred successfully via FTP on server 192.168.1.123.
Any one know why I get that error when I run the shell script?!Thank you!
I found another solution for sending files via FTP using a script.
I use curl to send the file like this:
curl -T your_file ftp://your_IP
This command can be put in a script and automatic run that script.Works for me.

automate getting diagnostic files from a controller via ssh commands

I'd like to automate getting diagnostic files from a controller that responds to ssh commands, like e.g.
ssh diag#controller tarred > diags.tgz
Unfortunately, I have to type a password to make the above command go through.
What have I considered to get around that:
using ssh keys: not possible, since I can't login to the controller, it just expects commands and doesn't offer a shell
using ssh-pass package: I don't have admin rights on the machine and can't install packages
using "expect": works to some extent, but the resulting file is corrupted.
Here's the "expect" script I've used:
#!/usr/bin/expect -f
log_user 0
set timeout 300
spawn ssh diag#controller tarred
expect "?assword:"
send "unrealpassword\r"
expect \r\n
log_user 1
expect eof
The script makes sure that only the required output gets stored with the "log_user" commands until eof is encountered.
I've piped this script to a file and that file is corrupted, i.e. it's either too short (because of a timeout?) or too long (?).
Any idea about what goes wrong here.?

Parsing SSH stream from batch

Using batch script to run ssh, I am finding all the output is dumped into the standard error... so my command:
ssh -i keyfile user#host "commands" 2> error.log
captures the remote server prompt for password if there are no matching keys in the local known_hosts...
This leaves me no way to capture the output for error processing or logging without leaving the user of my batch script stuck no knowing what the blank prompt is.
My other thought is to do a simple ssh to test the connection first and establish the password prompt if it's needed, then move to the command of interest. But I feel like if the first one passes, then the only thing I'm error is my remote command.
I've tried
>CON 2> error.log
... seems to do the same thing.
Unfortunately there's no TEE command default in Windows.
My best solution is to:
1) echo Enter password
2) ssh "params" 2>error.log
Suggestions?
If the remote system supports the syntax, you could do something like this:
ssh -i keyfile user#host "commands 2>&1" > output.log 2> error.log
This redirects the remote command's error output to its standard output. ssh's own standard output and standard error aren't affected. The 2>&1 part is Bourne shell syntax to redirect standard error (descriptor 2) to standard output (descriptor 1). It should work if the remote shell is sh, bash, or ksh.

Suppress welcome message on bash remote command execution

I'm executing some commands on remote server within a shell script like this:
ssh user#host <<ENDSSH
...
ENDSSH
Upon login I'm getting a standard server welcome message echoed. Is there a way to send it to \dev\null but to keep displaying the output of executed commands?
Thanks.
Create a file ~user/.hushlogin on the remote host. This will suppress output from the login program when user logs in (such as time of last login and any message of the day).
You can edit /etc/ssh/sshd_config (for debian/ubuntu, your server might be different file) and turn the following setting to 'no'.
PrintMotd no
PrintLastLog no

Spawn id exp7 not open while executing error shell script

I am using cron to run a script that connects to another server via sftp and uploads files. Once in a while, I will receive the error: Spawn-id-exp7-not-open while executing send \"ls\r\" and the script will stop executing (files will not be sent). However, most of the time, the script will run with no errors.
Below is my script I use to connect :
#!/bin/bash
HOST="removed.com:\API"
USER="user"
PASS="removed"
VAR=$(expect -c "
spawn sftp -o \"BatchMode no\" -b /var/www/prep/cmd -P 13266 $USER#$HOST
expect \"Password:\"
send \"$PASS\r\"
expect \"\\\\$\"
send \"ls\r\"
expect -re \"$USER.*\"
send \"exit\"
")
echo "==============="
echo "$VAR"
Below is the script that runs after I connect (cmd) :
put "/var/www/prep/1.xml"
put "/var/www/prep/2.xml"
I took a look at a similar question found at send: spawn id exp7 not open , but it has not helped me. Perhaps the connection ends before the cmd script can run?
Thanks for your help!
You're using sftp with -b. Therefore, once you provide the password, there will be no interaction: sftp will read commands from the batch file and exit.
From the man page:
-b batchfile
Batch mode reads a series of commands from an input batchfile instead of stdin. Since it lacks user interaction it should be used in conjunction with non-interactive authentication. A batchfile of ‘-’ may be used to indicate standard input. sftp will abort if any of the following commands fail: get, put, rename, ln, rm, mkdir, chdir, ls, lchdir, chmod, chown, chgrp, lpwd, df, symlink, and lmkdir. Termination on error can be suppressed on a command by command basis by prefixing the command with a ‘-’ character (for example, -rm /tmp/blah*).

Resources