I want to open a new terminal and ssh to a remote machine in the opened terminal, and this terminal has to be kept open so that it can be used later to work on.
I tried the command : gnome-terminal -x ssh user#IPaddress. But I am unable to give any commands in the newly opened terminal.
Can anyone please tell me where I am wrong and correct me?
Thanks in advance,
Saeya
I just tested exactly the code you wrote (inserting a valid user and ip, of course) and it worked fine, so I can't see where you've gone wrong.
if your session is timing out once it is established you can add the ServerAliveInterval option to the command line
gnome-terminal -e 'ssh -o ServerAliveInterval=60 -l user server'
The following command worked successfully : gnome-terminal --window-with-profile=NOCLOSEPROFILE -e "ssh -X $user#$IPaddress".
Related
I wish to ssh into a server in my department, cd to a directory automatically, and hopefully be able to auto-run other commands if I want. I have tried several ways that have worked for lots of people on stack-overflow, but they did not work for me.
I have tried the methods from the following threads:
Can I ssh somewhere, run some commands, and then leave myself a prompt?
Run ssh and immediately execute command
In particular, I have tried:
1. ssh one-liner
ssh -t user#domain.com 'cd /some/path; bash -l'
and
2. using the expect script
#!/usr/bin/expect -f
spawn ssh $argv
send "cd /some/path\n"
interact
Both codes look fine, and have worked for people in the threads. However, they did not work for me.
The problem, I expect, lies in the fact that the department server asks me automatically about my terminal type as I login, preventing my auto-commands to be run properly.
$ ssh -t user#domain.com 'cd /some/path; bash -l'
Terminal type? [xterm-256color]
After hitting , it takes me into the home directory as if I haven't cd yet.
The second way gives a similar result.
How can I get over this? Thank you very much in advance!
I have couple of bash command that I want to execute and was hoping to get some help to write a script to execute them with one script if possible.
On one console, I want to execute a command iproxy 2222 22. The console will then print waiting for connection.
I'll have to open another console to execute a command ssh -p2222 root#localhost. Once I remote into a phone, I want to execute a simple command like ls.
I'm getting stuck on opening a second console and executing the command.
Can anyone give me some hint?
Thanks
You have several things here:
First, to run both the iproxy and the ssh in the same script, without having to use two different consoles you need to know how to run commands in background. This is easily done by appending & to the end of a command. In the next example the iproxy command will be run in background, and the ssh command will be run in the foreground at the same time:
iproxy 2222 22 &
ssh -p2222 root#localhost
Then, to execute a command over the remote shell opened by the ssh command you just need to include it as the last part of the ssh call. The next example will open a SSH connection to root#localhost on the port 2222, then it will execute a ls command in the remote shell, and finally it will close the SSH connection:
ssh -p2222 root#localhost ls
Finally, to launch a new terminal and execute a command (or a script) in it, you just need to call the type of terminal of your choice, using the -e option with the command (or the name of the script) to be executed. The next example will open a new gnome-terminal and will execute the previous ssh example:
gnome-terminal -e "ssh -p2222 root#localhost ls"
Alternatively you can open a new kconsole or a new xterm (or any other kind of terminal you may have installed in your system).
You will notice that the terminal will close itself after executing the command. If you need or want it to remain open, then you will have to modify the call according to the type of terminal you have opened:
For xterm you will need to use the -hold option.
For kconsole you will need to use the --noclose option.
For gnome-terminal this is a bit more tricky. The way to do it is to create a profile, then modify the profile preferences to hold the terminal when command exits, and referencing to this profile when calling the terminal: gnome-terminal --window-with-profile=NAMEOFTHEPROFILE -e command.
Putting all together, your script should be more or less like this:
#!/bin/bash
iproxy 2222 22 &
xterm -hold -e "ssh -p2222 root#localhost ls"
I am on a server and running a script file that has following code.
ssh username#servername
sudo su - root
cd /abc/xyz
mkdir asdfg
I am able to ssh... but then the next command is not working.. the script is not sudo-ing. any idea?
Edit: Able to create a mech id and then do the things.. though still looking for the answer to above question :|
First of all your command will "stuck" on the first line because it will go into an interactive mode. The ssh command will require a password to be provided by a user (unless there is an sshkey being used) . And if the ssh is logged into the remote server then it will wait for user commands from standard input.
Secondly the lines following the ssh command will be executed only when the first process has exited. This is why your script is not "sudoing" - it's waiting for the ssh to end.
So if your point is to run a command on a remote server then put the command as a parameter into the same line as ssh connection. In your case:
ssh user#server sudo su - root
But this will not be of satisfaction for you. I suggest you create a script of what you want to execute on the remote server and then execute the script.
ssh user#server scriptName
The sudo thing here is very tricky because again your script might get stuck in the interactive mode waiting for a password to be inserted so I suggest you think again on the basis of the script.
mb47!
You want to run the script on the remote computer, correct?
On the remote machine, create a file containing the commands you would like to execute.
Then, on the other machine, run ssh user#machine /path/to/script/you/created/earlier
I hope this helps!
ALinuxLover
In the following context : VisualVM over ssh
I try to execute the 2 following commands in a single script:
ssh -D 9696 john.doe#121.122.123.124
/usr/bin/jvisualvm -J-Dnetbeans.system_socks_proxy=localhost:9696 \
-J Djava.net.useSystemProxies=true
Having the 2 command like this does not work because the ssh command starts in an interactive mode, so the VisualVM is started after the ssh is closed (explicitly with an 'exit').
What could be a good way to solve that issue?
PS. I am running MacOS X.
try:
ssh john.doe#121.122.123.124 '/usr/bin/jvisualvm -J-Dnetbeans.system_socks_proxy=localhost:9696 -J Djava.net.useSystemProxies=true'
If I understand your use case properly, you want to setup port-forwarding with the ssh connection then the second command is run on the localhost which uses the forwarded port on the localhost. I think you could try the -f or -n options to ssh to achieve this. It does however require a command to be run on the remotehost. You could use a bogus command like echo &> /dev/null for that.
EDIT:
Something like this seemed to work in a naïve test:
ssh -f -D <port> remotehost <dummy_program_that_doesnt_quit>
This is best done using an SSH key and screen, so that we interact with and can close the SSH session.
I'm also presuming jvisualvm takes control of the terminal so that when it exits, we clean up the screen session. If jvisualvm detaches from the terminal, the script immediately jumps to cleaning up the screen session while jvisualvm is running.
ssh-add .ssh/key
screen -dmS sshproxy ssh -i .ssh/key -D 9696 john.doe#121.122.123.124
/usr/bin/jvisualvm -J-Dnetbeans.system_socks_proxy=localhost:9696 \
-J Djava.net.useSystemProxies=true
screen -r -d sshproxy -X quit
I'm having a curious problem with a little script to make a VNC connection to a remote host. The script just makes an SSH tunnel for the VNC session and then opens the viewer. It's only two lines, and when copied into the shell manually, it works fine. However, invoking the script causes the VNC viewer to fail with this error:
main: unable to connect to host: Connection refused (111)
Here's the script:
#!/bin/bash
ssh -N -L5903:localhost:5903 username#example.com &
vncviewer :3
The tunnel lives throughout the process, so that isn't the problem. Neither is permissions -- the same error occurs when the script is run as root. I've got public key authentication set up, so it's not that ssh is requesting a password.
What am I missing? The commands work when typed in the shell!
Thanks in advance.
Most likely the vncviewer command is being executed too quickly after the ssh command. Try putting
sleep 3
between those two commands to allow time for the port forwarding to be set up.