I am writing a script to upload DLLs to a remote machine. I first use a command to ssh in and stop the service running. I then try to upload the new DLLs via scp, but it fails basically immediately with lost connection.
My entire shell script looks like this:
ssh -p 29170 DLL_Uploader#XXX.XXX.XX.XXX "powershell.exe; .\stop_file_watcher.ps1; exit";
scp -P 29170 $1 "DLL_Uploader#XXX.XXX.XX.XXX:/Bin/File Watcher Service"
ssh -p 29170 DLL_Uploader#XXX.XXX.XX.XXX "powershell.exe; .\start_file_watcher.ps1; exit";
You can see a pastebin of this scp debug output with -vvv here.
Related
I am creating a Jenkins job in which am running a ssh command to execute a script for comparing two folders using diff command on a remote server. Script is running fine, output file is getting created. But after this command Jenkins execute shell block is failed.
Command:
ssh -T user#dtest.com "bash /tmp/sample.sh" >> result.txt
Log:
ssh -T user#dtest.com "bash /tmp/sample.sh" >> result.txt
stdin: is not a tty
"Execute shell" is marked as failure
I am not sure what sample.sh is supposed to do, but I understand that you are trying to capture what is logged by this script.
I would try several solutions:
ssh -T user#dtest.com "bash /tmp/sample.sh >> result.txt"
This should save your output in your remote server. Then you could copy this file from remote to local using:
scp user#dtest.com:/remote/dir/result.txt /local/dir/
More context: Copying files from server to local computer using ssh
If you are choosing this solution, you could also consider to write your result.txt directly from your script, and keep the console output for important logging purpose.
Another Solution I could think of would be to use
ssh user#dtest.com "bash /tmp/sample.sh" > result.txt
With this solution you will redirect your output directly to your local machine.
But you will need to delete the ssh "-T" option. And you will run into other problems with Jenkins. So this might not fit you.
ssh -T Disables pseudo-tty allocation, what sounds like your problem's root cause. (https://docs.oracle.com/cd/E36784_01/html/E36870/ssh-1.html)
I have one file in my local Linux machine and I want to move that one to remote machine and then execute one command on remote machine to restart the service. The issue is after moving the file the remote connection get closed. I have used the following command:
rsync --remove-source-files -av -e ssh -t /home/testdata.txt root#vdstvmaster:/home/; service restart
If I execute the above command file is successfully moved to remote machine. But the second command (service restart) is not executed on remote machine.
rsync can use a remote shell to perform the copy operation. But it is not meant as a "general-purpose" remote shell application. Just invoke the second command over SSH locally after the rsync command like this:
rsync --remove-source-files -av -e ssh -t /home/testdata.txt root#vdstvmaster:/home/
ssh root#vdstvmaster service restart
BTW some people may consider remotely logging into another machine as root bad security.
I am working on a script which will be used to transfer a file (using rsync) from a remote location and then perform some basic operations on the retrieved content.
When I initially connect to the remote location (not running an rsync daemon, I'm just using rsync to retrieve the files) I am placed in a non-standard shell. In order to enter the bash shell I need to enter "run util bash". Is there a way to execute "run util bash" before rsync begins to transfer the files over?
I am open to other suggestions if there is a way to do this using scp/ftp instead of rsync.
One way is to exectue rsync from the server, instead of from the client. An ssh reverse tunnel allows us to temporarily access the local machine from the remote server.
Assume the local machine has an ssh server on port 22
Shh into the remote host while specifying a reverse tunnel that maps a port in the remote machine (in this example let us use 2222) to port 22 in our local machine
Execute your rsync command, replacing any reference to your local machine with the reverse ssh tunnel address: my-local-user#localhost
Add a port option to rsync's ssh to have it use the 2222 port.
The command:
ssh -R 2222:localhost:22 remoteuser#remotemachine << EOF
# we are on the remote server.
# we can ssh back into the box running the ssh client via ${REMOTE_PORT}
run utils bash
rsync -e "ssh -p 2222" --args /path/to/remote/source remoteuser#localhost:/path/to/local/machine/dest
EOF
Reference to pass complicated commands to ssh:
What is the cleanest way to ssh and run multiple commands in Bash?
You can achieve it using --rsync-path also. E.g rsync --rsync-path="run util bash && rsync" -e "ssh -T -c aes128-ctr -o Compression=no -x" ./tmp root#example.com:~
--rsync-path is normally used to specify what program is to be run on the remote machine to start-up rsync. Often used when rsync is not in the default remote-shell’s path (e.g. –rsync-path=/usr/local/bin/rsync). Note that PROGRAM is run with the help of a shell, so it can be any program, script, or command sequence you’d care to run, so long as it does not corrupt the standard-in & standard-out that rsync is using to communicate.
For more details refer
I have written a bash script which I should run on the remote server(ubuntu) with GUI(zenity) interface and I will issue below command on the local machine.
sshpass -p $PASS ssh root#$SERVER 'bash' < /tmp/dep.sh | tee >(zenity --progress --title "Tomcat Deployer" --text "Connecting to Tomcat Server..." --width=400 --height=150) >>/tmp/temp.log;
I want to transfer a file from my local machine to server and I want to achieve this placing an enter in bash file(/tmp/dep.sh) in the above command itself without opening a new session on server.
I prefer below command to transfer the file to server and I should place this in the bash script(/tmp/dep.sh) and it should run on server to copy the file from my local machine. I don't want to specify my local ip as a variable and use as source in the blow command as the script is used on other machines too and thus ip changes. And I should not transfer the file from my local to server writing a separate rsync & ssh creating one more ssh session.
rsync --rsh="sshpass -p '$PASS' ssh" '$local:$APPATH/$app.war' /tmp
Anybody can do any magic to transfer the file from local to server with the above connected ssh session with the help of above rsync or by other means and without opening new separate connection?
Thank you!
Edit 1:
Could this be achieved with single ssh session(single command)?:
rsync --rsh="sshpass -p serverpass ssh -o StrictHostKeyChecking=no" /home/user1/Desktop/app.war root#192.168.1.5:/tmp;
sshpass -p serverpass ssh -o StrictHostKeyChecking=no root#192.168.1.5 '/etc/init.d/tomcat start'
You'll want to use SSH multiplexing. This is done using the ControlMaster and ControlPath options. Here's an article on it.
I am new to world of scripting. I am getting problem while executing local shell script on remote server using expect script.
my script is following
VAR=$(/home/local/RD/expect5.45/expect -c "
spawn -noecho ssh -q -o StrictHostKeyChecking=no $USER#$HOST $CMD
match_max 100000
expect \"*?assword:*\"
send -- \"$PASS\r\"
send -- \"\r\"
send \"exit\n\r\"
expect eof
")
It is working fine if CMD is basic commands like df -kh;top.
But I need to collect several stats on remote server for which i have created a shell script.
I have tried following with no luck
spawn -noecho ssh -q -o StrictHostKeyChecking=no $USER#$HOST 'bash -s' < localscript.sh
its not able to pick and execute localscript on remote server.
Please help to resolve this issue.
The last time I tried something like this, I quickly grew weary of using expect(1) to try to respond to the password prompts correctly. When I finally spent the ten minutes to learn how to create an ssh key, copy the key to the remote system, and set up the ssh-agent to make key-based logins easier to automate, I never had trouble running scripts remotely:
ssh remotehost "commands ; go ; here"
First, check if you need to create the key or if you already have one:
ls -l ~/.ssh/id_*
If there are no files listed, then run:
ssh-keygen
and answer the prompts.
Once your key is generated, copy it to the remote system:
ssh-copy-id remote
Most modern systems run ssh-agent(1) as part of the desktop start up; to determine if you've got the agent started already, run:
ssh-add -l
If you see "The agent has no identities.", then you're good to go. If you see "Could not open a connection to your authentication agent." then you'll have to do some research about the best place to insert the ssh-agent(1) into your environment. Or, forgo the agent completely, it is just a nice convenience.
Add your key, perhaps with a timeout so it is only valid for a short while:
ssh-add -t 3600
Now test it:
ssh remote "df -hk ; ps auxw ; ip route show ; free -m"
expect(1) is definitely a neat tool, but authentication on remote systems is easier (and more safely) accomplished with SSH keys.