0

so im trying to make an if statement to tell me if an sftp connection was sucessfull or failed, and if its a sucess i want to run a piece of code that automates an sftp download that ive already made. My problem is that this if statement executes this sftp connection, and then prompts me for a password and stalls the rest of the code. i wanted to do something like this

if ( sftp -oPort=23 user@server )
then
expect <<-EOF
spawn sftp -oPort=23 user@server
.....

I want to know if its possible for me to make the if statement not execute the sftp connection and then not prompt me , maybe execute it on the background or something.

I would appreciate if someone could tell me if what im asking is possible, or propose a better solution to what im trying to do, thanks

Letho123
  • 99
  • 1
  • 2
  • 9
  • Just don't try to reconnect once you're connected. Anyway, depends on what you intend to do next. You'll probably try to send commands via the connection, but it won't work because `sftp` will be expecting input from the standard input by that point. – Federico klez Culloca May 23 '19 at 10:41
  • After your edit, just get rid of the `if`, unless you need to do something else after you connected and send the commands. It will error out anyway. – Federico klez Culloca May 23 '19 at 10:43
  • thats the problem i cant IF my actual stfp connection command, cause its under an heredoc and its also inside expect, which i need to make the automation part – Letho123 May 23 '19 at 10:43
  • yeah i need to download some files after im connected – Letho123 May 23 '19 at 10:43
  • `except` has conditionals. Take a look at [this](https://stackoverflow.com/questions/1538444/using-conditional-statements-inside-expect) question and relative answers. It may give you some idea on how to proceed. – Federico klez Culloca May 23 '19 at 10:52
  • i will take a look at it ,thanks – Letho123 May 23 '19 at 11:13
  • (of course I meant `expect` and I can no longer edit that comment) – Federico klez Culloca May 23 '19 at 11:39

1 Answers1

1

You cannot not-execute a command and then react on the return value of the executed command (because this is what you really want to do: check if you can run sftp successful, and if so do a "proper" run; but you'll never know whether it can run successfull without running it).

So the main question is, what it is what you actually want to test.

If you want to test whether you can do a full sftp connection (with all the handshaking and what not), you could try running sftp in batch-mode (which is handily non-interactive).

E.g. the following runs an sftp session, only to terminate it immediately with a bye command:

if echo bye | sftp -b - -oPort=23 user@server ; then 
  echo "sftp succeeded"
fi

This will only succeed if the entire sftp session works (that is: you pass any key checks; you can authenticate, ...). If the server asks you for a password, it will fail to authenticate (being non-interactive), and you won't enter the then body.

If you only want to check whether something is listening on port 23, you can use netcat for this:

if netcat -z server 23; then
  echo "port:32 is open"
fi

This will succeed whenever it can successfully bind to port 23 on the server. It doesn't care whether there's an sftp daemon running, or (more likely) a telnet daemon.

You could also do some minimal test whether the remote server looks like an SSH/SFTP server: ssh servers usually greet you with a string indicating that they indeed speak ssh: something like "SSH-2.0-OpenSSH_7.2p2 Ubuntu-4ubuntu2.4". With this information you can then run:

if echo QUIT | netcat server 23 | grep SSH; then
 echo "found an ssh server"
fi
umläute
  • 28,885
  • 9
  • 68
  • 122
  • my objective here is, to connect to a bunch of servers that shoud be connectable to by ssh, but there are always some that are going trough some type of error which makes it impossible to connect to them by ssh. I want to make a script that connect trough all these servers and prints a line to a file like "couldnt connect to ....", i have figured a good work around now, so i dont need help anymore but your answer still seems useful ,so thanks – Letho123 May 24 '19 at 13:38