I am trying to run python script inside tmux session. I wrote a command (tmux new-session -d -s my_session) which is running fine from crontab.
But when I am trying to run python or shell file with tmux new-session -d -s my_session 'python3 test.py or tmux new-session -d -s my_session 'sh test.sh
The script doesn't run. I used the reference from here.
Please help me with this.
Edit
You can separate tmux commands with \;, then use the send-keys command to send the command to the active window.
In your case you can use:
tmux new-session -d -s my_session \; send-keys "python3 test.py" Enter
tmux new-session -d -s my_session \; send-keys "sh test.sh" Enter
tmux new-session -d -s my_session \; send-keys "python3 -m http.server 8080" Enter
You can find more about send-keys options on the tmux manpages section for send-keys:
send-keys [-lMRX] [-N repeat-count] [-t target-pane] key ...
(alias: send)
Send a key or keys to a window. Each argument key is the name of the key (such as ‘C-a’ or ‘NPage’) to send; if the string is not recognised as a key, it is sent as a series of characters. The -l flag disables key name lookup and sends the keys literally. All arguments are sent sequentially from first to last. The -R flag causes the terminal state to be reset.
-M passes through a mouse event (only valid if bound to a mouse key binding, see MOUSE SUPPORT).
-X is used to send a command into copy mode - see the WINDOWS AND PANES section.
-N specifies a repeat count.
The send-keys syntax is described on the Key Bindings section of the tmux manpage. The key names used by send-keys are the same ones used by bind-key.
I usually work with different configuration files, on top of a base file.
Imagine that you've your tmux configuration in ~/.tmux.conf I then create different configuration files in my ~/.tmux/ folder. As an example I can have a python configuration file (use the attach if you want to enter in the session):
# To use this configuration launch tmux with the command:
# > tmux -f ~/.tmux/python.conf attach
#
# Load default tmux config
source-file ~/.tmux.conf
# Create session and launch python script
new-session -s python -n python -d -c ~/src/python/
send-keys "python test.py" Enter
This gives me the flexibility to create much more complex sessions.
Related
I am trying to link a window from another session by specifying target session using format variable. In that way I hope to get it always linked next to the current active window.
The hard coded version of the working command:
:link-window -a -s 1:remote -t 0:2
in which case I specify a target pane literaly. When I try any of:
:link-window -a -s 1:remote -F -t "#{session_name}":"#{window_index}"
:link-window -a -s 1:remote -F "#{session_name}":"#{window_index}"
:link-window -a -s 1:remote -t "#{session_name}":"#{window_index}"
I got an error. The notable part here is that when I do use -F flag, the usage for link-window command is displayed. And when I omit it and use only -t, the error is cann't find window #{session_name}
Does it mean that link-window command simply doesn't support format variables?
-t does not support format variables and link-window does not support -F. run-shell will expand so you can do it by doing, for example:
run "tmux linkw -t '#{session_name}'"
I've been able to turn-on the vi-mode inside tmux successfully by pressing C-a : (I've changed default prefix) and then typing set-window-option mode-keys vi. However, I can't make it stick by adding into ~/.tmux.conf. Here is my config:
# Change default prefix key to C-a
unbind-key C-b
set -g prefix 'C-a'
bind-key 'C-a' send-prefix
# Configure Vim mode for navigating text and selection
set-window-option -g mode-keys vi
bind-key -t vi-copy 'v' begin-selection
bind-key -t vi-copy 'y' copy-selection
# Allow navigating panes with vi-like commands.
bind k selectp -U
bind j selectp -D
bind h selectp -L
bind l selectp -R
Notably, navigating panes using kjhl keys also doesn't work. Perhaps this is because tmux fails to execute set-window-option earlier in the script. What's wrong here?
Try using setw -g mode-keys vi
"set-window-option -g mode-keys vi" works in tmux 2.1 and above.
"setw -g mode-keys vi" works in tmux 1.8
Note that
bind-key -t vi-copy 'v' begin-selection
bind-key -t vi-copy 'y' copy-selection
will no longer work as of tmux 2.4. For the same functionality (using xclip for copying), instead use
bind-key -T copy-mode-vi 'v' send -X begin-selection
bind-key -T copy-mode-vi 'y' send -X copy-pipe-and-cancel "xclip -in -selection clipboard"
from this blog post
Another thing that may affect this: tmux turns on vi keys for copy mode only if it finds 'vi' in $VISUAL or $EDITOR. I had changed both to use neovim remote tool (nvr -ls) and suddenly tmux copy keys I had bound in copy-mode-vi stopped working because it defaulted back to using emacs keybindings. Check that your $VISUAL and EDITOR contains some version of the string 'vi'.
I have a script which runs another script via SSH on a remote server using sudo. However, when I type the password, it shows up on the terminal. (Otherwise it works fine)
ssh user#server "sudo script"
What's the proper way to do this so I can type the password for sudo over SSH without the password appearing as I type?
Another way is to use the -t switch to ssh:
ssh -t user#server "sudo script"
See man ssh:
-t Force pseudo-tty allocation. This can be used to execute arbi-
trary screen-based programs on a remote machine, which can be
very useful, e.g., when implementing menu services. Multiple -t
options force tty allocation, even if ssh has no local tty.
I was able to fully automate it with the following command:
echo pass | ssh -tt user#server "sudo script"
Advantages:
no password prompt
won't show password in remote machine bash history
Regarding security: as Kurt said, running this command will show your password on your local bash history, and it's better to save the password in a different file or save the all command in a .sh file and execute it. NOTE: The file need to have the correct permissions so that only the allowed users can access it.
Sudo over SSH passing a password, no tty required:
You can use sudo over ssh without forcing ssh to have a pseudo-tty (without the use of the ssh "-t" switch) by telling sudo not to require an interactive password and to just grab the password off stdin. You do this by using the "-S" switch on sudo. This makes sudo listen for the password on stdin, and stop listening when it sees a newline.
Example 1 - Simple Remote Command
In this example, we send a simple whoami command:
$ ssh user#server cat \| sudo --prompt="" -S -- whoami << EOF
> <remote_sudo_password>
root
We're telling sudo not to issue a prompt, and to take its input from stdin. This makes the sudo password passing completely silent so the only response you get back is the output from whoami.
This technique has the benefit of allowing you to run programs through sudo over ssh that themselves require stdin input. This is because sudo is consuming the password over the first line of stdin, then letting whatever program it runs continue to grab stdin.
Example 2 - Remote Command That Requires Its Own stdin
In the following example, the remote command "cat" is executed through sudo, and we are providing some extra lines through stdin for the remote cat to display.
$ ssh user#server cat \| sudo --prompt="" -S -- "cat" << EOF
> <remote_sudo_password>
> Extra line1
> Extra line2
> EOF
Extra line1
Extra line2
The output demonstrates that the <remote_sudo_password> line is being consumed by sudo, and that the remotely executed cat is then displaying the extra lines.
An example of where this would be beneficial is if you want to use ssh to pass a password to a privileged command without using the command line. Say, if you want to mount a remote encrypted container over ssh.
Example 3 - Mounting a Remote VeraCrypt Container
In this example script, we are remotely mounting a VeraCrypt container through sudo without any extra prompting text:
#!/bin/sh
ssh user#server cat \| sudo --prompt="" -S -- "veracrypt --non-interactive --stdin --keyfiles=/path/to/test.key /path/to/test.img /mnt/mountpoint" << EOF
SudoPassword
VeraCryptContainerPassword
EOF
It should be noted that in all the command-line examples above (everything except the script) the << EOF construct on the command line will cause the everything typed, including the password, to be recorded in the local machine's .bash_history. It is therefore highly recommended that for real-world use you either use do it entirely through a script, like the veracrypt example above, or, if on the command line then put the password in a file and redirect that file through ssh.
Example 1a - Example 1 Without Local Command-Line Password
The first example would thus become:
$ cat text_file_with_sudo_password | ssh user#server cat \| sudo --prompt="" -S -- whoami
root
Example 2a - Example 2 Without Local Command-Line Password
and the second example would become:
$ cat text_file_with_sudo_password - << EOF | ssh va1der.net cat \| sudo --prompt="" -S -- cat
> Extra line1
> Extra line2
> EOF
Extra line1
Extra line2
Putting the password in a separate file is unnecessary if you are putting the whole thing in a script, since the contents of scripts do not end up in your history. It still may be useful, though, in case you want to allow users who should not see the password to execute the script.
Assuming you want no password prompt:
ssh $HOST 'echo $PASSWORD | sudo -S $COMMMAND'
Example
ssh me#localhost 'echo secret | sudo -S echo hi' # outputs 'hi'
The best way is ssh -t user#server "sudo <scriptname>", for example ssh -t user#server "sudo reboot".
It will prompt for password for user first and then root(since we are running the script or command with root privilege.
I hope it helped and cleared your doubt.
NOPASS in the configuration on your target machine is the solution. Continue reading at http://maestric.com/doc/unix/ubuntu_sudo_without_password
echo $VAR_REMOTEROOTPASS | ssh -tt -i $PATH_TO_KEY/id_mykey $VAR_REMOTEUSER#$varRemoteHost
echo \"$varCommand\" | sudo bash
confirming that the answer of #ofirule is working like a charm.
I try ot even with sshpass & it works. This is how to use it with sshpass:
echo $pass | sshpass -p $pass ssh -tt cloud_user#$ip "sudo su -"
you will find yourself in the root shell directly
This is the first time it has happened to me where I am using the su command and it actually displays the password on the terminal and doesn't stay hidden. Here is my code snippet:
sshpass -p "password" ssh -q username#74.11.11.11 "su -lc 'mkdir temp/'"
Code explanation: I am accessing a remote server and trying be root on that server to create a folder. In doing so I have to use the su command and it prompts me for the password. When I enter the password, it gets displayed and doesn't stay hidden. How do I fix that?
The solution is to allocate a pseudo TTY (using the -t option on ssh):
sshpass -p "password" ssh -t -q username#74.11.11.11 "su -lc 'mkdir temp/'"
Without this, there's no "terminal" in this context and su is unable to disable echo of the password.
Just like I replied to you here.
It's possible to keep it "hidden" from the command line:
Edit your /etc/profile and paste there:
export SSHPASS='my_pass_here'
Use the -e argument with sshpass command
$ sshpass -e ssh usernmane#hosting.example 'ls -ll'
Another option is to save your password in a different file and use the -f argument:
$ sshpass -f password_filename ssh usernmane#hosting.example 'ls -la'
But the best solution is to follow the #Hristo Mohamed suggestion:
In general please AVOID using sshpass with a password.
You can set up easily a generate ssh key just to do this job and then remove it.
If i've already put public key to remote host. So there is no password input problem.
I want to login a remote machine and execute screen -r immediately. Is there a way to achieve this ?
For example:
ssh example.com ; screen -r
But this is wrong since screen -r won't send to remote host.
When you run a command on a remote host, by default ssh will not allocate a pseudoterminal. If you run an interactive program like screen on a remote host, you must have a pseudoterminal. The -t option makes this happen. Try:
ssh -t example.com "screen -r"
Remove the semicolon from your example:
ssh example.com "screen -r"
Your not going to get much bandwidth for that particular command though, as it needs an attached terminal in order to execute successfully.
* EDIT 1 *
To run multiple commands, just string them together separated by semi-colon:
ssh example.com "screen -r; ls -al; ps -elfc"
* EDIT 2 *
Still not entirely sure what you are trying to accomplish (was screen -r just an example, or are you really trying to just chain a bunch of commands together?). In any case, I am amending my answer to cover more possibilities:
To chain random commands together:
ssh example.com "ps -elfc; ls"
To run some random commands after running screen:
ssh -t example.com "screen -r; ls"
To specifically run screen and send commands to it:
ssh -t example.com "screen -r -X ls"
You can also echo your command to the remote host:
echo "command" | ssh user#host
http://unixhelp.ed.ac.uk/CGI/man-cgi?ssh+1
ssh [-1246AaCfgkMNnqsTtVvXxY] [-b bind_address] [-c cipher_spec] [-D
[bind_address:]port] [-e escape_char] [-F configfile]
[-i identity_file] [-L [bind_address:]port:host:hostport]
[-l login_name] [-m mac_spec] [-O ctl_cmd] [-o option] [-p port] [-R
[bind_address:]port:host:hostport] [-S ctl_path] [-w tunnel:tunnel]
[user#]hostname [command]
Command is the last parameter. ; tells the local shell that ssh and screen are two different commands, not that one is a command and the other is an argument.
Not sure if it will work or not since screen is an odd program to do it with, but ssh blah.com 'screen -r' is the correct syntax.
This will work
ssh root#something 'ls -l'
Try using single quotes:
ssh example.com 'screen -r'
Just put the command you want to run after the host name:
ssh example.com screen -r
try this one...
ssh -XY -t user#remote_IP 'ssh -XY -t user#remoteToRemote_IP'
you can continue with command screen -r or any other at remoteToRemote_IP machine.
All you need to do is pass the -t flag.
This:
ssh -t v tmux attach -t o
runs tmux with the specified options (-t o means to connect to the session named o)
If I run it without the -t I get an error:
➜ ~ ssh v tmux attach -t o
open terminal failed: not a terminal
The quotes are optional. This also works, but involves typing two extra keystrokes:
ssh -t v 'tmux attach -t o'
By the way my ssh host is called v because I added an entry to ~/.ssh/config. The v stands for vagrant, and it goes to a user I created instead of the vagrant user that ships by default.