I'd like to write a script that calls openvpn first, followed by ssh. When typing the command  sudo openvpn ~/my_connection.ovpn   in the command prompt I get the following output:  ... Wed Jan  4 21:04:35 2017 do_ifconfig, tt-&gt;ipv6=0, tt-&gt;did_ifconfig_ipv6_setup=0 Wed Jan  4 21:04:35 2017 /sbin/ip link set dev tun0 up mtu 1500 Wed Jan  4 21:04:35 2017 /sbin/ip addr add dev tun0 local IP_FOO peer IP_FOO Wed Jan  4 21:04:35 2017 Initialization Sequence Completed   This command is blocking and the shell is not released. In order to start an ssh connection afterwards I need to push the openvpn process into the background by typing Ctrl+Z followed by a bg.  However, I'd like to call the openvpn connection step and the ssh automatically by calling just my bash file. How do I manage to emulate the Ctrl+Z and bg steps in this file?  I've tried to append a &amp; to the openvpn command and to place nohup in front of it. Neither works.
16261
How do I run a program in the background of a shell, with the ability to close the shell while leaving the program running? Lets say my UI is having problems or for some reason, I need to boot up a program from the terminal window, say, nm-applet:  nm-applet   When it's started, it occupies the foreground of the terminal window.  Is there any simple way to run the program in the background without needing to leave the terminal open or have it occupy the whole terminal?  On that note, I did find a way to run programs from the terminal and have it allow for other inputs, by appending an ampersand (&amp;) to the command as such:  nm-applet &amp;   But this isn't much use as any processes started in the terminal are killed once the terminal is closed.
70572
I have created a .bat file which automatically takes the SSH server (Jump Server) access. Now, I have couple of commands which I would like to hit in that window.  For an ex. (To take root access)  usrun -u root rush   With this command, it'll ask me to enter the password.  So, how can I achieve this entirely through .bat file?
12487
I have a script which connects to a remote host to replicate some files via SSH.  I must connect to an rsync daemon listening on port 873.  To accomplish this I have a ssh configuration like this.  Host blah     LocalForward 10873 127.0.0.1:873     Hostname 10.2.4.1   Unfortunately because of the timing of this script, there is a small chance that on occasion when it attempts to establish the SSH session another similar script will have the port open to a different host.  If I connect while another ssh session is open I get the obvious message Address already in use.  me@local:~$ ssh blah bind: Address already in use channel_setup_fwd_listener: cannot listen to port: 10873 Could not request local forwarding. Linux remote 2.6.26-2-amd64 #1 SMP Tue Jan 25 05:59:43 UTC 2011 x86_64 motd... remote:~$    What I want to have happen is to have the SSH client immediately exit with a exit code so I can put some logic into my script to report a failure instead of trying to run any commands against the port forwarded to the incorrect host.
79110
I am trying to write a bash script in a file that would, when run start pinging a host until it becomes available, when the host becomes reachable it runs a command and stops executing, I tried writing one but the script continues pinging until the count ends,  Plus I need to put that process in the background but if I run the script with the dollar ($) sign it still runs in foreground,  #!/bin/bash ping -c30 -i3 192.168.137.163 if [ $? -eq 0 ] then /root/scripts/test1.sh exit 0 else echo “fail” fi
7608
This is a copy of my post from stackoverflow; I realize I should have asked it here...  I want to run a script that changes the hostname and have my bash prompt (PS1 variable) update immediately with the proper hostname. How do I make this happen?  I run this  #!/bin/bash # Usage: ./changehost &lt;newhostname&gt;  #Do two sed's to edit the files for persistent hostname change sed -i s/$(hostname)/$1/g "/etc/hosts" sed -i s/$(hostname)/$1/g "/etc/sysconfig/network"  #run the hostname command with new hostname to update it hostname $1   In a terminal like so and get this  [user@host dir]# ./changehost newhostname [user@host dir]#    But what I want is this  [user@host dir]# ./changehost newhostname [user@newhostname dir]#    The terminal is updated properly only when I open up a new shell.  I have also tried to do   export PS1='somestring'; export PS1='[\u@\h \W]\$'    outside of the script in the terminal and it does switch to 'somestring' and back, but the hostname is unchanged. :\   Is it possible that the \h is stored in memory when the process begins and can't be changed after startup?
25536
says:  SSH_ASKPASS    If  ssh needs a passphrase, it will read the passphrase from the    current terminal if it was run from a terminal.  If ssh does not    have  a  terminal associated with it but DISPLAY and SSH_ASKPASS    are set, it will execute the program  specified  by  SSH_ASKPASS    and open an X11 window to read the passphrase.   I'd like SSH to use an askpass program even if it was run from a terminal.  On occasion, I have to connect to servers, where there's some delay in showing a password prompt (maybe due to network issues, maybe due to attempted reverse DNS lookups, …). I get annoyed and switch to something else, and forget about the attempted connection. (Insert joke about attention span of a goldfish.) When I finally get back to it, the prompt's timed out and even a correct password would just result in a closed connection.  Keys would be one solution, but not every system I use has my usual SSH keys. However, I usually use Ubuntu systems, and Ubuntu has an SSH askpass program installed by default.  If an askpass window popped up, however, I'd be immediately aware of it. That's a good enough compromise for me, if only I can get it to work.
4738
I sometimes have long running processes that I want to kick off before going home, so I create a SSH session to the server to start the process, but then I want to close my laptop and go home and later, after dinner, I want to check on the process that I started before leaving work. How can I do that with SSH? My understanding is that if you break your SSH connection you will also break your login session on the server, therefore killing the long running process.
19060
I have Windows 10 Anniversary Edition with "Bash on Ubuntu on Windows" installed and working. I'd like to be able to SSH into this Ubuntu instance, but although I have openssh-server installed and configured (and listening on port 2200), when I try to ssh to "localhost:2200", it tells me "Server unexpectedly closed network connection".  Has anyone been able to successfully accomplish this?
20110
I have an expect script that connects to a few routers through ssh.  All these routers have the same password (I know, it's wrong), and the script needs to know that password in order to be able to connect to the routers.  Currently, the password is passed to my script as an argument on the command line, but this means that there's a trace of that password in my .bash_history file as well as in the running processes.  So instead I would like the user to be prompted for a password, if possible silently.  Do you know whether or not it's possible to prompt the user for a password with expect?  Thank you.  Edit: if I was connecting to servers instead of routers, I would probably use ssh keys instead of passwords.  But the routers I'm using just support passwords.
110176
I have a macbook pro with mavericks running. I am looking for a way to connect to a VPN network in terminal.   The reason why I want to do this is that I want to write a small program in Python which automatically detects the fastest VPN server among 30 servers. This is a self-motivated practicing project so I think I'll stick to Python language. So I break down the task and think the program may need to connect to one of the server first and after that, run a speed test.  So I'm now stuck in this first step because I realised establishing a VPN connection seems to be under system level because I can't find pre-written VPN module in python. So I guess it will be like I tell Python to tell system shell to connect to a VPN server.  As I poking around and I found a command by typing apropos vpn. It's called vpnagent. But man vpnagent doesn't provide useful information nor which vpnagent tells me the utility is not installed in my Mac. Another interesting thing I found was pppd but setting up the configuration file was very frustrating. I didn't manage to do that.   So is there a way of connecting to VPN using terminal? In addition, since I'm new to programming, any comment on my project is also welcome. Thank you in advance.
117284
I was searching for a way to connect to one of my servers who is behind a jumpbox via SSH. I was able to do so by running the following command:  $ ssh -tt ptitpou@jumpbox ssh -tt ptitpou@targetserver   Now, I want to be able to use that server to access webservers on targetserver's network. For that, I followed  and I was able to successfully access the websites. The command I used was:   $ ssh -L 127.0.0.1:9999:localhost:9696 -tt ptitpou@jumpbox ssh -D 9696 -tt ptitpou@targetserver   The question I want to ask is how to use the ssh -o ProxyCommand to command in a slightly cleaner way. I tried to put the second part of the command as the proxy command and I had a successful SSH connection, but I was unable to use the proxy after that.   I've also tried  but the command line hangs after connecting to targetserver.  How can I do that? Am I wrong to try to use ProxyCommand option for this task?
79626
I'd like to run a privileged command upon successful SSH logins at the server's end. However, this has to be in the server configuration (sshd_config or anything else that cannot be manipulated or circumvented by the user) as I need this to be mandatory. That is, however restricted the system account used is, it needs to be able to manipulate a piece of data that requires superuser privileges (namely use ipset add conditionally - which I can script). Oh and I'd like to be able to access the contents of SSH_CLIENT, please - despite privileged execution. The command also must be unaffected by ChrootDirectory.  I am aware of /etc/ssh/sshrc, but that appears to be "client-side", i.e. it's being run by the user who is logging into the machine.  Does such a facility exist in OpenSSH? I am using version 6.6.    Also, the man page (ssh(1)) isn't all too clear about sshrc:     /etc/ssh/sshrc   Commands in this file are executed by ssh when the user logs in, just before the user's   shell (or command) is started.  See the sshd(8) manual page for more information.   So does this mean it also runs whenever external commands (via ForceCommand) or subsystems like internal-sftp get invoked, despite restricting settings like PTY allocation or forcing a chroot? If so, I could resort to a restrictive /etc/sudoers entry if there is no other way at all.  NB: I am not using inetd for obvious reasons (see sshd(8)) as described .
95791
Is there some way I can run a Bash script as root without being prompted for a password every time? I'm attempting to automate the process of starting my LAMPP install, which requires me running a couple of sudo commands to start and stop services. Optimally, I'd like to build this into an executable file, so all I'd have to do is click an icon. ;)  Obviously, I have the password, so that's not an issue. I just want to take my laziness one step farther and not have to enter it every time I start my localhost. After all, that's what programming is all about, right?
82426
I have an old Ubuntu server (13.04), and want to open a port for this online game server style software for a mate. I need a port open, and to tell it that port (I know how to tell the game that port).  For the sake of argument, let the port I want to open be 12345.  How do I do such, with no access to a GUI, only a Bash shell. Both how to open the port, and how to fiddle with the firewall so that port is visible to the world.   Thanks!  EDIT: I can port forward it internally (iptables esque), but I don't know what firewalling software I have installed. Is there an easy way to find that out? Also, how do I port forward the router as well, all from the terminal. Sorry for the earlier lack of detail
6107
I am issuing a command to a remote device in order to run a tcpdump on one of it's interfaces, but capture the output to a local .pcap file on my Ubuntu VM. The command that I'm issuing is as follows:  drew@Drew-Ubuntu:~/Desktop$ sshpass -p $password ssh -oKexAlgorithms=+diffie-hellman-group1-sha1 -T $username@$remote-ip -p $remote-port &lt;&lt; EOF \ &gt; /home/drew/Desktop/Example_Capture.pcap &gt; sh &gt; tcpdump -i eth5.1 -s 0 -n -v not host $local-ip -w - &gt; EOF tcpdump: listening on eth5.1, link-type EN10MB (Ethernet), capture size 65535 bytes ^Ct 23 drew@Drew-Ubuntu:~/Desktop$   The issue that I'm having is that this does allow me to start up a tcpdump. I can see the "Got $counter" values increasing as the tcpdump runs which is great. I can then simply "CTRL + c" in order to stop the capture and my file is generated at my specified location. The issue is that the first command "> sh" shows up in the very first line of this capture file, which stops me from being able to properly open this file for review.  I've tried adding "> sh > /dev/null", but I'm pretty sure that is only going to suppress output on the remote side, and not mine, as it didn't work. I've tried messing with the syntax of this a bunch, but either my command doesn't work entirely, or that first line is included. Hoping there is a proper syntax out there to just populate my local file with the tcpdump information.  Update:  I have since tried to update things to work within a single script that runs my tcpdump capture, and then once cancelled out, sed would clean up the first line as needed. Below is what I have built:  #!/bin/bash  read -p $'\e[32mEnter Remote IP: \e[0m' remote_ip read -p $'\e[32mEnter Username: \e[0m' username read -sp $'\e[32mEnter Password: \e[0m' password echo read -p $'\e[32mEnter File Save Location and Name: \e[0m' save_file echo -e '\e[32mEnter tcpdump Options (ie. -i eth1 -s 0 .....)\e[0m' read -p $'\e[32m*note: You do not need to enter the \"-w\" modifier to save to a file: \e[0m' tcpdump_options  sshpass -p $password ssh -oKexAlgorithms=+diffie-hellman-group1-sha1 -T $username@$remote_ip -p 30007 &lt;&lt;- EOF \ &gt; /tmp/Remote_Capture.pcap         sh         tcpdump $tcpdump_options -U -w - EOF  trap '{ sed 1d /tmp/Remote_Capture.pcap &gt; $save_file; rm /tmp/Remote_Capture.pcap; }' SIGINT SIGTERM   The issue with this is that when I enter a CTRL+C, it seems to terminate out of the entire script and not make it to that final line. My capture file gets created in my /tmp directory with that extra line up top, but the sed operations don't actually happen to clean things up and push the cleaned up file to my set location.
39469
Is there a way to run a script/command every time a user connects using ssh? Can it be configured globally (i.e run the script when any user login)?  I came across this question on Identica, but there is no answer yet and would like to know it anyway.
87946
I'm connecting to a Linux machine through SSH, and I'm trying to run a heavy bash script that makes filesystem operations. It's expected to keep running for hours, but I cannot leave the SSH session open because of internet connections issues I have.  I doubt that running the script with the background operator, the ampersand (&amp;), will do the trick, because I tried it and later found that process was not completed. How can I logout and keep the process running?